Skip to content

Datetime timezone design #5301

@mpharrigan

Description

@mpharrigan

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

  1. Load as "aware". dt != read_json(to_json(dt)) when dt was naive.
  2. Load as "naive". dt != read_json(to_json(dt)) when dt was aware.
  3. Be a stickler about timezones during serialization. Be annoying to users.
  4. Be a stickler about timezones everywhere possible. Be annoying to users.
  5. 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)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions