書式文字列がばらばらで覚えきれない
ひとりWikiのプラグインで、文書の最終更新日時を表示するものを作っています。具体的にはこんなプログラム。
library lastmodify; // http://homepage2.nifty.com/Mr_XRAY/Delphi/plSamples/780_FileInfoListETC.htm // を参考にしました uses ActiveX, SysUtils, Windows; function FileTimeToDateTime(FileTime: TFileTime): TDateTime; var SystemTime: TSystemTime; TempTime: TFileTime; begin try // FileTime形式(世界協定時刻)を地域にあわせる FileTimeToLocalFileTime(FileTime, TempTime); // FileTime形式をSystemTime形式にする FileTimeToSystemTime(TempTime, SystemTime); // SystemTime形式をDelphiのTDateTime形式に変換 Result := SystemTimeToDateTime(SystemTime); except Result := 0; end; end; function GetFileTime(FileName: string; Fmt: string): string; var SearchRec: TSearchRec; Ret: integer; FileData: TWin32FindData; DateTime: TDateTime; DateFormat: string; begin Ret := SysUtils.FindFirst(FileName, faAnyFile, SearchRec); try if Ret = 0 then begin FileData := SearchRec.FindData; DateTime := FileTimeToDateTime(FileData.ftLastWriteTime); if DateTime <> 0 then begin DateFormat := Fmt; if DateFormat = '' then begin DateFormat := 'yyyy-mm-dd hh:nn:ss'; end; result := FormatDateTime(DateFormat, DateTime); end else begin result := ''; end; end else begin result := ''; end; finally SysUtils.FindClose(SearchRec); end; end; function GetPluginType: longint; stdcall; export; begin result := 1; end; function GetName: TBStr; stdcall; export; begin result := SysAllocString('lastmodify'); end; function GetVersion: TBStr; stdcall; export; begin result := SysAllocString('1.0.0'); end; function GetAuthor: TBStr; stdcall; export; begin result := SysAllocString('MAS'); end; function GetSyntax: TBStr; stdcall; export; begin result := SysAllocString('&lastmodify(書式)'); end; function GetDescription: TBStr; stdcall; export; begin result := SysAllocString('最終更新日時。'); end; function GetExample: TBStr; stdcall; export; begin result := SysAllocString('&lastmodify() &lastmodify(yyyy-mm-dd hh:nn:ss)'); end; function PluginInline( EscapedString: PChar; PlainString: PChar; DocumentName: PChar; FileName: PChar; DocumentFolder: PChar; TemplateFolder: PChar; SettingsName: PChar ): TBStr; stdcall; export; var Buff: string; StrMem: PWideChar; Ret: TBStr; Len: integer; Fmt: string; FileTime: string; begin Fmt := PlainString; FileTime := GetFileTime(DocumentFolder + '\' + FileName, Fmt); Buff := FileTime; Len := Length(Buff) * SizeOf(WideChar) + 1; GetMem(StrMem, Len); try StringToWideChar(Buff, StrMem, Len); Ret := SysAllocString(StrMem); finally FreeMem(StrMem, Len); end; result := Ret; end; exports GetPluginType, GetName, GetVersion, GetAuthor, GetSyntax, GetDescription, GetExample, PluginInline; begin end.
引数に日時から文字列への変換のための書式を指定できる親切設計です。
日時から文字列への変換はFormatDateTime関数を使っているので、この関数で利用できる形式の書式が指定できるようになっています。
この書式ですが 2008-03-21 01:02:03 と表示したい時はこのように指定します。
yyyy-mm-dd hh:nn:ss
何となく分かりますね。
これだけだったら覚えておけばよいのですが、Perlで同じようなことをやろうとした時にPOSIX::strftimeを使うと、こう書くことになります(その他詳しくは日付の書式指定)。
%Y-%m-%d %H:%M:%S
これはこれで何となく分かりますけど、さっきと全然違います。
さらに、Oracleで同じことをやろうとしてTO_CHAR関数をつかうとこれです。
YYYY-MM-DD HH24:MI:SS
これも分からなくはないですがやっぱり違います。しかもついうっかりHH24をHHと書いてしまって泣ける時があります(動作確認をしても間違いに気づかない場合が)。
JavaのSimpleDateFormatだとこうらしいです。
yyyy-MM-dd HH:mm:ss
とまあこれだけ色々あると、覚えきれないですね。