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.

to_xml datetime format regression

#6096

Rails 3.0.3 incorrectly formats datetime fields unless the record is reloaded from the database. The following gist provides the test case and possible fix: https://gist.github.com/722889

More info and Rails 3.0.0 comparison: https://gist.github.com/720887

Reported by Mike Dvorkin · December 1st, 2010 @ 05:25 AM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Aditya Sanghi
    Aditya Sanghi
    • Importance changed from to Low

    @Mike, Please send failing test and patch as per contributor's guide http://rails.lighthouseapp.com/projects/8994/sending-patches

    December 1st, 2010 @ 07:56 AM

  2. Aaron Patterson
    Aaron Patterson

    I've been investigating this, and it looks like if you set ActiveRecord::Base.time_zone_aware_attributes to true, the problem goes away.

    I checked out the v3.0.0 tag, and this test also fails in Rails 3.0.0.

    Here is a video of the test failing on 3.0.0:

    http://www.youtube.com/watch?v=nrrWA7c9L1c

    Here is a video of the test passing with time zone aware attributes:

    http://www.youtube.com/watch?v=Q3UV-oCEmP4

    I'm not convinced that there has been a regression during Rails 3.0.x. I do agree this is strange behavior. Given the "time_zone_aware_attributes" method, I wonder if this strange behavior is expected? It seems to me "time_zone_aware_attributes" should always be set to true, shouldn't it?

    December 8th, 2010 @ 11:07 PM

  3. Mike Dvorkin
    Mike Dvorkin

    Ah, and I was about to submit a patch :-). I'm happy with ActiveRecord::Base.time_zone_aware_attributes approach and it certainly makes sense to set it to true by default.

    I haven't run the tests against 3.0.0 so it's hard to say why they fail without looking into it (maybe Base.time_zone_aware_attributes should be explicitly set). But in any case, I've noticed this whole formatting discrepancy because of failing specs in my app after Rails 3.0.3 upgrade.

    December 8th, 2010 @ 11:49 PM

  4. Espen Antonsen
    Espen Antonsen

    Seems ActiveRecord::Base.time_zone_aware_attributes is set to true by default. But I still get the error when it is set to true as it was in my project.
    Adding 'raw_value = raw_value.in_time_zone if raw_value.respond_to?(:in_time_zone)' as Mike suggests in his gist fixes the problem. But that is not necessary in 3.0.1 where this problem does not occur. Strange. What could be the cause? Also, as mention in ticket 6054 this problem does not occur with to_json.

    December 9th, 2010 @ 03:18 AM

  5. Espen Antonsen
    Espen Antonsen

    Aaron: Your video of the test passing is marked as private. http://www.youtube.com/watch?v=Q3UV-oCEmP4

    December 9th, 2010 @ 03:32 AM

  6. Mike Dvorkin
    Mike Dvorkin

    Aaron, unfortunately flipping ActiveRecord::Base.time_zone_aware_attributes doesn't seem to affect datetime formatting. Please see https://gist.github.com/734332 (view raw).

    Let me follow-up with a patch...

    December 9th, 2010 @ 04:29 AM

  7. Espen Antonsen
    Espen Antonsen

    Mike: thanks for the update, but with your patch keep in mind that the fix you provided earlier by changing the value in ActiveModel::Serializers::Xml::Serializer::Attribute was not necessary in earlier versions. So I assume the problem must originate somewhere else?

    December 9th, 2010 @ 04:34 AM

  8. Mike Dvorkin
    Mike Dvorkin
    • Tag changed from activemodel, to_xml to activemodel, patch, to_xml

    Here's the patch ;-)

    December 9th, 2010 @ 04:43 AM

  9. Repository
    Repository
    • State changed from new to resolved

    (from [f572a02b94ce85bb8db64e838aa159cf3ef4b1fa]) Take into account current time zone when serializing datetime values [#6096 to_xml datetime format regression state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    https://github.com/rails/rails/commit/f572a02b94ce85bb8db64e838aa15...

    December 9th, 2010 @ 08:07 AM

  10. Repository
  11. bingbing