Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix skipping day when parsing times if UTC and TIMEZONE day are different #1183

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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