From 5bddb9f8126e39156fa692c0ba96ae9a965ca7aa Mon Sep 17 00:00:00 2001 From: Ruby Marx Date: Mon, 18 Dec 2023 02:11:35 -0800 Subject: [PATCH] Fix skipped day when UTC and tz are different days (#1183) --- dateparser/parser.py | 11 ++++------- tests/test_date_parser.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/dateparser/parser.py b/dateparser/parser.py index 297e73f3b..dcd8a194d 100644 --- a/dateparser/parser.py +++ b/dateparser/parser.py @@ -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 diff --git a/tests/test_date_parser.py b/tests/test_date_parser.py index 24ba6732c..97ee361cd 100644 --- a/tests/test_date_parser.py +++ b/tests/test_date_parser.py @@ -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), @@ -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),