Time zone bugs in Python are rarely about the time zone database being wrong. They come from code that never said which of the two kinds of datetime it was handling, or from an hour that exists twice a year. This reading review covers two pages of the Python documentation, the datetime page and the zoneinfo page, and pulls out what they say about naive and aware objects, the repeated hour, and what arithmetic actually does across a daylight saving transition.

1. Naive and aware, and why the distinction is not cosmetic (datetime page)

The page splits date and time objects into aware and naive. An aware object, with sufficient knowledge of time zone and daylight saving adjustments, can locate itself relative to other aware objects, and represents a specific moment in time that is not open to interpretation. A naive object does not contain enough information to locate itself unambiguously, and the page compares it to a number that might be metres or miles: only the program knows.

It also gives the test rather than a vibe: a datetime object d is aware if d.utcoffset() does not return None, and naive otherwise. Objects of type date are always naive, and the distinction does not apply to timedelta.

Lesson: "naive" is not a style preference. It means the object cannot answer "when, exactly".

Source: https://docs.python.org/3/library/datetime.html

2. utcnow() returns a naive object (same page)

The page is explicit that utcnow() returns the current UTC date and time as a naive datetime, and that because many datetime methods treat naive objects as local times, it prefers aware datetimes for UTC. Its recommended way to get the current time in UTC is datetime.now(timezone.utc).

The same pattern appears for timestamps. fromtimestamp() without a tz argument converts to the platform's local time and returns a naive object, and the page recommends datetime.fromtimestamp(timestamp, tz=timezone.utc) when what you want is a specific moment.

Lesson: a function named after UTC that hands back a naive object still leaves the ambiguity in place, because the object carries no marker saying "this is UTC".

3. The hour that happens twice, and the fold attribute (both pages)

The datetime page describes fold as a value in [0, 1] used to disambiguate wall times during a repeated interval, which happens when clocks are rolled back at the end of daylight saving time or when a zone's UTC offset is decreased. The values 0 and 1 mean the earlier and the later of the two moments that share the same wall time.

The zoneinfo page shows what that means in practice: a datetime of 2020-11-01 01:00 in America/Los_Angeles prints with the offset from before the transition (-07:00), and when.replace(fold=1) prints the same wall time with the offset after the transition (-08:00). It also notes that when converting from another time zone, fold is set to the correct value automatically, and it shows 08:00 UTC converting to 01:00-07:00 and 09:00 UTC converting to 01:00-08:00.

Lesson: an ambiguous local time is not a bug in your code, it is a question your code has to answer. fold is the answer, and converting from UTC fills it in for you.

Source: https://docs.python.org/3/library/zoneinfo.html

4. What arithmetic does across a transition (both pages)

This is where reading both pages together pays off. The datetime page states, for timedelta addition and subtraction, that the result has the same tzinfo attribute as the input and that no time zone adjustments are done even if the input is an aware object.

The zoneinfo page shows the consequence: adding one day to 2020-10-31 12:00 in Los Angeles gives 2020-11-01 12:00, with the name changing from PDT to PST. The wall time was preserved, and the offset moved, because the object carries the zone and the offset is derived from the zone's rules.

Lesson: aware arithmetic moves wall time, not elapsed time. If you need "24 hours later", do the arithmetic in UTC and convert for display.

5. Using ZoneInfo in practice (zoneinfo page)

The module was added in version 3.9 and implements the IANA database as a concrete tzinfo. By default it uses the system's time zone data when available, and falls back to the first-party tzdata package on PyPI otherwise. A ZoneInfo is attached through the constructor, datetime.replace() or datetime.astimezone().

The page also documents the smaller decisions: available_timezones() lists the keys the module can use, ZoneInfo.from_file() builds a zone from a file object, and ZoneInfo.no_cache(key) bypasses the cache. It spells out the serialization caveat too: a ZoneInfo built from a file raises an exception on pickling, and if you need to pickle one, the page suggests serializing by key or storing the file contents yourself.

Lesson: the zone database is data, not a constant. Which copy is loaded, and whether a zone came from the cache or a file, changes how objects serialize.

Checklist from the sources

  • Build aware datetimes: datetime.now(timezone.utc) rather than utcnow().
  • Pass tz=timezone.utc to fromtimestamp() when the value is a specific moment.
  • Treat d.utcoffset() is None as the naive/aware test, not as a hint.
  • Decide what an ambiguous local time means, and set fold deliberately when you construct one.
  • Do elapsed-time arithmetic in UTC, and convert to a zone only for display.
  • Know where your zone data comes from, and avoid pickling a ZoneInfo built from a file.

This is a documentation review, not a bug report. It reports no timing measurement and no behaviour from a specific application.

Sources: