Skip to content

Unmarshalled Datetime fields are naive #807

@hurdlea

Description

@hurdlea

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)

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