Sign in Join

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

Mailing lists / lazarus / 2012-01

JSON and Object Pascal DateTime

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

What the best way to save a datetime value in a JSON object?

1 - JSONObj['datetime'].AsString := DateTimeToStr(Now); // No no no ... :/
2 - JSONObj['datetime'].AsString := DateTimeToJS(Now); // Result:
\/Date(1240718400000)\/, So: { "datetime": "\/Date(1240718400000)\/" }
// Hm... :|

1 or 2, or other?

Is there a native function to convert it? From JSON to DateTime and from DateTime to JSON?

Thanks,

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ====================================

Reply by email

silvioprog

Hello,

What the best way to save a datetime value in a JSON object?

1 - JSONObj['datetime'].AsString := DateTimeToStr(Now); // No no no ... :/
2 - JSONObj['datetime'].AsString := DateTimeToJS(Now); // Result:
\/Date(1240718400000)\/, So: { "datetime": "\/Date(1240718400000)\/" }
// Hm... :|

1 or 2, or other?

Is there a native function to convert it? From JSON to DateTime and from DateTime to JSON?

Thanks,

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ====================================

Reply by email

silvioprog

Hello,

What the best way to save a datetime value in a JSON object?

1 - JSONObj['datetime'].AsString := DateTimeToStr(Now); // No no no ... :/
2 - JSONObj['datetime'].AsString := DateTimeToJS(Now); // Result:
\/Date(1240718400000)\/, So: { "datetime": "\/Date(1240718400000)\/" }
// Hm... :|

1 or 2, or other?

Is there a native function to convert it? From JSON to DateTime and from DateTime to JSON?

Thanks,

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ====================================

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 14 lines
Hello, What the best way to save a datetime value in a JSON object? 1 - JSONObj['datetime'].AsString := DateTimeToStr(Now); // No no no ... :/ 2 - JSONObj['datetime'].AsString := DateTimeToJS(Now); // Result: \/Date(1240718400000)\/, So: { "datetime": "\/Date(1240718400000)\/" } // Hm... :| 1 or 2, or other? Is there a native function to convert it? From JSON to DateTime and from DateTime to JSON?

Strictly speaking, JSON does not know a date/time object. So, how to write it depends on the client application.

ExtJS uses by default an ISO format (and expects one as of ExtJS 4):

1995-07-18T17:21:28-02:00

Which seems the best to do anyway.

Michael.

Reply by email

silvioprog
to Michael Van Canneyt

2012/1/30 Michael Van Canneyt :
[...]

Strictly speaking, JSON does not know a date/time object. So, how to write it depends on the client application.

Yes, I observe it doesn't deal with date type... ;)

Show quoted text 7 lines
ExtJS uses by default an ISO format (and expects one as of ExtJS 4): 1995-07-18T17:21:28-02:00 Which seems the best to do anyway. Michael.

I would like to avoid ecessive convertions. In the attatched example, you can see the reasons why I prefer to use another format.

I will use own conversion routines (MSecToDateTime/DateTimeToMSec). Saving in this format, it's easy to handle with dates without converting from string to datetime.

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ==================================== -------------- next part -------------- A non-text attachment was scrubbed... Name: jsondt.zip Type: application/zip Size: 1696 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120130/d53b5244/attachment.zip>

Reply by email

silvioprog
to Luiz Americo Pereira Camara

2012/1/30 Luiz Americo Pereira Camara :

Show quoted text 10 lines
On 30/1/2012 17:09, silvioprog wrote: > > Hello, > > What the best way to save a datetime value in a JSON object? I store as a float Luiz

Me too. ^^

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ====================================

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 13 lines
2012/1/30 Luiz Americo Pereira Camara : > On 30/1/2012 17:09, silvioprog wrote: >> >> Hello, >> >> What the best way to save a datetime value in a JSON object? > > > I store as a float > > Luiz Me too. ^^

In that case, why do you still ask ? :-)

Like I said, there is no standard, it really depends on the code in the browser what to send best. (I assume, of course, that the JSON is sent to a browser). Sending as text has the advantage that you can check your server responses visually and see what date/time is sent. A float like 45685.25 is a valid timestamp, but which date/time is it ?

Michael.

Reply by email

silvioprog
to Michael Van Canneyt

2012/1/31 Michael Van Canneyt :
[...]

In that case, why do you still ask ? :-)

Curiosity. :) I saw this article:
http://msdn.microsoft.com/en-us/library/bb299886.aspx. So I was really in doubt about which type to use.

Show quoted text 8 lines
Like I said, there is no standard, it really depends on the code in the browser what to send best. (I assume, of course, that the JSON is sent to a browser). ?Sending as text has the advantage that you can check your server responses visually and see what date/time is sent. A float like 45685.25 is a valid timestamp, but which date/time is it ? Michael.

I separate the presentation of persistence, so for me it is advantageous to use json as a database.

Thank you Michael, sorry for not thanking yesterday, when i wrote the email was already dawn, and I was very sleepy. :)

Silvio Cl?cio ==================================== Site - <silvioprog.com.br> LazSolutions - <code.google.com/p/lazsolutions> ====================================

Reply by email