Skip to content

Do not read a longer zone name as a military zone in Time.rfc2822 - #92

Open
bardrop wants to merge 2 commits into
ruby:masterfrom
bardrop-oss:fix-rfc2822-zone-suffix
Open

bardrop wants to merge 2 commits into
ruby:masterfrom
bardrop-oss:fix-rfc2822-zone-suffix

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026 •

Copy link
Copy Markdown

Time.rfc2822 has no right boundary after the zone, so a longer zone name matches its first letter as a military zone:

require 'time'
Time.rfc2822("Sat, 01 Jan 2000 12:00:00 CET").utc_offset  # => 10800  ("C", +03:00; CET is +01:00)
Time.rfc2822("Sat, 01 Jan 2000 12:00:00 HST").utc_offset  # => 28800  ("H", +08:00; HST is -10:00)
Time.rfc2822("Sat, 01 Jan 2000 12:00:00 IST").utc_offset  # => 32400  ("I")
Time.rfc2822("Sat, 01 Jan 2000 12:00:00 JST")             # ArgumentError (J is not a military zone)

So these strings are parsed with a wrong offset (off by up to 18 hours) instead of raising ArgumentError like JST does, and a caller that rescues and falls back to Time.parse never gets the chance.

The fix adds \b after the zone. Comments and other separators are still accepted (-0330 (Newfoundland Time), GMT(comment), a trailing ,). UTC is added to the list because it used to match only as UT followed by C; the result is the same as before.

Tests: new test_rfc2822_zone_end fails before and passes after; rake test passes (54 tests). I also ran 30k generated dates (numeric, named and military zones, with and without comments/trailing text) through the old and new code: the only changes are inputs where the zone is directly followed by a letter or digit, which now raise.

The zone part of the pattern had no right boundary, so "CET" matched the
military zone "C" (+03:00), "HST" matched "H" (+08:00), "IST" matched
"I" (+09:00) and so on. The date was parsed with a wrong offset instead
of raising ArgumentError, as it already does for "JST".

Require the zone not to be followed by a letter or a digit. Comments and
other separators after the zone are still accepted. Also accept "UTC",
which used to match only as "UT" followed by "C".
Comment thread lib/time.rb Outdated
Comment on lines +527 to +528
UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])
(?![a-z\d])/ix =~ date

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])
(?![a-z\d])/ix =~ date
UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])\b/ix =~ date

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied in 0f22c53. rake test passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ Proxy