Apply dates, UTC conversion and validation in one short integrated exercise.
How It Works
An event time should be parsed, given a documented timezone, converted to UTC and displayed in the user’s zone. This checkpoint intentionally starts from an offset-aware ISO string so no implicit local timezone assumptions are needed.
An explicit offset identifies the instant; a named timezone is needed for ongoing local schedules.
Step-by-Step Example
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
def display_event(iso_value, zone_name):
event = datetime.fromisoformat(iso_value)
if event.tzinfo is None:
raise ValueError("Event time needs a timezone offset")
return event.astimezone(ZoneInfo(zone_name)).isoformat()
print(display_event("2026-10-17T10:00:00+00:00", "Europe/Paris"))
Understanding the Example
- The function definition gives the operation a reusable name and makes its inputs explicit.
- The expression 'datetime.fromisoformat(iso_value)' supplies the value stored as 'event'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
2026-10-17T12:00:00+02:00
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
Change the destination to Asia/Tokyo and add a test that rejects naive timestamps.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Accepting timezone-free timestamps and silently guessing the origin timezone.
- Confusing a UTC offset with a geographic timezone that has historical and future rules.
Key Takeaways
- Apply dates, UTC conversion and validation in one short integrated exercise.
- An event time should be parsed, given a documented timezone, converted to UTC and displayed in the user’s zone.
- An explicit offset identifies the instant; a named timezone is needed for ongoing local schedules.