Skip to content

fix(fields): store auto_now datetimes as UTC when use_tz=True (#2412) - #2413

Open
MokuLakshithReddy wants to merge 4 commits into
tortoise:developfrom
MokuLakshithReddy:fix-sqlite-auto-now-timezone
Open

MokuLakshithReddy wants to merge 4 commits into
tortoise:developfrom
MokuLakshithReddy:fix-sqlite-auto-now-timezone

Conversation

@MokuLakshithReddy

Copy link
Copy Markdown

Description

When use_tz=True and a non-UTC timezone is configured, DatetimeField.to_db_value() now returns timezone.now() (UTC) for auto_now and auto_now_add values instead of timezone.localtime(). The model instance attribute continues to receive the localized datetime matching the configured timezone via self.to_python_value(now).

Motivation and Context

Fixes #2412.

When use_tz=True and a non-UTC timezone is configured (e.g. timezone="Asia/Kolkata" or timezone="America/New_York"), auto_now and auto_now_add values were previously saved to the database with the local timezone offset (timezone.localtime()).

On database backends like SQLite which compare timestamp columns lexicographically as text strings, storing local offsets causes time filters against timezone.now() (UTC) to fail or return inverted results (e.g., created__lt=now() returning 0 for a record created milliseconds earlier in Asia/Kolkata, or created__gt=now()-1h returning 0 in America/New_York).

Per docs/timezone.rst:

"When enabled, all datetimes are stored as UTC in the database and tortoise.timezone.now() returns a timezone-aware datetime."

By using timezone.now() for the returned DB value, the database column always receives UTC while the Python model instance attribute continues to receive the localized timestamp via to_python_value().

How Has This Been Tested?

  • Added regression test test_datetime_auto_now_with_custom_timezone_sqlite_filters in tests/fields/test_time.py covering multiple timezones (UTC, Asia/Kolkata, America/New_York, Asia/Shanghai) verifying that datetime_add__lte=now and datetime_add__gt=now - timedelta(hours=1) both match newly created records.
  • Verified that all 585 tests in tests/fields/ pass (558 passed, 27 skipped).

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

…se#2412)

When use_tz=True and a custom timezone is configured, to_db_value()
previously saved auto_now and auto_now_add values with the local timezone
offset (timezone.localtime()). On backends like SQLite which compare
timestamps lexicographically as text strings, storing local offsets broke
time filtering and ordering against timezone.now() (UTC).

docs/timezone.rst specifies that 'When enabled, all datetimes are stored
as UTC in the database'. By using timezone.now() for the returned DB value,
the database column always receives UTC while the Python model instance
attribute continues to receive the localized timestamp via
to_python_value().

Fixes tortoise#2412
Copilot AI balanced review requested due to automatic review settings November 13, 2026 12:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codspeed

codspeed Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 24 untouched benchmarks


Comparing MokuLakshithReddy:fix-sqlite-auto-now-timezone (f5f51dc) with develop (938163c)

Open in CodSpeed

@waketzheng

Copy link
Copy Markdown
Contributor

#2273 (comment)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLite: since 1.1.9, auto_now values with a non-UTC timezone break time filters

3 participants