Skip to content

Commit

Permalink
Hash.from_xml: datetime xml types overflow to Ruby DateTime class whe…
Browse files Browse the repository at this point in the history
…n out of range of Time. Adding tests for utc offsets
  • Loading branch information
gbuesing committed May 18, 2008
1 parent cde9c09 commit cee9297
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,3 +1,5 @@
* Hash.from_xml: datetime xml types overflow to Ruby DateTime class when out of range of Time. Adding tests for utc offsets [Geoff Buesing]

* TimeWithZone #+ and #- : ensure overflow to DateTime with Numeric arg [Geoff Buesing]

* Time#to_json: don't convert to utc before encoding. References #175 [Geoff Buesing]
Expand Down
Expand Up @@ -70,7 +70,7 @@ module Conversions
XML_PARSING = {
"symbol" => Proc.new { |symbol| symbol.to_sym },
"date" => Proc.new { |date| ::Date.parse(date) },
"datetime" => Proc.new { |time| ::Time.parse(time).utc },
"datetime" => Proc.new { |time| ::Time.parse(time).utc rescue ::DateTime.parse(time).utc },
"integer" => Proc.new { |integer| integer.to_i },
"float" => Proc.new { |float| float.to_f },
"decimal" => Proc.new { |number| BigDecimal(number) },
Expand Down
38 changes: 38 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -733,6 +733,44 @@ def test_roundtrip_to_xml_from_xml

assert_equal hash, Hash.from_xml(hash.to_xml(@xml_options))['person']
end

def test_datetime_xml_type_with_utc_time
alert_xml = <<-XML
<alert>
<alert_at type="datetime">2008-02-10T15:30:45Z</alert_at>
</alert>
XML
alert_at = Hash.from_xml(alert_xml)['alert']['alert_at']
assert alert_at.utc?
assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
end

def test_datetime_xml_type_with_non_utc_time
alert_xml = <<-XML
<alert>
<alert_at type="datetime">2008-02-10T10:30:45-05:00</alert_at>
</alert>
XML
alert_at = Hash.from_xml(alert_xml)['alert']['alert_at']
assert alert_at.utc?
assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
end

def test_datetime_xml_type_with_far_future_date
alert_xml = <<-XML
<alert>
<alert_at type="datetime">2050-02-10T15:30:45Z</alert_at>
</alert>
XML
alert_at = Hash.from_xml(alert_xml)['alert']['alert_at']
assert alert_at.utc?
assert_equal 2050, alert_at.year
assert_equal 2, alert_at.month
assert_equal 10, alert_at.day
assert_equal 15, alert_at.hour
assert_equal 30, alert_at.min
assert_equal 45, alert_at.sec
end
end

class QueryTest < Test::Unit::TestCase
Expand Down

0 comments on commit cee9297

Please sign in to comment.