-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Datetimes come in two flavors: with ("aware") or without ("naive") timezone information. There's also the timestamp
integer which is always UTC.
You can generally convert between the three forms.
actual restrictions / considerations:
- you can't compare naive vs. aware datetimes
- more opportunity for error when converting aware -> naive
- if someone somehow gives you a naive datetime from a different timezone it could be misinterpreted
- proto library gives aware datetimes
A naive datetime is inappropriate for serialization, so we use its timestamp
.
Options
- Load as "aware".
dt != read_json(to_json(dt))
whendt
was naive. - Load as "naive".
dt != read_json(to_json(dt))
whendt
was aware. - Be a stickler about timezones during serialization. Be annoying to users.
- Be a stickler about timezones everywhere possible. Be annoying to users.
- Upconvert to "aware" wherever we can.
CirqObj(dt=dt).dt != dt
(3) is annoying to users when we could easily up-convert, especially if errors only appear during serialization which might not be caught by tests. Users could easily end up with a mish-mash of aware and naive datetimes and have to learn about all this nonsense.
(4) is also pretty annoying to users but they won't have a mish-mash. Annoying for developers who have to remember to put in checks
(1,2) vs (5) is a decision about where to break roundtrippability: in our containers or during json.
Appendix
There's probably a hidden (6) option to serialize datetimes exactly as they come and if you serialize naive datetimes you are free to screw yourself up.
https://docs.python.org/3/library/datetime.html
# naive -> aware
dt.astimezone()
# aware -> naive
dt.astimezone(None).replace(tzinfo=None)