Sub topic 11
Where time zone data comes from, and the tzdata package
This chapter assumes you can build an aware datetime with ZoneInfo and call astimezone. It explains where the zone rules behind ZoneInfo("America/New_York") come from, and what to do when they are missing.
Python does not contain the rules
A time zone name such as America/New_York is only a key. The rules behind it include UTC offsets, daylight saving dates, and historical changes. They live in the IANA time zone database, also called tz, tzdata, or the Olson database. Governments change these rules often, so the database is released several times a year.
zoneinfo reads this data from compiled files. It looks in two places, in this order:
- The system time zone database, usually
/usr/share/zoneinfoon Linux and macOS. - The
tzdatapackage from PyPI, if it is installed.
Windows has no system database, so on Windows tzdata is the only source.
Inspecting the search path
zoneinfo.TZPATH is a tuple of the directories that are searched. You can replace it with the PYTHONTZPATH environment variable or with zoneinfo.reset_tzpath().
import zoneinfo
print(zoneinfo.TZPATH)
print("Asia/Tokyo" in zoneinfo.available_timezones())The first line prints your platform's directories, for example ('/usr/share/zoneinfo', '/usr/lib/zoneinfo', '/usr/share/lib/zoneinfo', '/etc/zoneinfo'). On Windows without tzdata it prints an empty tuple. available_timezones() returns a set of every key Python can find in either source. It scans the disk, so call it once and keep the result instead of calling it in a loop.
The common error
When no source has the key, ZoneInfo raises ZoneInfoNotFoundError. It is a subclass of KeyError.
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
try:
ZoneInfo("Mars/Olympus_Mons")
except ZoneInfoNotFoundError as e:
print("Unknown zone:", e)This code fails for a bad key, but it also fails for a good key like "Europe/Paris" when the machine has no data. A typo in the key and a missing database give the same exception, so check the key spelling first. If the key is valid, the environment is the problem. These setups often have no system database:
- Windows.
- Minimal container images such as
python:slim, Alpine, or distroless images. - Embedded or stripped-down Linux systems.
Code that works on your laptop and fails in a container is a classic sign of this problem.
Installing the tzdata package
The fix is to add tzdata as a normal dependency:
python -m pip install tzdatatzdata is a first-party package maintained by the Python core developers. It contains the IANA files as package data. Add it to requirements.txt or pyproject.toml for any program that runs on Windows or in a slim container. Installing it on a machine that already has a system database does no harm. The system copy is searched first, so tzdata acts as a fallback.
Keeping the data current
Rules change. A country can abolish daylight saving time or move its transition date with a few weeks of notice. A program with stale data computes wrong offsets for the affected dates, and nothing raises an error.
Which copy is used decides how you update it:
- System database: it is updated by the operating system. In containers it only changes when you rebuild the image.
tzdatapackage: update it withpip install --upgrade tzdata. Its version looks like2025.2, meaning the IANA release of that year and letter.
Two behaviors matter for long-running programs:
ZoneInfocaches the objects it creates. After the data changes underneath a running process, callZoneInfo.clear_cache()or restart the process.- Datetimes you already stored as UTC are not affected by rule changes. Future local times, such as a meeting stored as "9:00 in Cairo next year", can change meaning when the rules change. This is another reason to store UTC for past events and keep the zone key alongside future local appointments.
Keys are validated
ZoneInfo rejects keys that could escape the data directory, such as "../secret". It raises ValueError for them. This lets you pass user-supplied zone names to ZoneInfo without building file paths yourself. Catch both ValueError and ZoneInfoNotFoundError when you accept names from users.
Exercise
Your script runs on your Linux laptop but raises ZoneInfoNotFoundError for "Europe/Paris" inside a python:slim Docker container. What are two checks you can make, and what is the fix?
Show answer
Check that the key is spelled correctly, then check whether the container has any data: print zoneinfo.TZPATH and test "Europe/Paris" in zoneinfo.available_timezones(). If the key is valid and no data is found, the slim image has no system time zone database. Add tzdata to the project's dependencies (pip install tzdata) so ZoneInfo falls back to the package.