Skip to content

JSON encoder does not respect ZoneInfo (the IANA time zone database) when encoding datetime objects. #533

Closed

Description

@striking-quasar

Description

When using the JSON encoder on datetime objects with a timezone set using the IANA time zone database record, the encoder will not convert the datetime to UTC, only drop the timezone. This is not the case with the msgpack encoder, which correctly converts the datetime to UTC and removes the timezone.

I've replicated the issue using the Zone Info module from the Standard Library and the 3rd party module python-dateutil.

Minimal example:

import datetime
from zoneinfo import ZoneInfo

import msgspec

foo = datetime.datetime.now(tz=ZoneInfo("Europe/Prague"))
foo.isoformat()
# > 2023-08-24T12:17:59.327527+02:00

# NOT converted to UTC
foo_json = msgspec.json.decode(msgspec.json.encode(foo), type=datetime.datetime)
foo_json.isoformat()
# > 2023-08-24T12:17:59.327527+00:00

# Converted to UTC
foo_msgpack = msgspec.msgpack.decode(msgspec.msgpack.encode(foo), type=datetime.datetime)
foo_msgpack.isoformat()
# > 2023-08-24T10:17:59.327527+00:00

Activity

  1. jcrist commented on Aug 24, 2023

    @jcrist
    Member

    Thanks for the excellent reproducible issue, this should be fixed by #534.

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

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