0

私の大きなアプリケーションでは、ロケール設定とは別の日付形式を読み取ろうとしています。しかし、それは例外的に失敗しました。そこで、再現する簡単なデモを作成しました。

単純な間違いを犯した可能性があります。私の Windows XP のローカル設定は、「dmyyyy」であるフィンランドの日付形式です。「yyyy-mm-dd」であるスウェーデン語の形式を読みたいです。助けてください!

unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

const
  cnFormat     = 'yyyy-mm-dd';             // Swedish dateformat
  cnFIFormat   = 'd.m.yyyy';               // Finnish dateformat

type
  TForm5 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    fSetting: TFormatSettings;
    function GetCustomDateFormatSettings(aDateFormat: String = cnFormat): TFormatSettings;
    function GetSafeDate(aDate: String): TDate;
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

procedure TForm5.FormCreate(Sender: TObject);
var
  vDate: TDate;
begin
  fSetting := GetCustomDateFormatSettings;
  vDate := GetSafeDate('2010-01-04');
end;

function TForm5.GetCustomDateFormatSettings(aDateFormat: String): TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, Result);
  Result.ShortDateFormat := aDateFormat;
end;

function TForm5.GetSafeDate(aDate: String): TDate;
begin
  try
    Result := StrToDate(aDate, fSetting);  // <- Exception here
  except
    on E: EConvertError do
    begin
      // logic to recover from exception
    end;
  end;
end;

end.
4

1 に答える 1