This project is archived and is in readonly mode.

#3064 ✓invalid
Andrew

Bug in Time#to_json

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

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

Comments and changes to this ticket

  • Geoff Buesing

    Geoff Buesing August 18th, 2009 @ 02:16 PM

    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.

  • Andrew

    Andrew August 18th, 2009 @ 02:42 PM

    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

  • Geoff Buesing

    Geoff Buesing August 18th, 2009 @ 03:40 PM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Tags

Pages