Skip to content

Commit

Permalink
Fix date_parser with prefer_month_of_year wrong results
Browse files Browse the repository at this point in the history
Fix two problems
1. Parser would use current month even if prefer_month_of_year was not
   current when relative_base was not none

2. Parser would use current month to derive 'what is the last day of
   this month' - for example, with prefer_month=last and
   prefer_day=past, but current_month=april, it would return december
   30th, because it would use april to find that the last day was the
   30th, when it should use the month.

Additionally, add a test to test_date_parser that uses prefer_month
  • Loading branch information
benbuzbee committed Apr 2, 2024
1 parent f659364 commit 13efeac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
11 changes: 7 additions & 4 deletions dateparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,11 @@ def _correct_for_month(self, dateobj):
relative_base_month = (
relative_base.month if hasattr(relative_base, "month") else relative_base
)
if getattr(self, "_token_month", None) or relative_base_month:

if getattr(self, "_token_month", None):
return dateobj

dateobj = set_correct_month_from_settings(dateobj, self.settings)
dateobj = set_correct_month_from_settings(dateobj, self.settings, relative_base_month)
return dateobj

@classmethod
Expand All @@ -613,11 +614,13 @@ def parse(cls, datestring, settings, tz=None):
# correction for past, future if applicable
dateobj = po._correct_for_time_frame(dateobj, tz)

# correction for preference of month: beginning, current, end
# must happen before day so that day is derived from the correct month
dateobj = po._correct_for_month(dateobj)

# correction for preference of day: beginning, current, end
dateobj = po._correct_for_day(dateobj)

# correction for preference of month: beginning, current, end
dateobj = po._correct_for_month(dateobj)
period = po._get_period()

return dateobj, period
Expand Down
57 changes: 57 additions & 0 deletions tests/test_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,63 @@ def test_prefer_dates_from_with_timezone(
self.then_date_was_parsed_by_date_parser()
self.then_date_obj_exactly_is(expected)


@parameterized.expand(
[
param(
"2015",
prefer_day="current",
prefer_month="current",
today=datetime(2010, 2, 10),
expected=datetime(2015, 2, 10),
),
param(
"2015",
prefer_day="last",
prefer_month="current",
today=datetime(2010, 2, 10),
expected=datetime(2015, 2, 28),
),
param(
"2015",
prefer_day="first",
prefer_month="current",
today=datetime(2010, 2, 10),
expected=datetime(2015, 2, 1),
),
param(
"2015",
prefer_day="current",
prefer_month="last",
today=datetime(2010, 2, 10),
expected=datetime(2015, 12, 10),
),
param(
"2015",
prefer_day="last",
prefer_month="last",
today=datetime(2010, 2, 10),
expected=datetime(2015, 12, 31),
),
param(
"2020", #Leap year last day test
prefer_day="last",
prefer_month="current",
today=datetime(2010, 2, 10),
expected=datetime(2020, 2, 29),
),
]
)
def test_dates_with_no_day_or_month(
self, date_string, prefer_day, prefer_month, today=None, expected=None
):
self.given_parser(
settings={"PREFER_DAY_OF_MONTH": prefer_day, "PREFER_MONTH_OF_YEAR": prefer_month, "RELATIVE_BASE": today}
)
self.when_date_is_parsed(date_string)
self.then_date_was_parsed_by_date_parser()
self.then_date_obj_exactly_is(expected)

def given_local_tz_offset(self, offset):
self.add_patch(
patch.object(
Expand Down

0 comments on commit 13efeac

Please sign in to comment.