2

I populate a TStringList under Delphi 7 with the return values of that function :

function  dateTime2str(td : TDateTime) : string;
var
  iformatsettings : tformatsettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, iformatsettings);

  result := DateTimeToStr(td, iFormatSettings);
end;

Then I read each values of the TStringList with that function :

function str2DateTime(s : string) : TDateTime;
var
  iformatsettings : tformatsettings;
begin

  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, iformatsettings);


  result := strtodatetime(s,iFormatSettings);

end;

but it triggers an EConvertError exception : '6/7/12 3:02:31 AM' is not a valid date and time.

Why ?

Many thanks

user382591
  • 1,320
  • 5
  • 19
  • 39
  • 1
    I guess a date string of that format does not fit the locale settings on this machine. – David Heffernan Jul 06 '12 at 08:52
  • Ok, but I use the same iformatsettings for the 2 functions – user382591 Jul 06 '12 at 09:12
  • Are you quite sure about that? Are you saying you are running these functions on the same machine? – David Heffernan Jul 06 '12 at 09:43
  • Just to be sure, write the values of iFormatSettings `ShortDateFormat`,`DateSeparator`,`LongTimeFormat` and `TimeSeparator` in both functions. – LU RD Jul 06 '12 at 13:10
  • There are issues with other Delphi versions and/or a (64-bit) Windows version that look similar to yours, like http://qc.embarcadero.com/wc/qcmain.aspx?d=80189 and http://stackoverflow.com/questions/1767946/getthreadlocale-returns-different-value-than-getuserdefaultlcid. I found these Googling for 'GetLocaleFormatSettings "delphi 7" bug' and 'GetLocaleFormatSettings delphi bug'. I suggest you do some digging around. – Jan Doggen Jul 06 '12 at 13:53

1 Answers1

0

By using FormatDateTime you know the exact format the date and time has, so when you retrieve it, it will be exactly the same, instead of depending on the locale configuration.

If the first function run in one computer and the second in other, you'll still have problems, using your first approach.

MarcoA
  • 3
  • 4