Skip to content

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

Open
Sanjays2402 wants to merge 1 commit into
scrapinghub:masterfrom
Sanjays2402:fix/tz-aware-relative-base-time-only
Open

Fix parsing time-only strings with a timezone-aware RELATIVE_BASE#1362
Sanjays2402 wants to merge 1 commit into
scrapinghub:masterfrom
Sanjays2402:fix/tz-aware-relative-base-time-only

Conversation

@Sanjays2402

Copy link
Copy Markdown

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 97.10%. Comparing base (b1f603e) to head (de1413a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1362   +/-   ##
=======================================
  Coverage   97.10%   97.10%           
=======================================
  Files         236      236           
  Lines        3008     3008           
=======================================
  Hits         2921     2921           
  Misses         87       87           

☔ 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.

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

2 participants