-
Notifications
You must be signed in to change notification settings - Fork 124
Description
Problem
When dealing with timezone aware datetime objects the Pydantic fields are marshalled into Redis as timestamp floats. If the objects are naive then the stored timestamps will be local-time values and not referenced to UTC.
Unmarshalling datetime fields creates naive objects (no specified timezone).
There are several issues with this approach:
- Storing naive timestamps will cause time jumps forwards or backwards around the in and out of daylight savings period
- Dealing with datetime aware applications requires additional work to apply timezones to the unmarshalled redis-om objects
Without timezone values timezone aware applications cannot compare datetime objects and unmarshalled redis-om datetime objects.
Suggested solution
Align with all datastore solutions and use UTC timezone as the reference persistence values.
Marshalling of datetime objects to the datastore will be in UTC regardless of timezone aware or naive datetime objects
if dt.tzinfo is None:
dt = dt.astimezone(timezone.utc)
timestamp_value = dt.timestamp()Unmarshalling of datetime values is configurable for timezone naive or aware objects.
dt_obj = datetime.fromtimestamp(timestamp_value, timezone.utc)
if not config.timezone_aware:
# convert back to naive local time value
dt_obj = dt_obj.astimezone(None)