fix: Return UTC-aware datetimes from unmarshalling#809
Open
abrookins wants to merge 5 commits intoredis:mainfrom
Open
fix: Return UTC-aware datetimes from unmarshalling#809abrookins wants to merge 5 commits intoredis:mainfrom
abrookins wants to merge 5 commits intoredis:mainfrom
Conversation
Previously, datetime fields were unmarshalled using datetime.fromtimestamp(value) which returns a naive datetime in the server's local timezone. This caused: - Non-deterministic behavior depending on server timezone - Inability to compare retrieved datetimes with timezone-aware datetimes - Time jumps around daylight savings transitions This fix changes unmarshalling to use datetime.fromtimestamp(value, timezone.utc) which returns a UTC-aware datetime. This follows the standard ORM pattern of storing UTC and returning UTC-aware datetimes. BREAKING CHANGE: Retrieved datetime fields are now UTC-aware instead of naive local time. Code that compared retrieved datetimes with naive datetimes will need to either: 1. Make the comparison datetime UTC-aware, or 2. Use .timestamp() for comparison Fixes redis#807
| # preferred timezone with dt.astimezone(tz). | ||
| dt = datetime.datetime.fromtimestamp( | ||
| value, datetime.timezone.utc | ||
| ) |
There was a problem hiding this comment.
Date fields shift by one day in non-UTC timezones
High Severity
The retrieval path now uses datetime.timezone.utc to interpret timestamps, but convert_datetime_to_timestamp still stores datetime.date values by combining with midnight in local time (naive datetime.datetime.combine(obj, datetime.time.min).timestamp()). When the server is east of UTC, local midnight is the previous day in UTC, so fromtimestamp(value, utc).date() returns the wrong date. For example, a stored date(2023, 1, 1) in UTC+5 round-trips as date(2022, 12, 31).
Additional Locations (1)
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.


Summary
Fixes #807 - Unmarshalled datetime fields are now UTC-aware instead of naive local time.
Problem
Previously, datetime fields were unmarshalled using
datetime.fromtimestamp(value)which returns a naive datetime in the server's local timezone. This caused:Solution
Changed unmarshalling to use
datetime.fromtimestamp(value, datetime.timezone.utc)which returns a UTC-aware datetime. This follows the standard ORM pattern of storing UTC and returning UTC-aware datetimes.Breaking Change
Retrieved datetime fields are now UTC-aware instead of naive local time. Code that compared retrieved datetimes with naive datetimes will need to either:
.timestamp()for comparisonTests
Added new tests for timezone-aware datetime handling and updated existing tests to work with the new behavior.
Co-authored by Augment Code
Note
Medium Risk
Changes the public behavior of datetime fields to return UTC-aware values instead of naive local-time datetimes, which can break callers comparing/serializing datetimes. Scope is limited to timestamp unmarshalling and associated tests.
Overview
Datetime unmarshalling now returns UTC-aware values.
convert_timestamp_to_datetime()switches fromdatetime.fromtimestamp(value)todatetime.fromtimestamp(value, datetime.timezone.utc), making retrieveddatetimefields consistently timezone-aware.Tests are updated to assert UTC tzinfo and compare instants via
.timestamp(), plus new coverage for round-tripping non-UTC timezone-aware datetimes in bothHashModelandJsonModel. Minor string/formatting cleanups are included in query/schema helpers.Written by Cursor Bugbot for commit 1bff3fd. This will update automatically on new commits. Configure here.