Travis Griggs
Travis Griggs

Reputation: 22252

How to work with datetimes betwixt javascript, JSON, and python, and mongodb?

Colleague and I have scabbed together a little app that uses a bunch of JS in the browser, and communicates with a Tornado (Python3) server via JSON, the server uses mongodb as a backing store for persistent data. This is a sort of first for both of us.

What we're finding difficult is how to interchange datetime information between the JS and Python. We do believe that we should use UTC times for everything. JSON doesn't have a datetime literal, so we have to encode it somehow. We naively (?) used the JS notion of milliseconds from 1970 and have been sharing big integers back and forth. So the JS code might get the current utc now time with something like:

var newTime = new Date().getTime();

On the Python3/mongo side, we'd like to use real datetime objects, so we convert that with something like:

datetime.datetime.utcfromtimestamp(jsMilliseconds / 1000)

But then when we have to send date back, said Python3 object only has a timestamp() method. And round tripping that doesn't seem to create the same time. So we've been frustrated with this.

What we're looking for is for someone with experience to give us a good set of idioms to use here. Should we be using strings, instead of the ms integers when passing back and forth with JSON? What are the recommended methods to use on both sides to go between that format? Or should we stick with the integers and which methods should we be using then?

Upvotes: 2

Views: 1298

Answers (1)

Grady G Cooper
Grady G Cooper

Reputation: 1064

There are obviously a lot of requirements to consider when dealing with time. However, in the case you want to maintain a Date/Time to be displayed to user in their time zone and use mongo/python/java/javascript, I've used ISO 8601 date formats and always store UTC (Zulu) time. Also, if you truly want to maintain the actual time that something occured, not only do you need to store the "date/time" you need to also store the "timezone" (for example IANA timezone string) where the event occurred.

For a lifetime a reading, you can search for "date time best practices". This answer has a good start on discussion: Daylight saving time and time zone best practices

Alright, now to the code (all of this can be found readily on the internet if you search for " parse||output ISO 8601 parsing" (e.g. "python parse ISO 8601 date string"):

  1. JSON - on the wire between JavaScript and Python backend send a complex (can be simple if you have no need to preserve time zone) object with ISO-8601 formatted string and string to store time zone string:
    { "dateTime": "1970-07-10T12:00:00.047Z", "timeZone": "America/New_York" }
  2. Java Script

    a. read datetime string JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse
    var d = Date.parse("2011-04-26T13:16:50Z");
    b. write datetime string How do I output an ISO 8601 formatted string in JavaScript?
    var date = new Date(); date.toISOString(); //"2011-12-19T15:28:46.493Z"

  3. Python

    a. read datetime string How do I translate a ISO 8601 datetime string into a Python datetime object?
    import dateutil.parser yourdate = dateutil.parser.parse(datestring)
    b. write datetime string Python - Convert date to ISO 8601
    import dateutil.parser as parser date.isoformat()

  4. Mongo - store datetime as native datetime field http://docs.mongodb.org/manual/reference/mongodb-extended-json/#date
    a. store datetime string (notice "$date" and that datetime is in ZULU/UTC (denoted by 'Z')):
    "dateTime" : { "$date" : "1970-07-10T13:00:00.047Z"}

Reference:
1. IANA TimeZone Databasehttp://en.wikipedia.org/wiki/Tz_database
2. The google calendar API (event resource) can be used as an example for RFC 3339, as well (see start/end): https://developers.google.com/google-apps/calendar/v3/reference/events

Upvotes: 2

Related Questions