diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2017-05-22 08:59:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-22 08:59:55 -0700 |
commit | e8cfcfedaa27c9a091491b2cff46a167f7bc4305 (patch) | |
tree | a2e4dfeb24cdcb8308314c30a9596b331293ea1b | |
parent | c91c4ad74ff768255ed83b07eff5d5541adc4909 (diff) | |
parent | d2db988626a6bab441b6609a10d31e39b09fc784 (diff) | |
download | psych-e8cfcfedaa27c9a091491b2cff46a167f7bc4305.tar.gz |
Merge pull request #316 from stomar/timezone-offset
Preserve time zone offset when deserializing times
-rw-r--r-- | lib/psych/scalar_scanner.rb | 2 | ||||
-rw-r--r-- | test/psych/test_date_time.rb | 31 |
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index 449efa4..67c0cc3 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -143,7 +143,7 @@ module Psych offset += ((tz[1] || 0) * 60) end - klass.at((time - offset).to_i, us) + klass.new(yy, m, dd, hh, mm, ss+us/(1_000_000r), offset) end end end diff --git a/test/psych/test_date_time.rb b/test/psych/test_date_time.rb index 443669d..3c8b436 100644 --- a/test/psych/test_date_time.rb +++ b/test/psych/test_date_time.rb @@ -9,10 +9,41 @@ module Psych assert_cycle time end + def test_usec + time = Time.utc(2017, 4, 13, 12, 0, 0, 5) + assert_cycle time + end + + def test_non_utc + time = Time.new(2017, 4, 13, 12, 0, 0.5, "+09:00") + assert_cycle time + end + + def test_timezone_offset + times = [Time.new(2017, 4, 13, 12, 0, 0, "+09:00"), + Time.new(2017, 4, 13, 12, 0, 0, "-05:00")] + cycled = Psych::load(Psych.dump times) + assert_match(/12:00:00 \+0900/, cycled.first.to_s) + assert_match(/12:00:00 -0500/, cycled.last.to_s) + end + def test_new_datetime assert_cycle DateTime.new end + def test_datetime_non_utc + dt = DateTime.new(2017, 4, 13, 12, 0, 0.5, "+09:00") + assert_cycle dt + end + + def test_datetime_timezone_offset + times = [DateTime.new(2017, 4, 13, 12, 0, 0, "+09:00"), + DateTime.new(2017, 4, 13, 12, 0, 0, "-05:00")] + cycled = Psych::load(Psych.dump times) + assert_match(/12:00:00\+09:00/, cycled.first.to_s) + assert_match(/12:00:00-05:00/, cycled.last.to_s) + end + def test_invalid_date assert_cycle "2013-10-31T10:40:07-000000000000033" end |