Full answer
python
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
ist = ZoneInfo("Asia/Kolkata"); london = ZoneInfo("Europe/London")
meeting = datetime(2026, 3, 28, 15, 0, tzinfo=ist)
print(meeting.isoformat(), meeting.astimezone(timezone.utc).isoformat())
print(meeting.astimezone(london).strftime("%H:%M %Z"), (meeting + timedelta(days=2)).astimezone(london).strftime("%H:%M %Z"))
naive = datetime(2026, 3, 28, 15, 0)
try:
naive < meeting
except TypeError as e:
print("TypeError:", e)
print(datetime.fromisoformat("2026-12-04T01:38:00+05:30").astimezone(timezone.utc))Output:
text
2026-03-28T15:00:00+05:30 2026-03-28T09:30:00+00:00
09:30 GMT 10:30 BST
TypeError: can't compare offset-naive and offset-aware datetimes
2026-12-03 20:08:00+00:00The 15:00 IST meeting is 09:30 UTC. In London it is 09:30 GMT on 28 March, but the same IST time two days later is 10:30 BST because the UK moved its clocks on 29 March 2026; ZoneInfo applies the rules automatically, while a fixed offset would be wrong. Comparing a naive and an aware datetime raises TypeError, which prevents silent mistakes. fromisoformat parses offsets, and converting to UTC normalises values for storage.
python
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
ist = ZoneInfo("Asia/Kolkata"); london = ZoneInfo("Europe/London")
meeting = datetime(2026, 3, 28, 15, 0, tzinfo=ist)
print(meeting.isoformat(), meeting.astimezone(timezone.utc).isoformat())
print(meeting.astimezone(london).strftime("%H:%M %Z"), (meeting + timedelta(days=2)).astimezone(london).strftime("%H:%M %Z"))
naive = datetime(2026, 3, 28, 15, 0)
try:
naive < meeting
except TypeError as e:
print("TypeError:", e)
print(datetime.fromisoformat("2026-12-04T01:38:00+05:30").astimezone(timezone.utc))Run on Python 3.13.5, twice in fresh interpreters, with identical output.
Key points
- Use aware datetimes for instants
- Store UTC, display local
- ZoneInfo handles daylight saving
- Naive and aware values cannot be compared
- fromisoformat parses offsets
Common mistakes
- Using naive datetime.now() for timestamps
- Hard-coding UTC offsets for zones with DST
Follow-ups
- How would you schedule a daily 9 AM job in a user's zone?
- What is the fold attribute for?
Tips
- Show the DST change