SyntaxStudy
Sign Up
Python Timezones with zoneinfo
Python Intermediate 4 min read

Timezones with zoneinfo

Timezones

Python 3.9+ includes zoneinfo for IANA timezone support. Always store UTC, display local time.

Example
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

utc_now  = datetime.now(tz=timezone.utc)
ny_now   = utc_now.astimezone(ZoneInfo("America/New_York"))
lon_now  = utc_now.astimezone(ZoneInfo("Europe/London"))
# Convert naive to aware
naive = datetime(2024, 6, 15, 14, 0)
aware = naive.replace(tzinfo=ZoneInfo("UTC"))
Pro Tip

Naive datetimes have no timezone — always use aware datetimes in multi-timezone applications.