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

Luiz Americo Pereira Camara luizmed at oi.com.br
Mon Apr 4 02:22:07 CEST 2011


As pointed in bug http://bugs.freepascal.org/view.php?id=19075 
converting from vardate variants (variants with TDateTime values) to 
string is done differently in Delphi and fpc. According to 
http://support.embarcadero.com/article/35913 the conversion is done with 
the default OS format option independent of shortdatetime.

In the other way (converting from a string variant to a TDateTime) is 
not allowed at all in Delphi.

Running this code in Delphi 7 gives the exception below:

   V: Variant;
   D: TDateTime;
   ShortDateFormat := 'dd/mm/yyyy'; // the problem occurs regardless of 
the format option
   V := '20/04/2011';
   D := V;
   Memo1.Lines.Add('Date: ' + DateToStr(D)); // required to avoid dead 
code elimination

raised exception class EVariantTypeCastError with message 'Could not 
convert variant of type (String) into type (Double)'

Can someone check with more recent Delphi if this is still true?

My guess is that Delphi recognizes vardate variants as vardouble so it 
expects the string compatible with double type. This code works:

   V: Variant;
   D: TDateTime;
   V := FloatToStr(Date);
   D := V;
   Memo1.Lines.Add('Date: ' + DateToStr(D));

in fpc:
The same code will do the conversion dependent of the shortdateformat 
setting, i.e., changing to an incompatible format will raise the exception:

   ShortDateFormat := 'dd/mm/yyyy';
   // Uncomment below to raise an exception  EVariantError : Invalid 
variant type cast
   //ShortDateFormat := 'mm/dd/yyyy';
   V := '20/04/2011';
   D := V;
   WriteLn('Date: ' + DateToStr(D));

This has the consequence that, assuming the compatibility fix of my 
initial patch (that now i see as partially wrong), the following code 
will not work:

   ShortDateFormat := 'mm/dd/yyyy';
   V := Date;
   S := V;
   V2 := S;

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?

Someone with a more recent Delphi could help to see if one of these bugs 
were fixed.


More information about the fpc-devel mailing list