Skip to content

Use the reference year when choosing a month-end day - #1389

Open
agammann wants to merge 4 commits into
scrapinghub:masterfrom
agammann:fix-yearless-month-end
Open

agammann wants to merge 4 commits into
scrapinghub:masterfrom
agammann:fix-yearless-month-end

Conversation

@agammann

@agammann agammann commented Sep 20, 2026 •

Copy link
Copy Markdown

Custom formats that omit the year can choose February's last day using strptime's default year. With a leap-year reference date, PREFER_DAY_OF_MONTH='last' then returns February 28 instead of February 29. The current day and month preferences can also mix RELATIVE_BASE with today's date.

Use the same reference date for the year, month, and day defaults. Apply its year before parsing yearless day formats and filling missing components, and skip invalid leap-day dates instead of raising ValueError. Add coverage for first/current/last preferences, a year-only format, leap and non-leap reference years, and parsing without RELATIVE_BASE.

Rebased onto current master (4c4c5e5) and validated head 91c931a on Ubuntu with Python 3.14.4:

  • Full suite: 25,131 passed, 19 skipped, 4 xfailed, 508 subtests.
  • Focused parser/date/strptime/API suite: 1,329 passed, 4 xfailed, 477 subtests.
  • Four focused regression cases were previously confirmed to fail on unmodified master and pass with this patch.
  • Ruff lint and format checks and git diff --check pass.

The new-head Build, CIFuzz, and CodSpeed workflows await maintainer approval.

@codspeed

codspeed Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 10 untouched benchmarks


Comparing agammann:fix-yearless-month-end (91c931a) with master (4c4c5e5)

Open in CodSpeed

@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.02%. Comparing base (4c4c5e5) to head (91c931a).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
dateparser/date.py 76.92% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1389      +/-   ##
==========================================
- Coverage   99.09%   99.02%   -0.08%     
==========================================
  Files         239      239              
  Lines        3988     3991       +3     
==========================================
  Hits         3952     3952              
- Misses         36       39       +3     

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

Code review (claude code-review, at c627f48). I'm not adding my approval yet:

  1. Conflicts with master. GitHub reports the PR as conflicting. The hunk in parse_with_formats rewrites lines master has since changed: master uses now = settings.RELATIVE_BASE or _now(settings) and a "year" in _missing check, not the "%y" in date_format or "%Y" in date_format string check. Resolving the conflict mechanically would bring the old check and the UTC now back. On the new base the fix would be if "year" in _missing: date_obj = date_obj.replace(year=now.year) before the day/month fill.
  2. Related spots left out. set_correct_day_from_settings and set_correct_month_from_settings still default to datetime.now(), so with RELATIVE_BASE set and PREFER_DAY_OF_MONTH='current' or PREFER_MONTH_OF_YEAR='current', the reference year is mixed with today's day or month.
  3. Leap day. For %d %B with 29 February and a non-leap reference year, date_obj.replace(year=now.year) raises an uncaught ValueError. This already happens today, but since the year replacement is moving, a guard and a test would fit here.
  4. Tests. The test covers only %B with PREFER_DAY_OF_MONTH='last'. Please add the first/current preferences, a format without month and day, and a run without RELATIVE_BASE.

@agammann
agammann force-pushed the fix-yearless-month-end branch from 03963a8 to 91c931a Compare October 23, 2026 06:32
@agammann

Copy link
Copy Markdown
Author

@serhii73 Thanks for the detailed review. I rebased the final patch onto current master at 91c931a; GitHub now reports no conflicts. The change uses the parsed _missing year check, takes the current day and month from the same reference date, guards invalid leap-day dates, and covers first/current/last preferences, a year-only format, leap and non-leap years, and parsing without RELATIVE_BASE. On Ubuntu with Python 3.14.4, the full suite passed (25,131 passed, 19 skipped, 4 xfailed), along with Ruff lint and format checks. The new-head workflows are awaiting maintainer approval.

@agammann
agammann requested a review from serhii73 October 23, 2026 06:33
Avoid requiring the optional US/Eastern backward-compatibility alias in
system tzdata. America/New_York preserves the intended Eastern daylight
time conversion without adding a dependency or changing parser behavior.
Upstream scrapinghub#1550 independently selected the same canonical timezone.
Drop the explanatory comment to avoid a needless overlapping-hunk
conflict with that change; test behavior is unchanged.
@agammann

agammann commented Nov 4, 2026

Copy link
Copy Markdown
Author

I addressed the review points on head 51270be and documented the rebase, related cases, and full local suite above. GitHub now reports the PR as mergeable. When convenient, could you re-review and approve the pending CI workflows? Happy to adjust anything else you find.

@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 addressing the earlier review, LGTM.

Non-blocking: now and _get_missing_parts() now run once for every format that is tried, so a long date_formats list with a non-UTC TIMEZONE gets noticeably slower; computing now once before the loop would avoid that.

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.

3 participants