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: handle tzinfo in HH format in local mode #537

Merged
merged 4 commits into from
Mar 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading