From d680a3187130a19cc9f374d0241a4189f4639a1b Mon Sep 17 00:00:00 2001 From: chuenchen309 <48723787+chuenchen309@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:38:33 +0800 Subject: [PATCH] Honor PREFER_DATES_FROM for a date_formats entry with no year MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_with_formats applied PREFER_DATES_FROM only to a two-digit-year format (via _apply_century_preference, added in #1342). A format with no year directive at all just took the current year unconditionally, so parse("December 25", date_formats=["%B %d"], settings={"PREFER_DATES_FROM": "past"}) returned a future date -- contradicting the setting, and disagreeing with the NLP path, which parses the same string correctly. Generalise the helper to shift by ±N years and call it with 1 for the year-less branch (100 stays for the two-digit branch), so both share the same direction and Feb-29 leap-year handling. Signed-off-by: chuenchen309 <48723787+chuenchen309@users.noreply.github.com> --- dateparser/date.py | 20 ++++++++++++-------- tests/test_date_parser.py | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/dateparser/date.py b/dateparser/date.py index c1d89c5da..7bf15145c 100644 --- a/dateparser/date.py +++ b/dateparser/date.py @@ -337,12 +337,13 @@ def get_date_from_timestamp( return None -def _apply_century_preference( - date_obj: datetime, now: datetime, prefer_from: str +def _apply_year_preference( + date_obj: datetime, now: datetime, prefer_from: str, years: int ) -> datetime: - """Shift *date_obj* by ±100 years to satisfy PREFER_DATES_FROM. + """Shift *date_obj* by ±*years* to satisfy PREFER_DATES_FROM. - *date_obj* and *now* are compared as naive datetimes, so either of them + Used with ``years=100`` for a two-digit-year format and ``years=1`` for a + format with no year at all. *date_obj* and *now* are compared as naive datetimes, so either of them can be tz-aware. When the shifted year falls on Feb 29 in a non-leap year, the nearest valid leap year in the preferred direction is used — matching the behaviour of the NLP path in :file:`parser.py`. @@ -353,9 +354,9 @@ def _apply_century_preference( date_naive = date_obj.replace(tzinfo=None) if now_naive < date_naive and prefer_from == "past": - target_year = date_obj.year - 100 + target_year = date_obj.year - years elif now_naive >= date_naive and prefer_from == "future": - target_year = date_obj.year + 100 + target_year = date_obj.year + years else: return date_obj @@ -405,9 +406,12 @@ def parse_with_formats( now = settings.RELATIVE_BASE or _now(settings) if "year" in _missing: date_obj = date_obj.replace(year=now.year) + date_obj = _apply_year_preference( + date_obj, now, settings.PREFER_DATES_FROM, 1 + ) elif "%y" in date_format and "%Y" not in date_format: - date_obj = _apply_century_preference( - date_obj, now, settings.PREFER_DATES_FROM + date_obj = _apply_year_preference( + date_obj, now, settings.PREFER_DATES_FROM, 100 ) date_obj = apply_timezone_from_settings(date_obj, settings) diff --git a/tests/test_date_parser.py b/tests/test_date_parser.py index 72988bce3..862e963d6 100644 --- a/tests/test_date_parser.py +++ b/tests/test_date_parser.py @@ -2400,7 +2400,7 @@ def test_prefer_dates_from_with_date_formats_tz_aware_relative_base(self) -> Non def test_prefer_dates_from_with_date_formats_feb29_non_leap(self) -> None: """Test that Feb 29 shifted to a non-leap year finds the next valid leap year (Bug #2 fix).""" # 2/29/00 parses as 2000-02-29; shifting +100 → 2100 which is not a leap year. - # _apply_century_preference finds the next valid leap year: 2104. + # _apply_year_preference finds the next valid leap year: 2104. result = parse( "2/29/00", date_formats=["%m/%d/%y"], @@ -2415,6 +2415,30 @@ def test_prefer_dates_from_with_date_formats_feb29_non_leap(self) -> None: self.assertEqual(2, result.month) self.assertEqual(29, result.day) + @parameterized.expand( + [ + param("December 25", "past", datetime(2026, 6, 15), datetime(2025, 12, 25)), + param("March 10", "future", datetime(2026, 6, 15), datetime(2027, 3, 10)), + ] + ) + def test_prefer_dates_from_with_yearless_format( + self, + date_string: str, + prefer_from: str, + relative_base: datetime, + expected: datetime, + ) -> None: + """PREFER_DATES_FROM also applies to a date_formats entry with no year.""" + result = parse( + date_string, + date_formats=["%B %d"], + settings={ + "PREFER_DATES_FROM": prefer_from, + "RELATIVE_BASE": relative_base, + }, + ) + self.assertEqual(expected, result) + @parameterized.expand( [ param(date_string="1999001", expected=datetime(1999, 1, 1)),