utcoffset() returns how far the datetime's local time is ahead of UTC, as a timedelta: 5:30:00 for UTC+05:30. Zones west of UTC give negative values.
For a naive datetime it returns None. Otherwise it calls self.tzinfo.utcoffset(self), so a custom tzinfo can return different offsets for winter and summer. The offset includes any daylight saving shift reported by dst().
datetime.utcoffset() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.utcoffset(...). Import the class first, for example from datetime import datetime. The time class has a method with the same name.
Returns datetime.timedelta(days=-1, seconds=68400), which prints as -1 day, 19:00:00.
datetime(2024, 3, 15).utcoffset()
Returns None: the value is naive.
time.utcoffset() works the same way for aware time objects, and tzinfo.utcoffset(dt) is the method a custom zone implements.
Method signatures
python
datetime.utcoffset()
time.utcoffset()
tzinfo.utcoffset(dt) # implemented by tzinfo subclasses
# returns: timedelta strictly between -24h and +24h, or None
Parameters
utcoffset() takes no parameters on datetime and time objects.
Return Value
Return Value
Description
timedelta
Local time minus UTC for an aware value.
None
For a naive value, or when the tzinfo returns None.
Python Examples
Example 1 — Read the Offset of Three datetimes
A naive value, a UTC value, and a zone at UTC-03:30.
python
from datetime import datetime, timedelta, timezone
samples = [
datetime(2024, 3, 15, 10, 30),
datetime(2024, 3, 15, 10, 30, tzinfo=timezone.utc),
datetime(2024, 3, 15, 10, 30, tzinfo=timezone(timedelta(hours=-3, minutes=-30))),
]
for moment in samples:
print(moment.utcoffset())
Output
None
0:00:00
-1 day, 20:30:00
How It Works
Naive gives None. No tzinfo means no offset at all.
UTC gives zero.timedelta(0) prints as 0:00:00.
Negative offsets. Minus 3 hours 30 minutes is stored as -1 day plus 20:30:00, the normalized timedelta form.
Example 2 — Test Whether a datetime Is Aware
The official rule: a datetime is aware when tzinfo is set and utcoffset() does not return None. Then read an offset in hours.
python
from datetime import datetime, timedelta, timezone
def is_aware(moment):
return moment.tzinfo is not None and moment.utcoffset() is not None
print(is_aware(datetime(2024, 1, 1)))
print(is_aware(datetime(2024, 1, 1, tzinfo=timezone.utc)))
nepal = timezone(timedelta(hours=5, minutes=45))
offset = datetime(2024, 1, 1, tzinfo=nepal).utcoffset()
print(offset.total_seconds() / 3600)
Output
False
True
5.75
How It Works
Two-part check. A tzinfo could still return None, so the helper checks both conditions.
Naive vs aware. The plain datetime fails the test; the UTC one passes.
Hours as a float.total_seconds() of 5:45:00 is 20700.0, which is 5.75 hours.
Example 3 — Convert to UTC by Subtracting the Offset
Subtract the offset from the clock reading yourself and compare with astimezone(timezone.utc).