Skip to content

Commit

Permalink
fix: handle tzinfo in HH format in local mode (#537)
Browse files Browse the repository at this point in the history
* fix: handle tzinfo in HH format in local mode

* fix: fix mypy

* fix: replace dt split by trying adding minutes to tz (#540)

* refactoring: remove redundant import
  • Loading branch information
joein authored Mar 13, 2024
1 parent 848c63c commit 185121e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
33 changes: 22 additions & 11 deletions qdrant_client/local/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ def parse(date_str: str) -> Optional[datetime]:
Optional[datetime]: the datetime if the string is valid, otherwise None
"""

for fmt in available_formats:
try:
dt = datetime.strptime(date_str, fmt)
if dt.tzinfo is None:
# Assume UTC if no timezone is provided
dt = dt.replace(tzinfo=timezone.utc)
return dt
except ValueError:
pass

return None
def parse_available_formats(datetime_str: str) -> Optional[datetime]:
for fmt in available_formats:
try:
dt = datetime.strptime(datetime_str, fmt)
if dt.tzinfo is None:
# Assume UTC if no timezone is provided
dt = dt.replace(tzinfo=timezone.utc)
return dt
except ValueError:
pass
return None

parsed_dt = parse_available_formats(date_str)
if parsed_dt is not None:
return parsed_dt

# Python can't parse timezones containing only hours (+HH), but it can parse timezones with hours and minutes
# So we add :00 to the assumed timezone and try parsing it again
# dt examples to handle:
# "2021-01-01 00:00:00.000+01"
# "2021-01-01 00:00:00.000-10"
return parse_available_formats(date_str + ":00")
14 changes: 9 additions & 5 deletions qdrant_client/local/tests/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@
datetime(2021, 1, 1, 0, 0, 0, 9, tzinfo=timezone(timedelta(minutes=30))),
),
# this is accepted in core but not here, there is no specifier for only-hour offset
# (
# "2021-01-01 00:00:00.000+01",
# datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone(timedelta(hours=10))),
# ),
(
"2021-01-01 00:00:00.000+01",
datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone(timedelta(hours=1))),
),
(
"2021-01-01 00:00:00.000-10",
datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone(timedelta(hours=-10))),
),
(
"2021-01-01 00:00:00-03:00",
datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone(timedelta(days=-1, seconds=75600))),
datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone(timedelta(hours=-3))),
),
],
)
Expand Down

0 comments on commit 185121e

Please sign in to comment.