10

I'm writing a app that exposes a REST API. Some of the query parameters will be date/time (accurate to second), and some of the responses will be timestamps (accurate to millisecond).

The API implementation on the server is in Java. The client apps can be anything - java, javascript, .NET. The API returns XML or JSON data. Date/Time data is stored in a Oracle database.

Does anyone have recommendations, based on prior pain, of what the best format format is for passing these date/time values. I'm thinking myself to just use a good old fashioned long to store the number of milliseconds since January 1, 1970, 00:00:00 GMT.

Edit The date range covered in the API is for real time events, so there will be nothing before 2010, and (setting myself up for abuse here) nothing after 2038.

I guess best would be determined by

a) Wide variety of languages support converting this long into internal date object, without having to write code to do it.

b) Lowest CPU overhead (on server app)

1 Answer 1

9

ISO 8601 all the way

Using any epoch-based method means you are bound to the range (in most systems) of a signed 32-bit INT (1901-12-13T20:45:52+00:00 through 2038-01-19T03:14:07+00:00) which, is really more of a timestamp than a date, since it can't handle far-reaching historical or future dates.

4
  • Hi Peter - I've edited question to clarify the range of dates I will be querying, comfortable within the 'epoch-range' - would this change your recommendation for ISO 8601 at all?
    – Kevin
    Aug 24, 2010 at 16:35
  • @Tom I know, I'm just waiting for it Aug 24, 2010 at 16:57
  • @Kevin Yes, since the Date constructor understands ISO 8601 dates just fine. You may want to demarcate dates in a special way as suggested in Darin's answer, but I still firmly believe we should stick to ISO 8601. As an added bonus, it's way more human-readable than a timestamp value. And, FWIW, Facebook's Graph API (which is all JSON) uses ISO 8601. Aug 24, 2010 at 17:03
  • @PeterBailey - This link - agiletribe.wordpress.com/2015/06/10/jsonrest-api-handling-dates recommends integer epoch to avoid human readability problems that could be experienced with iso-8601 format. Let me know if you have different views Mar 7, 2018 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.