Skip to content

Use relative base month and day for custom date formats - #1394

Closed
Eric3-jp wants to merge 1 commit into
scrapinghub:masterfrom
Eric3-jp:fix-custom-format-relative-base
Closed

Eric3-jp wants to merge 1 commit into
scrapinghub:masterfrom
Eric3-jp:fix-custom-format-relative-base

Conversation

@Eric3-jp

Copy link
Copy Markdown

Custom formats fill missing month/day fields from the system clock even when RELATIVE_BASE is supplied. For example, parsing "2022" with date_formats=["%Y"] and RELATIVE_BASE=datetime(2020, 4, 30) uses today's month and day instead of returning 2022-04-30.

Pass the month/day from an explicit RELATIVE_BASE to the existing helpers when filling those fields. Explicit first/last preferences still take precedence, and complete dates and reported periods are preserved. When no relative base is supplied, the helpers retain their existing local-clock defaults.

The regressions freeze the system clock independently of RELATIVE_BASE and cover missing month, missing day, both missing fields, February day clamping with explicit leap/non-leap years, and first/last preferences. Four cases fail before the fix. Another control keeps the existing local-clock default when UTC and local time fall in different months.

Year inference remains unchanged. This is independent of the yearless month-end issue handled in #1389.

Validation on Python 3.12.3 with calendar/langdetect extras and timezone data installed:

  • pytest tests --ignore=tests/benchmarks --cov=dateparser: 24,369 passed, 15 skipped, 1 expected failure; 97% coverage.
  • pre-commit run --all-files: passed (Ruff 0.9.3 lint and formatting).
  • Wheel/sdist build and twine check: passed.

This issue was identified during source review. The implementation and tests were prepared with OpenAI Codex assistance.

@codspeed

codspeed Bot commented Sep 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks


Comparing Eric3-jp:fix-custom-format-relative-base (143094d) with master (901414a)

Open in CodSpeed

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.27%. Comparing base (901414a) to head (143094d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1394      +/-   ##
==========================================
+ Coverage   97.21%   97.27%   +0.06%     
==========================================
  Files         236      236              
  Lines        3084     3087       +3     
==========================================
+ Hits         2998     3003       +5     
+ Misses         86       84       -2     

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

@AdrianAtZyte

Copy link
Copy Markdown
Member

I would rather not keep the local-clock default, though: without RELATIVE_BASE, month and day would still come from local time while the year comes from UTC a few lines below, and the non-format parser uses UTC for all of them. Please, move the existing now = settings.RELATIVE_BASE or datetime.now(tz=timezone.utc)... line above the branches and pass current_month=now.month / current_day=now.day, like parser.py does, and drop the local-clock test.

In the tests, the PREFER_*="current" settings are the defaults and can go, and so can the "%Y-%m-%d" case, since it has no missing parts.

@Eric3-jp Eric3-jp closed this by deleting the head repository Sep 23, 2026
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.

2 participants