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: make datetime range conditions tz aware #538

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
13 changes: 13 additions & 0 deletions qdrant_client/local/payload_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from typing import Any, List, Optional

import numpy as np
Expand Down Expand Up @@ -107,6 +108,13 @@ def check_range(condition: models.Range, value: Any) -> bool:


def check_datetime_range(condition: models.DatetimeRange, value: Any) -> bool:
def make_condition_tz_aware(dt: Optional[datetime]) -> Optional[datetime]:
if dt is None or dt.tzinfo is not None:
return dt

# Assume UTC if no timezone is provided
return dt.replace(tzinfo=timezone.utc)

if not isinstance(value, str):
return False

Expand All @@ -115,6 +123,11 @@ def check_datetime_range(condition: models.DatetimeRange, value: Any) -> bool:
if dt is None:
return False

condition.lt = make_condition_tz_aware(condition.lt)
condition.lte = make_condition_tz_aware(condition.lte)
condition.gt = make_condition_tz_aware(condition.gt)
condition.gte = make_condition_tz_aware(condition.gte)

return (
(condition.lt is None or dt < condition.lt)
and (condition.lte is None or dt <= condition.lte)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def random_datetime() -> Union[str, datetime]:
tzinfo=timezone(offset=timedelta(hours=random.randint(-12, 12)))
)

if random.random() < 0.1:
if random.random() < 0.15:
return random_datetime

dt_str = random_datetime.strftime(fmt)
Expand Down
Loading