Skip to content

Commit

Permalink
Merge branch 'master' into feat/oss-fuzz-bug-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
capuanob authored Dec 20, 2023
2 parents ab129af + 5bddb9f commit d11d91d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions dateparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,9 @@ def _correct_for_time_frame(self, dateobj, tz):
# Convert dateobj to utc time to compare with self.now
try:
tz = tz or get_timezone_from_tz_string(self.settings.TIMEZONE)
tz_offset = tz.utcoffset(dateobj)
except pytz.UnknownTimeZoneError:
tz = None
tz_offset = timedelta(hours=0)

dateobj_time = None
if tz:
Expand All @@ -577,10 +578,10 @@ def _correct_for_time_frame(self, dateobj, tz):
dateobj_time = dateobj.time()

if "past" in self.settings.PREFER_DATES_FROM:
if self.now.time() < dateobj_time:
if self.now < dateobj - tz_offset:
dateobj = dateobj + timedelta(days=-1)
if "future" in self.settings.PREFER_DATES_FROM:
if self.now.time() > dateobj_time:
if self.now > dateobj - tz_offset:
dateobj = dateobj + timedelta(days=1)

# Reset dateobj to the original value, thus removing any offset awareness that may
Expand Down
20 changes: 20 additions & 0 deletions tests/test_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,16 @@ def test_parsing_strings_containing_only_separator_tokens(
datetime(2021, 10, 19, 20, 0),
{"PREFER_DATES_FROM": "future"},
),
param(
"10pm EDT",
datetime(2021, 10, 20, 2, 0),
{"PREFER_DATES_FROM": "future"},
),
param(
"8am AEDT",
datetime(2021, 10, 18, 21, 0),
{"PREFER_DATES_FROM": "past"},
),
param(
"11pm AEDT",
datetime(2021, 10, 19, 12, 0),
Expand All @@ -1223,6 +1233,16 @@ def test_parsing_strings_containing_only_separator_tokens(
datetime(2021, 10, 19, 20, 0),
{"PREFER_DATES_FROM": "future", "TIMEZONE": "EDT"},
),
param(
"10pm",
datetime(2021, 10, 20, 2, 0),
{"PREFER_DATES_FROM": "future", "TIMEZONE": "EDT"},
),
param(
"8am",
datetime(2021, 10, 18, 21, 0),
{"PREFER_DATES_FROM": "past", "TIMEZONE": "AEDT"},
),
param(
"11pm",
datetime(2021, 10, 19, 12, 0),
Expand Down

0 comments on commit d11d91d

Please sign in to comment.