That’s it! I have had enough. I have had enough of DateTime, time strings, datetime strings, Time.parse(), MySQL time, JSON time, CouchDB time, Ruby time, system time and all the rest of it.
I have come to realise that there is one, and only one, appropriate way to store time so everything can understand it without endless string conversion problems, and that is in a numeric format of milliseconds since Epoch UTC.
The only appropriate time to convert from milliseconds into a human-readable string is upon presentation to an actual human – ie, in the View. Or, if you like, store a second time field in any records you save or pass around – just make sure your program doesn’t care about those.
This revelation comes from YET ANOTHER journey into ISO document land as I realised that not only do I have no idea how to store milliseconds in a JSON date/time string, but neither does anyone else. RIGHT!! THAT IS IT! From now on, dates are an integer.
Maybe.
Anyway, here’s some notes on getting millisecond-precision time references in and out of Ruby and JS, both of whose time classes I am no fan of, more for me than anything else..
Ruby example:
# Getting milliseconds since Epoch out of Ruby: time_float = Time.now.to_f time_ms = (1000* time_float).to_i #writing >> t = Time.now.utc => Thu Nov 20 23:01:32 UTC 2008 >> time_float = t.to_f => 1227222092.50133 >> time_ms = (1000* time_float).to_i => 1227222092501 #reading >> n = Time.at(time_ms / 1000.0).utc => Thu Nov 20 23:01:32 UTC 2008 >> t => Thu Nov 20 23:01:32 UTC 2008 # check we retained usec through the process >> n.usec # note we lost microsecond precision, this is intended => 501000 >> t.usec => 501333
Reducing Ruby Time.now to millisecond precision:
time_float = Time.now.utc.to_f time_msp = ("%0.3f" % time_float).to_f
That doesn’t have much to do with time, I just thought it was cool. Note that apparently doing precision reduction with strings is faster.
Javascript
// Reading an ms-since-epoch time: js> date_from_above = new Date(1227222092501) Fri Nov 21 2008 10:01:32 GMT+1100 (EST) js> date_from_above.toUTCString() Thu, 20 Nov 2008 23:01:32 GMT // note same as above // Make a new current date js> t = new Date Fri Nov 21 2008 10:28:12 GMT+1100 (EST) // Output date object in milliseconds-since-epoch format js> t.valueOf() 1227223692041