You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DatetimeField auto_now/auto_now_add with use_tz=False store a datetime shifted by the host UTC offset #2348
Describe the bug
With use_tz=False, timezone.now() returns a naive local datetime.now() (documented in docs/timezone.rst and the now() docstring). DatetimeField.to_db_value (tortoise/fields/data.py:492) calls timezone.localtime(), which does now().astimezone(get_default_timezone()). Python treats the naive value as host-local and converts it to the configured TIMEZONE (UTC by default), so an aware, shifted datetime is written and set on the instance. docs/timezone.rst says timezone only takes effect when use_tz=True.
This may be a regression from #2277 (commit 0a454c1), where this line appears to have changed from timezone.now(). I have not bisected it, so that attribution is unverified.
Run with TZ=America/New_York. Output (observed by me when drafting this report):
naive now 2026-10-18 20:23:13.212271 (datetime.now() 20:23:13.212282)
a datetime.datetime(2026, 10, 19, 0, 23, 13, 212504) b datetime.datetime(2026, 10, 18, 20, 23, 13, 212290)
db a 2026-10-19 00:23:13.212504 b 2026-10-18 20:23:13.212290
auto_now is 4 hours ahead of the same instant set manually, so ordering and range filters mix two conventions.
Expected behavior
With use_tz=False the auto value should equal timezone.now() (naive local). For example now = timezone.localtime() if get_use_tz() else timezone.now().
Additional context
Severity is Low-Medium: it only happens when the host timezone differs from the configured TIMEZONE and use_tz=False, so it is easy to miss on UTC servers. TimeField is not affected. Checked against develop @ 5815573. Related but not the same: #458 (feature request to use local timezone for auto_now) and #2273 (closed, fixed by #2277, covers use_tz=True with a non-UTC TIMEZONE). I searched open and closed issues and PRs and found no report of the use_tz=False shift.
Activity
Keep auto_now values naive when use_tz is False (tortoise#2350)
Keep auto_now values naive when use_tz is False (tortoise#2350)