Fix parsing time-only strings with a timezone-aware RELATIVE_BASE - #1362
Open
Sanjays2402 wants to merge 1 commit into
Open
Fix parsing time-only strings with a timezone-aware RELATIVE_BASE#1362Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
AdrianAtZyte
approved these changes
Aug 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1213
When
RELATIVE_BASEis offset-aware,_correct_for_time_framelocalizesdateobjto UTC so it can be compared withself.now, and the followingtz.utcoffset(dateobj)then gets an aware datetime, which pytz rejects withValueError: Not naive datetime. That error isn't one of the two pytz exceptions caught there, so parsing aborts andparse("3pm", ...)returnsNone. Passing a naive copy toutcoffset()fixes it; a naiveRELATIVE_BASEis unaffected.Regression test added in
tests/test_date_parser.py(bothPREFER_DATES_FROMdirections); 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.