Skip to content

DatetimeField auto_now/auto_now_add with use_tz=False store a datetime shifted by the host UTC offset #2348

Closed

Description

@moneebullah25

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.

To Reproduce

class T(Model):
    id = fields.IntField(primary_key=True)
    a = fields.DatetimeField(auto_now=True)
    b = fields.DatetimeField(null=True)

await Tortoise.init(db_url="sqlite://:memory:", modules={"m": ["__main__"]}, use_tz=False, timezone="UTC")
await Tortoise.generate_schemas()
t = await T.create(b=datetime.datetime.now())

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

  1. added a commit that references this issue on Oct 24, 2026
    66bb9fe
  2. added a commit that references this issue on Oct 24, 2026
    d1acbfb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions