Content · Programming · Python

Python datetime.timezone Class: Fixed UTC Offsets

Apply a fixed offset from UTC with datetime.timezone.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Apply a fixed offset from UTC with datetime.timezone.

How It Works


datetime.timezone(timedelta(hours=...)) represents a fixed UTC offset. It does not track daylight saving transitions or historical changes; use ZoneInfo for real geographic regions.

Step-by-Step Example


from datetime import datetime, timedelta, timezone
fixed_offset = timezone(timedelta(hours=3))
local_time = datetime(2026, 10, 17, 9, tzinfo=fixed_offset)
print(local_time.utcoffset())
print(local_time.astimezone(timezone.utc).hour)

Understanding the Example


  • The expression 'timezone(timedelta(hours=3))' supplies the value stored as 'fixed_offset'. This value is then used or checked by the later statements.
  • The expression 'datetime(2026, 10, 17, 9, tzinfo=fixed_offset)' supplies the value stored as 'local_time'. This value is then used or checked by the later statements.

A successful run of this example prints the following output:

3:00:00
6

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


A fixed-offset timezone cannot infer DST rules for cities.

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


  • Using a fixed +03:00 offset as a substitute for a named city timezone for all dates.
  • Convert a fixed UTC+02:00 datetime to UTC.

Key Takeaways


  • Apply a fixed offset from UTC with datetime.timezone.
  • datetime.timezone(timedelta(hours=...)) represents a fixed UTC offset.
  • Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.
Share this resourceSend this page to someone who may find it useful.