[fpc-devel] Behavior of conversion between vardate variants and string in fpc and delphi

Sergei Gorelkin sergei_gorelkin at mail.ru
Mon Apr 4 07:28:21 CEST 2011


Luiz Americo Pereira Camara пишет:
> 
> So, what should be done?
> 
> 1) Be totally compatible with Delphi: convert date to string with 
> hardcoded format and raise exception when doing the conversion back?
> 2) Use the hardcoded format used to convert from vardate variant to 
> string and vice versa?
> 3) Use shortdateformat to convert from vardate variant to string and 
> vice versa?
> 
> As pointed in http://support.embarcadero.com/article/35913 the hardcoded 
> is not hardcoded at all. It's the default system setting.
> 
> It's a mater of considering where is the Delphi bug. It should use 
> ShortDateFormat to do the vardate to string conversion ? Should the 
> inverse conversion (string to vardate) be allowed at all?
> 
Unfortunately I don't have recent Delphi, but here are results of some quick testing with Delphi 7:

a) Only *assignment* Variant -> TDateTime is broken, explicit casting (e.g. VarAsType(v, varDate)) 
works.
b) Conversion is done using Windows API (in general, everything Delphi can't or doesn't do itself, 
it passes on to Windows), having its pros and cons:

test('12 января 2011') -> 12.01.2011     Works, assuming locale is Russian
test('04/07/1999') -> 04.07.1999
test('04/17/1999') -> 17.04.1999         Do you really want day and month swapped?

function test(const s: string): string;
var
   v,v1: variant;
   d: tdatetime;
begin
   v := s;
   {d := v;}         // broken
   v1 := VarAsType(v, varDate);
   d := v1;
   result := DateTimeToStr(d);
end;

Regards,
Sergei





More information about the fpc-devel mailing list