Dates, times and zones in Python
Aware datetimes only: a naive one is a bug waiting for a deploy.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
started = datetime(2026, 7, 29, 9, 30, tzinfo=timezone.utc)
local = started.astimezone(ZoneInfo("Europe/London"))
print(started.isoformat())
print(local.isoformat(), local.tzname())
print((started + timedelta(hours=36)).strftime("%a %d %b %H:%M"))
print(datetime.fromisoformat("2026-07-29T09:30:00+00:00") == started)
How it works
timezone.utcor aZoneInfomakes a datetime aware.astimezoneconverts; the instant does not change.isoformatandfromisoformatround-trip cleanly.
Keywords and builtins used here
print
The run, in numbers
- Lines
- 10
- Characters to type
- 400
- Tokens
- 107
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 61 seconds.
Step 2 of 3 in Configuration & time, step 33 of 41 in Domain tools.