pytz-deprecation-shim

0.1.0.post0 · maintenance · verified Fri Apr 10

pytz-deprecation-shim provides compatibility shims to ease the migration away from the `pytz` library to PEP 495-compatible time zone implementations like the standard library's `zoneinfo` (Python 3.9+) or `python-dateutil`. It allows existing codebases to gradually deprecate `pytz`-specific interfaces while ensuring continued functionality. The current version is 0.1.0.post0, with the last release in June 2020.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pytz_deprecation_shim.timezone` to obtain a time zone object and create an aware datetime by directly assigning the `tzinfo`. It highlights the PEP 495-compatible usage, where `localize()` and `normalize()` are typically avoided. It also shows how to wrap an existing `zoneinfo.ZoneInfo` object.

import pytz_deprecation_shim as pds
from datetime import datetime

# Get a timezone using the shim (behaves like zoneinfo/dateutil.tz)
LA = pds.timezone("America/Los_Angeles")

# Create an aware datetime by directly attaching the tzinfo
dt = datetime(2020, 10, 31, 12, tzinfo=LA)
print(f"Aware datetime: {dt}")
print(f"Timezone name: {dt.tzname()}")

# The shim can also wrap existing PEP 495-compatible time zones
# from datetime.zoneinfo (Python 3.9+) or dateutil.tz
try:
    from zoneinfo import ZoneInfo
    NYC_zoneinfo = ZoneInfo("America/New_York")
    NYC_shim = pds.wrap_zone(NYC_zoneinfo, key="America/New_York")
    dt_nyc = datetime(2023, 7, 15, 10, tzinfo=NYC_shim)
    print(f"Wrapped ZoneInfo datetime: {dt_nyc}")
except ImportError:
    print("zoneinfo not available (Python < 3.9 or backport not installed)")

view raw JSON →