Skip to content

Treat out-of-range compact zone offsets like the colon form in _parse - #214

Open
yppp wants to merge 1 commit into
ruby:masterfrom
yppp:warn-invalid-offset-in-parse
Open

yppp wants to merge 1 commit into
ruby:masterfrom
yppp:warn-invalid-offset-in-parse

Conversation

@yppp

@yppp yppp commented Nov 29, 2026 •

Copy link
Copy Markdown

As discussed, this now takes the narrower approach: no warning, and out-of-range compact zone offsets are treated exactly like the colon form.

date_zone_to_diff_frac range-checks the colon form (+25:00, +12:60) and returns Qnil so the offset is not set, but the compact form had no range checks at all:

  • DateTime._parse('2001-02-03T04:05:06+2500') reported offset: 90000
  • DateTime.parse('2001-02-03T04:05:06+1260') applied a wrong 13-hour offset

The compact branch now applies the same hour/minute/second range checks, so those offsets are not set (offset: nil from _parse, offset 0 from parse), matching +25:00 / +12:60.

Existing tests changed, as suggested:

  • test__parse: the RFC 2822 -9959/+9959 cases now expect no offset (the zone string is still reported).
  • test_strftime__offset: +2400/-2400 no longer round-trip; they are grouped with +2430/-2430 as parsing with offset 0. The old assert_warning(/invalid offset/) expectation for +2430 went away with this: that warning came from the constructor path when _parse still returned the out-of-range offset, and the colon form never warned here.

New tests: test__parse_invalid_offset_without_colon and test_parse_invalid_offset_without_colon. The full test suite passes.

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

parse is often called with untrusted data, so I'm not sure we want to issue a warning in that case.

However, we should probably treat +2500 like +25:00 and not set the offset in that case. Can you modify the PR to only do that?

@yppp

yppp commented Nov 30, 2026

Copy link
Copy Markdown
Author

Thanks for the review. I started making this change and found two existing tests that rely on the compact form accepting large hours:

  • test__parse (the RFC 2822 cases) expects -9959/+9959 to produce an offset of ±359940 seconds
  • test_strftime__offset expects +2400/-2400 to round-trip through DateTime.parse

Making the compact form reject out-of-range values the same way the colon form does (hour > 23, minute/second > 59) would change those cases to have no offset. Should I update those tests as part of this change, or did you have a narrower rule in mind for the compact form?

@jeremyevans

Copy link
Copy Markdown
Contributor

I would change both of the tests.

date_zone_to_diff_frac validated the hour/minute/second ranges for
the colon form of a zone offset (+25:00, +12:60), returning Qnil so
the offset is not set, but the compact form (+2500, +1260) had no
range checks at all: _parse returned an offset of 90000 seconds for
+2500, and DateTime.parse applied a wrong 13-hour offset for +1260.

Add the same range checks to the compact branch, so an out-of-range
compact offset does not set the offset, matching the colon form.

Update the two existing tests that expected the permissive behavior:
the RFC 2822 -9959/+9959 cases in test__parse now expect no offset,
and test_strftime__offset no longer round-trips +2400/-2400 (they
now parse with offset 0, like +2430/-2430 and the colon forms).
@yppp
yppp force-pushed the warn-invalid-offset-in-parse branch from 2ab4e23 to cbeaeeb Compare November 30, 2026 10:02
@yppp yppp changed the title Warn when a parsed zone offset is out of range and ignored Treat out-of-range compact zone offsets like the colon form in _parse Nov 30, 2026
@yppp

yppp commented Nov 30, 2026

Copy link
Copy Markdown
Author

Thank you — I've updated the PR along those lines, and changed both tests.

  • The compact branch of date_zone_to_diff_frac now has the same range checks as the colon branch, so +2500 is treated like +25:00 and the offset is not set. The warning from the previous version is gone.
  • test__parse: the -9959/+9959 cases now expect offset: nil.
  • test_strftime__offset: +2400/-2400 no longer round-trip and are grouped with +2430/-2430 as parsing with offset 0. One knock-on effect: the assert_warning(/invalid offset/) expectation for +2430 is removed, since that warning came from the constructor path when _parse still returned the out-of-range offset; the colon form (+24:30) does not warn in this test either.

The full suite 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