Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
agni-sairent opened this issue Aug 24, 2023 · 1 comment · Fixed by #534

Comments

@agni-sairent
Copy link

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
@jcrist
Copy link
Owner

jcrist commented Aug 24, 2023

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants