Backports ZoneInfo

0.2.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates importing `ZoneInfo` and using it to create a timezone-aware datetime object. Note that `backports.zoneinfo` provides the API, but relies on system timezone data (e.g., from `/usr/share/zoneinfo` or the `tzdata` package) to resolve timezone names like 'Europe/Paris'.

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`)")

view raw JSON →