Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-09

Conversion from TField.AsDateTime to string

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

I'm working with a CGI that returns data as JSON. Then I read this data from a GTK2 or Win32 client.

The JSON may have fields containing dates, that are populated from a database using TSqlQuery, then converted to TDateTime using Field.AsDateTime and added to a JSONObject using TJSonObject.Add(.., ..).

The result is something like this:

{
"apellido" : "BLANDINO",
"nombres" : "ALBERTO",
"fecha" : 40772.3926388889
}

On client side, instead of receiving 40772.3926388889 I can see this while debugging:

' 4.07714066087963E+004'

I don't know, but maybe this is a GDB conversion issue.

To convert the JSON string to TDateTime, I use StrToFloat, then I show the result using:

FormatDateTime('YYYY-MM-DD', myDate);

On Linux I get: 2011-08-17 09:25:24
On Win32 I get: 34248-02-16 00:00:00

Why the difference?.

Reply by email

Hans-Peter Diettrich
to Leonardo M. Ramé
Show quoted text 9 lines
To convert the JSON string to TDateTime, I use StrToFloat, then I show the result using: FormatDateTime('YYYY-MM-DD', myDate); On Linux I get: 2011-08-17 09:25:24 On Win32 I get: 34248-02-16 00:00:00 Why the difference?.

I'd guess that the time base is different for both OS, so that the float value is specific to the OS where the date was encoded. You can test yourself with a date set to 0.0.

If I'm right, dates should be stored as date strings, not as floats.

DoDi

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé: I'm working with a CGI that returns data as JSON. Then I read this data from a GTK2 or…
Leonardo M. Ram? wrote 21 lines
I'm working with a CGI that returns data as JSON. Then I read this data from a GTK2 or Win32 client. The JSON may have fields containing dates, that are populated from a database using TSqlQuery, then converted to TDateTime using Field.AsDateTime and added to a JSONObject using TJSonObject.Add(.., ..). The result is something like this: { "apellido" : "BLANDINO", "nombres" : "ALBERTO", "fecha" : 40772.3926388889 } On client side, instead of receiving 40772.3926388889 I can see this while debugging: ' 4.07714066087963E+004' I don't know, but maybe this is a GDB conversion issue.

Most likely.

Show quoted text 9 lines
To convert the JSON string to TDateTime, I use StrToFloat, then I show the result using: FormatDateTime('YYYY-MM-DD', myDate); On Linux I get: 2011-08-17 09:25:24 On Win32 I get: 34248-02-16 00:00:00 Why the difference?.

Probably because strtofloat takes into account localization settings.

You should use val() from the system unit. It ignores localization.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt
Michael Van Canneyt wrote 18 lines
On Fri, 9 Sep 2011, Leonardo M. Ram? wrote: > >To convert the JSON string to TDateTime, I use StrToFloat, then I show the result using: > >FormatDateTime('YYYY-MM-DD', myDate); > >On Linux I get: 2011-08-17 09:25:24 >On Win32 I get: 34248-02-16 00:00:00 > >Why the difference?. Probably because strtofloat takes into account localization settings. You should use val() from the system unit. It ignores localization. Michael.

Here's more info. I didn't noted the field type on the PostgreSql database is "timestamp without time zone", I thought it was type Date.

To get the value as TDateTime, I use myField.AsDateTime, then convert this value to JSON this is server side. On client side, I receive the string, then convert it to Variant, and using RTTI I assign the value to a published property of an object.

To assign the variant value to the published property of tipe TDateTime, I use this:

PDouble(P)^ := VarAsType(lValue, varDouble);

On Delphi this works as expected. Could this assignment be causing the wrong conversion?.

Reply by email