Skip to content

Preserve time zone offset when deserializing times #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/psych/scalar_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def parse_time string
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
31 changes: 31 additions & 0 deletions test/psych/test_date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,41 @@ def test_negative_year
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
Expand Down