diff --git a/lib/time.rb b/lib/time.rb index 679cc82..c4abf93 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -524,8 +524,10 @@ def rfc2822(date) :\s*(\d{2}) (?:\s*:\s*(\d\d))?\s+ ([+-]\d{4}| - UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date + UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])\b/ix =~ date # Since RFC 2822 permit comments, the regexp has no right anchor. + # The zone must still end there, so that "CET" is not read as the + # military zone "C". day = $1.to_i mon = MonthValue[$2.upcase] year = $3.to_i diff --git a/test/test_time.rb b/test/test_time.rb index 4054bc4..447fac0 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -62,6 +62,24 @@ def test_rfc2822 assert_equal(true, t.utc?) end + def test_rfc2822_zone_end + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 UTC") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + assert_equal(true, t.utc?) + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 GMT(comment)") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 Z") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + + # A longer zone name is not a military zone with extra letters. + %w[CET CEST HST IST BST MSK EET UTX GMTX ESTX].each do |zone| + assert_raise(ArgumentError, zone) { + Time.rfc2822("Sat, 01 Jan 2000 12:00:00 #{zone}") + } + end + assert_raise(ArgumentError) { Time.rfc2822("Sat, 01 Jan 2000 12:00:00 +09001") } + end + def test_rfc2822_nonlinear pre = ->(n) {"0 Feb 00 00 :00" + " " * n} assert_linear_performance([100, 500, 5000, 50_000], pre: pre) do |s|