Backports ZoneInfo
backports.zoneinfo provides a backport of the `zoneinfo` module from Python 3.9 for users running Python 3.6, 3.7, or 3.8. It offers an efficient, C-based implementation of the `zoneinfo` module, allowing older Python versions to use the standard library's `ZoneInfo` object for handling IANA time zones. The current version is 0.2.1, with releases typically happening as needed for bug fixes or minor enhancements.
Warnings
- gotcha When using `backports-zoneinfo` on Python versions older than 3.9, the correct import path is `from backports.zoneinfo import ZoneInfo`. Using `from zoneinfo import ZoneInfo` will result in an `ImportError` because the `zoneinfo` module is not built-in.
- gotcha `backports.zoneinfo` only provides the `ZoneInfo` API; it does not bundle timezone data itself. You must ensure that timezone data (e.g., IANA Time Zone Database) is available on your system for `ZoneInfo` to correctly resolve timezone names. Without this data, `ZoneInfo("America/New_York")` will raise a `ZoneInfoNotFoundError`.
- breaking An issue in version 0.2.0 caused `ZoneInfo.__init_subclass__` to not be a classmethod, leading to errors when attempting to subclass `ZoneInfo` directly. This was fixed in version 0.2.1.
Install
-
pip install backports-zoneinfo
Imports
- ZoneInfo
from backports.zoneinfo import ZoneInfo
Quickstart
import datetime
from backports.zoneinfo import ZoneInfo
# Ensure you have timezone data on your system (e.g., via 'pip install tzdata' or system package)
try:
paris_tz = ZoneInfo("Europe/Paris")
now_utc = datetime.datetime.now(datetime.timezone.utc)
now_paris = now_utc.astimezone(paris_tz)
print(f"UTC time: {now_utc.isoformat()}")
print(f"Paris time: {now_paris.isoformat()}")
except Exception as e:
print(f"Error: {e}. Do you have timezone data installed? (e.g. `pip install tzdata`)")