1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Make Time.parse respect timezone offset seconds

DateTime.parse handles them correctly, and DateTime.parse.to_time
results in the correct time.  Time.parse doesn't handle them
correctly because Time.zone_offset uses a different regexp that
only considers hours and minutes, not seconds.
[ruby-core:83400] [Bug #14034]

From: Jeremy Evans <code@jeremyevans.net>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-10-20 01:03:18 +00:00
parent f16f0441d6
commit 90048176cc
2 changed files with 9 additions and 2 deletions

View file

@ -134,8 +134,8 @@ class Time
def zone_offset(zone, year=self.now.year) def zone_offset(zone, year=self.now.year)
off = nil off = nil
zone = zone.upcase zone = zone.upcase
if /\A([+-])(\d\d):?(\d\d)\z/ =~ zone if /\A([+-])(\d\d)(:?)(\d\d)(?:\3(\d\d))?\z/ =~ zone
off = ($1 == '-' ? -1 : 1) * ($2.to_i * 60 + $3.to_i) * 60 off = ($1 == '-' ? -1 : 1) * (($2.to_i * 60 + $4.to_i) * 60 + $5.to_i)
elsif /\A[+-]\d\d\z/ =~ zone elsif /\A[+-]\d\d\z/ =~ zone
off = zone.to_i * 3600 off = zone.to_i * 3600
elsif ZoneOffset.include?(zone) elsif ZoneOffset.include?(zone)

View file

@ -343,6 +343,13 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc:
Time.parse("2000-01-01T00:00:00+11:00", nil)) Time.parse("2000-01-01T00:00:00+11:00", nil))
end end
def test_parse_offset_hour_minute_second
t = Time.at(-100000000000).utc
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00"))
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00"))
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00:00"))
end
def test_parse_leap_second def test_parse_leap_second
t = Time.utc(1998,12,31,23,59,59) t = Time.utc(1998,12,31,23,59,59)
assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998")) assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))