Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Bug in Time#to_json

#3064

There seems to be a bug in Rails' processing of Time#to_json. It just drops the timezone information

In Rails:

$ ./script/console
Loading development environment (Rails 2.3.3)
>> Time.now
=> Mon Aug 17 10:33:41 +0200 2009
>> Time.now.to_json
=> "\"2009/08/17 10:33:44 +0000\""

Here it just drops the time zone but doesn't adjust the time to utc.
I could call Time.now.utc.to_json but that won't work when I'm doing something like {:date => Time.now}.to_json

With the json gem

$ irb
>> require 'json'
=> true
>> Time.now
=> Mon Aug 17 10:34:36 +0200 2009
>> Time.now.to_json
=> "\"Mon Aug 17 10:34:40 +0200 2009\""

I've done some digging through ActiveSupport::JSON and ActiveSupport::TimeWithZone but I'm not sure where this is being handled.
If you can give me some pointers on where to_json is being handled in Rails, I'll submit a patch

Reported by Andrew · August 17th, 2009 @ 09:38 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Geoff Buesing
    Geoff Buesing

    I can't reproduce this with a fresh Rails 2.3.3 app:

    $ script/console
    Loading development environment (Rails 2.3.3)
    >> Time.now
    => Tue Aug 18 08:11:36 -0500 2009
    >> Time.now.to_json
    => "\"2009-08-18T08:11:39-05:00\""
    

    Do you have any plugins installed that would be adjusting Time#to_json or #xmlschema? Also, are you setting ActiveSupport.use_standard_json_time_format = true in config/initializers/new_rails_defaults.rb?

    Fyi the JSON gem doesn't define a specific Time#to_json by default; that's just a JSON version of its #to_s output.

    August 18th, 2009 @ 02:16 PM

  2. Andrew
    Andrew

    Sorry, you're right. I should have checked this on a fresh install.
    I have found the offending plugin and will fix it there.

    Thanks very much

    August 18th, 2009 @ 02:42 PM

  3. Geoff Buesing
    Geoff Buesing
    • State changed from new to invalid

    August 18th, 2009 @ 03:40 PM