I'm producing an API in rails, where some of the responses include a date.
In my database, the fields are setup as datetime fields, which rails then turns into ActiveSupport::TimeWithZone
objects. When I respond to a request with a datetime in, I expect to get something like
2013-07-23T01:18:32Z
But instead, I'm getting
2013-07-23T01:18:32.000Z
Why is there the additional .000
at the end? As right now this is breaking the code on the client I'm writing. Obviously I could fix the client by changing the format it is expecting, but I'd like to know why rails is doing this in the first place, as the documentation suggests it shouldn't have the .000
there.
For others coming here from Google. There is a related question with a more up to date answer for Rails 4.1+ here.
The precision for JSON encoding of time is now configurable. According to the Rails upgrade guides you can now add the following line in an initializer instead of monkey-patching:
ActiveSupport::JSON::Encoding.time_precision = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With