Take into account current time zone when serializing datetime values [#6096 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
07b0e59988
commit
f572a02b94
|
@ -17,6 +17,7 @@ module ActiveModel
|
|||
|
||||
def initialize(name, serializable, raw_value=nil)
|
||||
@name, @serializable = name, serializable
|
||||
raw_value = raw_value.in_time_zone if raw_value.respond_to?(:in_time_zone)
|
||||
@value = raw_value || @serializable.send(name)
|
||||
@type = compute_type
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ require 'models/post'
|
|||
require 'models/author'
|
||||
require 'models/comment'
|
||||
require 'models/company_in_module'
|
||||
require 'models/toy'
|
||||
|
||||
class XmlSerializationTest < ActiveRecord::TestCase
|
||||
def test_should_serialize_default_root
|
||||
|
@ -83,6 +84,26 @@ class DefaultXmlSerializationTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
class DefaultXmlSerializationTimezoneTest < ActiveRecord::TestCase
|
||||
def test_should_serialize_datetime_with_timezone
|
||||
timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
|
||||
|
||||
toy = Toy.create(:name => 'Mickey', :updated_at => Time.utc(2006, 8, 1))
|
||||
assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
|
||||
ensure
|
||||
Time.zone = timezone
|
||||
end
|
||||
|
||||
def test_should_serialize_datetime_with_timezone_reloaded
|
||||
timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
|
||||
|
||||
toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload
|
||||
assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
|
||||
ensure
|
||||
Time.zone = timezone
|
||||
end
|
||||
end
|
||||
|
||||
class NilXmlSerializationTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
@xml = Contact.new.to_xml(:root => 'xml_contact')
|
||||
|
|
Loading…
Reference in New Issue