Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Changed

Fixed
^^^^^
- Store ``auto_now`` and ``auto_now_add`` datetime values as UTC when ``use_tz=True``, fixing SQLite time filtering and ordering with custom timezones. (#2412)
- ``DecimalField`` no longer raises ``decimal.InvalidOperation`` for values with more than 28 significant digits. (#2349)
- fix(expressions): inline subquery parameters when ``params_inline`` is enabled (#2340)
- Use database column names in reverse foreign-key and one-to-one filters with renamed columns or one-to-one primary keys. (#2338)
Expand Down
23 changes: 23 additions & 0 deletions tests/fields/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,13 +923,36 @@ async def test_datetime_naive_input_with_custom_timezone_update(db, enable_tz):
)

started_at = timezone.localtime()
await sleep(0.01)
obj.datetime = started_at.replace(tzinfo=None)
with pytest.warns(RuntimeWarning, match="received a naive datetime"):
await obj.save()
await sleep(0.01)
now = timezone.localtime()
obj = await model.get(id=obj.id)
assert obj.datetime == started_at, f"Expected {started_at}, got {obj.datetime}"
assert obj.datetime_add < started_at, f"Expected: {obj.datetime_add} < {started_at}"
assert now > obj.datetime_auto > started_at, (
f"Expected: {now} > {obj.datetime_auto} > {started_at}"
)


@pytest.mark.asyncio
async def test_datetime_auto_now_with_custom_timezone_sqlite_filters(db, enable_tz):
"""Test auto_now/auto_now_add with custom timezone allows correct SQLite time filters (#2412)."""
model = testmodels.DatetimeFields
for tz_name in ("UTC", "Asia/Kolkata", "America/New_York", "Asia/Shanghai"):
enable_tz(tz_name)
obj = await model.create(datetime=datetime(2021, 1, 1, tzinfo=get_default_timezone()))
now = timezone.now()

# The row was just created, so created <= now and created > now - 1h
count_lte = await model.filter(id=obj.id, datetime_add__lte=now).count()
count_gt = await model.filter(id=obj.id, datetime_add__gt=now - timedelta(hours=1)).count()
assert count_lte == 1, f"Expected datetime_add__lte=now for {tz_name}, got {count_lte}"
assert count_gt == 1, f"Expected datetime_add__gt=now-1h for {tz_name}, got {count_gt}"

# Fetch from DB and check instance matches
obj_db = await model.get(id=obj.id)
assert obj.datetime_add == obj_db.datetime_add
assert obj.datetime_add.tzinfo.key == tz_name or str(obj.datetime_add.tzinfo) == tz_name
39 changes: 25 additions & 14 deletions tortoise/fields/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,17 @@ def to_python_value(self, value: Any) -> datetime.datetime | None:
else parse_datetime(value)
)
if get_use_tz():
# When use_tz=True, ensure all datetimes are timezone-aware
# When use_tz=True, ensure all datetimes are timezone-aware.
# auto_now/auto_now_add values are written in UTC. Backends with
# timezone-naive timestamp columns return those values as naive
# datetimes, so interpret them as UTC before converting to the
# configured timezone. User-supplied naive datetimes retain the
# existing behavior of being interpreted in the configured zone.
if timezone.is_naive(value):
value = timezone.make_aware(value)
if self.auto_now or self.auto_now_add:
value = timezone.make_aware(value, "UTC").astimezone(get_default_timezone())
else:
value = timezone.make_aware(value)
else:
value = value.astimezone(get_default_timezone())
else:
Expand All @@ -538,24 +546,27 @@ def to_db_value(
self.auto_now
or (self.auto_now_add and getattr(instance, self.model_field_name) is None)
):
now = timezone.localtime() if get_use_tz() else timezone.now()
# Convert to match what would be read from DB (apply timezone conversion)
now_python = self.to_python_value(now)
setattr(instance, self.model_field_name, now_python)
now = timezone.now()
if get_use_tz():
setattr(instance, self.model_field_name, now.astimezone(get_default_timezone()))
else:
setattr(instance, self.model_field_name, now)
return now # type: ignore
if (
value is not None
and isinstance(value, datetime.datetime)
and get_use_tz()
and timezone.is_naive(value)
):
warnings.warn(
f"DateTimeField {self.model_field_name} received a naive datetime ({value})"
" while time zone support is active.",
RuntimeWarning,
stacklevel=2,
)
value = timezone.make_aware(value) # ty:ignore[invalid-assignment]
if timezone.is_aware(value):
value = value.astimezone(datetime.timezone.utc)
else:
warnings.warn(
f"DateTimeField {self.model_field_name} received a naive datetime ({value})"
" while time zone support is active.",
RuntimeWarning,
stacklevel=2,
)
value = timezone.make_aware(value) # ty:ignore[invalid-assignment]
self.validate(value)
return value

Expand Down
Loading