Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi strftime für Delphi ? (https://www.delphipraxis.net/199280-strftime-fuer-delphi.html)

scrat1979 12. Jan 2019 11:41

AW: strftime für Delphi ?
 
Zitat:

Zitat von Schokohase (Beitrag 1423171)
Wenn du bei
Code:
Aus %%Y wird %Y
alle
Delphi-Quellcode:
%Y
durch
Delphi-Quellcode:
YYYY
ersetzt, dann erhälst du
Code:
Aus %YYYY wird YYYY
Du brauchst aber für die korrekte Formatierung
Code:
"Aus %Y wird "YYYY

Ah, alles klar. Klingt irgendwie logisch :)

Schokohase 12. Jan 2019 12:48

AW: strftime für Delphi ?
 
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.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:35 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz