Repository navigation
Conversation
…pinghub#375) The numeric offset patterns in timezone_info_list only matched at the very end of the string ("(.)%s$"), so an offset followed by more text (e.g. the year in Twitter's "Fri Jan 26 16:32:21 +0000 2018") was never extracted, and the whole string failed to parse. Relax the trailing anchor to match the abbreviation group's style, while excluding a following digit or colon so a short offset like "+5" can't partial-match a longer one like "+5:30". Co-Authored-By: Claude Sonnet 5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1599 +/- ##
=======================================
Coverage 99.22% 99.22%
=======================================
Files 239 239
Lines 4256 4271 +15
=======================================
+ Hits 4223 4238 +15
Misses 33 33 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
serhii73
left a comment
There was a problem hiding this comment.
Thanks for looking into this! Two problems before it can go in:
- CI fails on three legs in
test_parse_run_of_separators_then_skip_words_is_linear[bang](about 1.5 s), also after the re-run. The template no longer ends in$, so_build_tz_regexesstops compiling this family withregex.REVERSE, and_find_tznow runs an overlappedfinditerover the whole string. - An offset can now match in the middle of the text.
dateparser.parse("01.05.2020 -04.30")gives 2020-01-05 04:30 on master; here-04is taken as UTC-4 and what is left,01.05.2020 30, does not parse. The trailing group also removes the character after the offset, so16:32:21+0000 2018becomes16:32:212018.
Could you keep the $ anchor and allow only a 4-digit year after the offset, the layout from the issue? Tests for the cases above would help.
fix The previous commit relaxed the UTC-offset regexes to match anywhere in the string, not just at the end. A maintainer review on scrapinghub#1599 found two problems with that: - It dropped the "$"-anchored fast path (regex.REVERSE), so every parse call without a trailing offset fell back to a slow, unanchored scan across the whole offset table (~28% slower per CodSpeed). - The loosened boundary let a short offset match in the middle of unrelated text (e.g. "-04" in "01.05.2020 -04.30"), and dropped the separator between digits run together without a preceding space (e.g. "16:32:21+0000 2018" became "16:32:212018"). Revert dateparser/timezones.py to its original anchored-at-the-end patterns, and instead add a small, separate fallback in timezone_parser.py: only once the normal (fast) lookup fails, a single cheap, non-alternating regex checks whether the string ends in a numeric offset immediately followed by a 4-digit year (the Twitter layout from the issue); if so, the year is set aside, the normal fast lookup runs again on the rest, and the year is reattached. This keeps the common case (no offset, or an offset already at the end) exactly as fast as before. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
Thanks for the detailed review, @serhii73 — both issues are fixed in 11dcfd2.
Verified locally: full test suite passes (25730 tests), |
Fixes #375
Problem
dateparser.parse('Fri Jan 26 16:32:21 +0000 2018')returnsNone— this is the exact format Twitter's API used in thecreated_atfield. The offset sits before trailing text (the year) instead of at the very end of the string.The numeric UTC-offset patterns in
timezone_info_list(dateparser/timezones.py) only matched at the end of the string ("(.)%s$"), so an offset followed by more text was never extracted bypop_tz_offset_from_string, and the whole string then failed to parse.Change
Relax the trailing anchor to match the style already used by the abbreviation group (
"(\W|\d|_)%s($|\W)"-ish), so the offset can be found anywhere in the string, not just at the end:The
[^:\d]boundary (instead of a bare\W) is needed to avoid an ambiguity: without it, a shorter generated offset like+5(an alternate form ofUTC+05:00) would partial-match inside a longer one like+5:30, since:counts as\W. Excluding a following digit or colon forces the longer, more specific pattern to match instead.Testing
tests/test_timezone_parser.py(offset extraction and string-stripping) andtests/test_date_parser.py(end-to-enddateparser.parse) covering the Twitter format and the+5/+5:30ambiguity.ruff check/ruff format --checkclean.🤖 Generated with Claude Code