Sign in Join

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

Mailing lists / fpc-pascal / 2013-02

JSON dates

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Reinier Olislagers

Hi,

Creating a client/server scanning application that uses JSON to exchange data.

Noticed there's no support for dates in the JSON specs; apparently various approaches are used (Unix epoch/ISO 8601 date strings/something Microsoft did).

Noticed that Firefox gives this:
new Date().toJSON();
/*
2013-02-21T09:47:42.467Z
*/
which looks like an ISO date to me.

Currently FPC JSON gives a huge number for current date/time, presumably just the representation of TDateTime.

Is this ISO 8601 format currently the most widely used? It seems the most sane to me.

Thanks,
Reinier

Graeme Geldenhuys
to Reinier Olislagers
Reinier Olislagers wrote 7 lines
Noticed that Firefox gives this: new Date().toJSON(); /* 2013-02-21T09:47:42.467Z */ which looks like an ISO date to me.

Yes, that is the ISO 8601 format, and the only sane format you should use. Even in my database application, my dates are stored as text fields using the ISO 8601 date/time format, instead of db server Timestamp fields (which could cause confusion too, depending on server locale settings).

Is this ISO 8601 format currently the most widely used? It seems the most sane to me.

Yes, that is the safest and most understood format.

In the tiOPF project I created a couple of methods (and unit tested them) to do conversion between string format and TDateTime format. You are welcome to use them for your purpose.

----------------[ Core/tiUtils.pas ]--------------------
  function tiDateTimeAsIntlDateStor(const ADateTime: TDateTime): string;
  function tiDateTimeAsIntlDateDisp(const ADateTime: TDateTime): string;
  function tiDateAsIntlDateDisp(const ADateTime: TDateTime): string;
  function tiIntlDateStorAsDateTime(const AValue: string): TDateTime;
  function tiIntlDateDispAsDateTime(const AValue: string): TDateTime;
----------------------------------------

Code can be found here...

http://tiopf.svn.sourceforge.net/viewvc/tiopf/tiOPF2/Trunk/Core/tiUtils.pas?view=markup

Also, here is a summary of ISO 8601:
http://www.cl.cam.ac.uk/~mgk25/iso-time.html

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Reinier Olislagers
to Graeme Geldenhuys
Graeme Geldenhuys wrote 5 lines
On 2013-02-21 09:59, Reinier Olislagers wrote: > Is this ISO 8601 format currently the most widely used? It seems the > most sane to me. Yes, that is the safest and most understood format.

So the JSON date wars have more or less ceased then? Anyway, doesn't matter, I can always adapt the code to read other formats. Having some readable format instead of the tdatetime float is already progress.

In the tiOPF project I created

Thanks, I have already written some code meanwhile.

And yes, I know tiopf's unit tested, where to get it etc... ;)