Skip to content

Commit

Permalink
Fix skipped day when UTC and tz are different days
Browse files Browse the repository at this point in the history
  • Loading branch information
MxMarx committed Sep 14, 2023
1 parent 725ccc0 commit 0df7c1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
11 changes: 4 additions & 7 deletions dateparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,15 @@ 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)

if tz:
dateobj_time = (dateobj - tz.utcoffset(dateobj)).time()
else:
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 @@ -1212,6 +1212,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 @@ -1222,6 +1232,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 0df7c1a

Please sign in to comment.