Thema: Delphi strftime für Delphi ?

Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#12

AW: strftime für Delphi ?

  Alt 12. Jan 2019, 12:48
Es ist auch relativ simpel so einen Parser zu basteln.

Hier ein Anfang
Delphi-Quellcode:
function strftime(const Format: string; Value: TDateTime; const FormatSettings: TFormatSettings): string; overload;
var
  controlChar: Boolean;
  current: Char;
begin
  Result := string.Empty;
  controlChar := false;
  for current in Format do
  begin
    if controlChar then
    begin
      case current of
        // ---
        // Tag
        // ---
        'd':
          // Tag des Monats als zweistellige Zahl (ggf. mit vorangestellten Nullen)
          // 01 bis 31
          Result := Result + FormatDateTime('dd', Value, FormatSettings);
        // -----
        // Monat
        // -----
        'm':
          // Zweistellige numerische Darstellung des Monats
          // 01 (für Januar) bis 12 (für Dezember)
          Result := Result + FormatDateTime('mm', Value, FormatSettings);
        // ----
        // Jahr
        // ----
        'Y':
          // Vierstellige numerische Darstellung des Jahres
          // Beispiel: 2038
          Result := Result + FormatDateTime('yyyy', Value, FormatSettings);
        // -------------
        // Verschiedenes
        // -------------
        'n':
          // Ein Zeilenvorschubzeichen ("\n")
          Result := Result + sLineBreak;
        't':
          // Ein Tabulatorzeichen ("\t")
          Result := Result + #9;
        '%':
          // Ein Tabulatorzeichen ("\t")
          Result := Result + '%';
      else
        Result := Result + '%' + current;
      end;
      controlChar := False;
    end
    else
    begin
      if current <> '%then
      begin
        Result := Result + current;
      end
      else
        controlChar := true;
    end;
  end;
  if controlChar then
    Result := Result + '%';
end;

function strftime(const Format: string; Value: TDateTime): string; overload;
begin
  Result := strftime(Format, Value, System.SysUtils.FormatSettings);
end;

function strftime(const Format: string): string; overload;
begin
  Result := strftime(Format, System.SysUtils.Now());
end;
Man braucht jetzt nur nach den Case-Teil um die anderen Zeichen ergänzen und fertig ist es.
  Mit Zitat antworten Zitat