Repository navigation
fix(fields): store auto_now datetimes as UTC when use_tz=True (#2412) - #2413
Open
MokuLakshithReddy wants to merge 4 commits into
Open
MokuLakshithReddy wants to merge 4 commits into
MokuLakshithReddy wants to merge 4 commits into
Conversation
…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
Merging this PR will not alter performance1 warning
Comparing |
Contributor
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When
use_tz=Trueand a non-UTC timezone is configured,DatetimeField.to_db_value()now returnstimezone.now()(UTC) forauto_nowandauto_now_addvalues instead oftimezone.localtime(). The model instance attribute continues to receive the localized datetime matching the configured timezone viaself.to_python_value(now).Motivation and Context
Fixes #2412.
When
use_tz=Trueand a non-UTC timezone is configured (e.g.timezone="Asia/Kolkata"ortimezone="America/New_York"),auto_nowandauto_now_addvalues 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 inAsia/Kolkata, orcreated__gt=now()-1hreturning 0 inAmerica/New_York).Per
docs/timezone.rst: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 viato_python_value().How Has This Been Tested?
test_datetime_auto_now_with_custom_timezone_sqlite_filtersintests/fields/test_time.pycovering multiple timezones (UTC,Asia/Kolkata,America/New_York,Asia/Shanghai) verifying thatdatetime_add__lte=nowanddatetime_add__gt=now - timedelta(hours=1)both match newly created records.tests/fields/pass (558 passed, 27 skipped).Checklist: