Skip to content

Fix parsing time-only strings with a timezone-aware RELATIVE_BASE - #1362

Merged
AdrianAtZyte merged 3 commits into
scrapinghub:masterfrom
Sanjays2402:fix/tz-aware-relative-base-time-only
Nov 5, 2026
Merged

AdrianAtZyte merged 3 commits into
scrapinghub:masterfrom
Sanjays2402:fix/tz-aware-relative-base-time-only

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #1213

When RELATIVE_BASE is offset-aware, _correct_for_time_frame localizes dateobj to UTC so it can be compared with self.now, and the following tz.utcoffset(dateobj) then gets an aware datetime, which pytz rejects with ValueError: Not naive datetime. That error isn't one of the two pytz exceptions caught there, so parsing aborts and parse("3pm", ...) returns None. Passing a naive copy to utcoffset() fixes it; a naive RELATIVE_BASE is unaffected.

Regression test added in tests/test_date_parser.py (both PREFER_DATES_FROM directions); it fails on master and passes with the fix.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

When RELATIVE_BASE is offset-aware, _correct_for_time_frame localizes
dateobj to UTC so it can be compared with self.now. The subsequent
tz.utcoffset(dateobj) call then receives an aware datetime, which pytz
rejects with "Not naive datetime (tzinfo is already set)". The exception
is not one of the two caught pytz errors, so parsing aborts and
dateparser.parse() returns None for inputs like "3pm".

Pass a naive copy to utcoffset() so the offset lookup works regardless of
whether dateobj was localized. Behaviour with a naive RELATIVE_BASE is
unchanged.

Adds a regression test in tests/test_date_parser.py covering both
PREFER_DATES_FROM directions with an offset-aware RELATIVE_BASE.

Closes scrapinghub#1213
@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.23%. Comparing base (bf51279) to head (83eae00).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1362   +/-   ##
=======================================
  Coverage   99.23%   99.23%           
=======================================
  Files         239      239           
  Lines        4203     4203           
=======================================
  Hits         4171     4171           
  Misses         32       32           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Checked the diff, the linked issue (#1213) and CI (15/15 green). I reproduced the bug on current master (with an offset-aware RELATIVE_BASE, parse("3pm", ...) returns None) and verified that the fix on this branch resolves to the right dates: with an aware America/Los_Angeles base, 3pm gives the previous day (base 12:00) or the same day (base 18:00) for PREFER_DATES_FROM="past", and the same day or the next day for "future"; it also works when TIMEZONE differs from the base's zone. Approving the change itself.

Non-urgent notes:

  • The branch is based on 1.4.2 and conflicts with current master. The block has changed (dateparser/parser.py around lines 805-821: tz_offset = tz.utcoffset(dateobj) or tz_offset inside if self.settings.RELATIVE_BASE:), so the naive copy has to be applied to that call during the rebase, keeping the or tz_offset fallback because utcoffset() can return None.
  • The new test only asserts that parsing succeeds and that the period is day. Asserting the resulting datetime (for example with then_date_obj_exactly_is) and adding a base later than 3pm would catch a wrong day.
  • On a DST fall-back day an ambiguous time (for example 1:30am with an aware LA base on 2020-11-01) now raises pytz.AmbiguousTimeError instead of returning None. A naive RELATIVE_BASE already does the same on master, so this is not new behavior for the function, but catching pytz.AmbiguousTimeError next to NonExistentTimeError would be a cheap hardening.

Resolve the conflicts in dateparser/parser.py and tests/test_date_parser.py.
Master restructured the time-only block in _correct_for_time_frame, so the
naive copy passed to utcoffset() is applied to master's version of that call,
keeping its `or tz_offset` fallback. Both imports (pytest and pytz) are kept in
the tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@codspeed

codspeed Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing Sanjays2402:fix/tz-aware-relative-base-time-only (83eae00) with master (bf51279)

Open in CodSpeed

Master type-checks the tests with mypy in strict mode, so the new test needs
parameter and return annotations.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@AdrianAtZyte
AdrianAtZyte merged commit 679bae6 into scrapinghub:master Nov 5, 2026
19 checks passed
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.

Parsing "3pm" fails if RELATIVE_BASE is timezone-aware

3 participants