Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveSupport::TimeWithZone as_json date format issue

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.

like image 868
PaReeOhNos Avatar asked Jul 23 '13 08:07

PaReeOhNos


1 Answers

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
like image 162
plainjimbo Avatar answered Oct 21 '22 06:10

plainjimbo