Do not read the day as a two-digit year after the year position - #1358
Merged
serhii73 merged 7 commits intoJul 29, 2026
Conversation
- Add month locative/dative forms (lednu, únoru, březnu, dubnu, květnu, červnu, červenci, srpnu, říjnu, prosinci) for dates like "v lednu 2023" - Add July abbreviation "črv" - Fix "za měsíc/týden/rok" not parsing: add accusative simplification patterns (previous patterns only matched instrumental -em forms) - Add tests covering the new expressions Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…scrapinghub#519) In year-first date orders (e.g. YMD, used by the Japanese locale), parse_number tried the %y directive on every two-digit number, so in "4月20日 18:10" the day 20 was consumed as the year 2020 and the day was silently filled from the current date. If a component that the date order places after the year has already been found, the year position in the date string has already been passed, so a two-digit number is now read as a year only if it cannot be any other component (e.g. 99 in "4-99").
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1358 +/- ##
==========================================
+ Coverage 97.07% 97.08% +0.01%
==========================================
Files 236 236
Lines 2975 2987 +12
==========================================
+ Hits 2888 2900 +12
Misses 87 87 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
AdrianAtZyte
approved these changes
Jul 28, 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.
Fixes #519 (
Close #519is in the commit message).Root cause
_parser.parse_number()tries the numeric directives in the resolvedDATE_ORDER. For year-first orders (e.g.YMD, which the Japanese locale uses), the%ydirective is tried first for every token, so any two-digit number that appears after the month is consumed as a year. In4月20日 18:10the20became the year 2020, the day was never parsed, and it was silently filled from the current date:The bug is locale-independent — the same happens for
dateparser.parse("4-20 18:10", settings={"DATE_ORDER": "YMD"}).Fix
If a component that the date order places after the year has already been found, the year position in the date string has already been passed, so a two-digit number cannot be the year anymore.
parse_number()now defers the%ydirective in that case, reading the number as a two-digit year only if it cannot be any other component (so4-99still parses as April 1999, since 99 can be neither month nor day).Year-last orders (
DMY,MDY— the majority of locales) have no components after the year, so their behavior is unchanged by construction. Four-digit years (%Y) are never deferred.Regression tests
test_two_digit_day_is_not_confused_with_yearintests/test_date_parser.pycovers:4月20日 18:10and3月8日(ja) → day is the day, year comes from the reference date4-20 18:10with explicitDATE_ORDER: YMD→ same, locale-independent4-99(YMD) → still April 1999 (two-digit year as last resort)2020年4月20日 18:10and19年4月20日→ explicit years keep workingNo existing tests were modified; the full suite passes (24103 passed, 18 skipped, 1 xfailed) and
pre-commit(ruff, ruff-format) is clean.