Skip to content

Parse UTC offsets followed by trailing text, e.g. Twitter dates - #1599

Open
yamilmaud wants to merge 3 commits into
scrapinghub:masterfrom
yamilmaud:fix-issue-375-twitter-date-offset
Open

yamilmaud wants to merge 3 commits into
scrapinghub:masterfrom
yamilmaud:fix-issue-375-twitter-date-offset

Conversation

@yamilmaud

Copy link
Copy Markdown
Contributor

Fixes #375

Problem

dateparser.parse('Fri Jan 26 16:32:21 +0000 2018') returns None — this is the exact format Twitter's API used in the created_at field. 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 by pop_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:

"regex_patterns": [r"(\W|\d|_)%s($|[^:\d])"],

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 of UTC+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

  • Added regression cases to tests/test_timezone_parser.py (offset extraction and string-stripping) and tests/test_date_parser.py (end-to-end dateparser.parse) covering the Twitter format and the +5 / +5:30 ambiguity.
  • Full test suite passes.
  • ruff check / ruff format --check clean.

🤖 Generated with Claude Code

…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]>
@codspeed

codspeed Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing yamilmaud:fix-issue-375-twitter-date-offset (11dcfd2) with master (ce8c439)

Open in CodSpeed

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.22%. Comparing base (ce8c439) to head (11dcfd2).

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

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_regexes stops compiling this family with regex.REVERSE, and _find_tz now runs an overlapped finditer over 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 -04 is 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, so 16:32:21+0000 2018 becomes 16: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]>
@yamilmaud

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review, @serhii73 — both issues are fixed in 11dcfd2.

  • Reverted dateparser/timezones.py entirely back to its original, $-anchored patterns, so the regex.REVERSE fast path is untouched for every call that doesn't need this.
  • Instead, added a small, separate fallback in timezone_parser.py: only once the normal (fast) lookup finds nothing, a single cheap, non-alternating regex checks whether the string ends in a numeric offset immediately followed by a 4-digit year (the exact layout from the issue). If so, the year is set aside, the normal fast lookup runs again on the rest (now truly offset-at-the-end), and the year is reattached.
  • 01.05.2020 -04.30 now parses the same as on master.
  • 16:32:21+0000 2018 no longer loses the separating space.

Verified locally: full test suite passes (25730 tests), ruff check/ruff format --check/mypy clean, and timeit of dateparser.parse() over the benchmark suite's own date strings is now within normal run-to-run noise vs master (previously ~30% slower).

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.

Unable to parse date with UTC offset

2 participants