diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 93f8f98..b06cc85 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -566,6 +566,11 @@ date_zone_to_diff_frac(VALUE str, int *fracp, long *whole) min = ruby_scan_digits(&s[2 - l % 2], 2, 10, &n, &ov); if (l >= 5) sec = ruby_scan_digits(&s[4 - l % 2], 2, 10, &n, &ov); + + if (out_of_range(hour, 0, 23) || + out_of_range(min, 0, 59) || + out_of_range(sec, 0, 59)) + return Qnil; } sec += min * 60 + hour * 3600; if (sign) sec = -sec; diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 0bf0a96..26ed45f 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -218,8 +218,8 @@ def test__parse # rfc 2822 [['Sun, 22 Aug 1999 00:45:29 -0400',false],[1999,8,22,0,45,29,'-0400',-4*3600,0], __LINE__], - [['Sun, 22 Aug 1999 00:45:29 -9959',false],[1999,8,22,0,45,29,'-9959',-(99*3600+59*60),0], __LINE__], - [['Sun, 22 Aug 1999 00:45:29 +9959',false],[1999,8,22,0,45,29,'+9959',+(99*3600+59*60),0], __LINE__], + [['Sun, 22 Aug 1999 00:45:29 -9959',false],[1999,8,22,0,45,29,'-9959',nil,0], __LINE__], + [['Sun, 22 Aug 1999 00:45:29 +9959',false],[1999,8,22,0,45,29,'+9959',nil,0], __LINE__], [['Sun, 22 Aug 05 00:45:29 -0400',true],[2005,8,22,0,45,29,'-0400',-4*3600,0], __LINE__], [['Sun, 22 Aug 49 00:45:29 -0400',true],[2049,8,22,0,45,29,'-0400',-4*3600,0], __LINE__], [['Sun, 22 Aug 1999 00:45:29 GMT',false],[1999,8,22,0,45,29,'GMT',0,0], __LINE__], @@ -666,6 +666,26 @@ def test__parse_invalid_offset assert_equal(['+12:60', nil], h.values_at(:zone, :offset)) end + def test__parse_invalid_offset_without_colon + h = DateTime._parse('2001-02-03T04:05:06+2400') + assert_equal(['+2400', nil], h.values_at(:zone, :offset)) + h = DateTime._parse('2001-02-03T04:05:06+2500') + assert_equal(['+2500', nil], h.values_at(:zone, :offset)) + h = DateTime._parse('2001-02-03T04:05:06+1260') + assert_equal(['+1260', nil], h.values_at(:zone, :offset)) + h = DateTime._parse('2001-02-03T04:05:06+125960') + assert_equal(['+125960', nil], h.values_at(:zone, :offset)) + h = DateTime._parse('2001-02-03T04:05:06-2500') + assert_equal(['-2500', nil], h.values_at(:zone, :offset)) + end + + def test_parse_invalid_offset_without_colon + d = DateTime.parse('2001-02-03T04:05:06+1260') + assert_equal(0, d.offset) + d = DateTime.parse('2001-02-03T04:05:06+0900') + assert_equal(Rational(9, 24), d.offset) + end + def test__parse_too_long_year omit 'transient' if RUBY_ENGINE == 'truffleruby' diff --git a/test/date/test_date_strftime.rb b/test/date/test_date_strftime.rb index 0214192..65e1114 100644 --- a/test/date/test_date_strftime.rb +++ b/test/date/test_date_strftime.rb @@ -186,15 +186,14 @@ def test_strftime__offset (-24..24).collect{|x| '%+.2d' % x}.each do |hh| %w(00 30).each do |mm| r = hh + mm - next if r.end_with?('2430') + next if r.end_with?('2400', '2430') d = DateTime.parse(s + hh + mm) assert_equal(r, d.strftime('%z')) end end - %w[+2430 -2430].each do |r| - assert_warning(/invalid offset/) do - DateTime.parse(s + r) - end + %w[+2400 -2400 +2430 -2430].each do |r| + d = DateTime.parse(s + r) + assert_equal(0, d.offset) end end