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

2011-04-04 Thread LacaK




Running this code in Delphi 7 gives the exception below:

var
  V: Variant;
  D: TDateTime;
begin
  ShortDateFormat := 'dd/mm/'; // 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

end;

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?

In DelphiXE:
raised exception class EVariantTypeCastError with message 'Could not 
convert variant of type (UnicodeString) into type (Double)'


Laco.

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2011-04-04 Thread LacaK



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?

Point 3 sounds to me as best and logical.

This code in DelphiXE:

  ShortDateFormat := 'dd#mm#'; // the problem occurs regardless of 
the format option

  DateSeparator:='#';
  V := '20#04#2011';
  v1:=varAsType(V, varDate);
  D:=v1;
  Writeln('Date: ' + DateToStr(D));

Outputs:
Date: 20#04#2011

So it seems, that DelphiXe also uses ShortDateFormat, DateSeparator

Laco.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2011-04-04 Thread Luiz Americo Pereira Camara

On 4/4/2011 04:34, LacaK wrote:



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?

Point 3 sounds to me as best and logical.



Agree. my second patch does that

Luiz
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2011-04-03 Thread Sergei Gorelkin

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


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel