Home Python Datetime Methods utctimetuple() Python datetime.utctimetuple() Method Definition utctimetuple() returns a time.struct_time describing the value in UTC . For an aware datetime it first subtracts utcoffset() ; for a naive datetime it uses the fields as they are, assuming they already mean UTC.
The tm_isdst flag is always 0, because UTC has no daylight saving time. Combined with calendar.timegm() it gives whole epoch seconds without depending on the local time zone.
datetime.utctimetuple() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.utctimetuple(...). Import the class first, for example from datetime import datetime.
Syntax Call it with no arguments.
tt = moment.utctimetuple() Call What it does datetime(2024, 3, 15, 2, 0, tzinfo=ist).utctimetuple().tm_mdayGives 14 when ist is UTC+05:30, because 02:00 there is 20:30 UTC the day before. datetime(2024, 3, 15, 10, 30).utctimetuple().tm_hourReturns 10: a naive value is used unchanged. datetime(2024, 7, 1, tzinfo=timezone.utc).utctimetuple().tm_isdstReturns 0: always 0 here.
Only datetime has this method; date objects do not.
Method signatures datetime.utctimetuple()
# returns: time.struct_time in UTC with tm_isdst == 0 Parameters utctimetuple() takes no parameters.
Return Value Return Value Description time.struct_time The UTC fields of the moment, weekday Monday = 0, day of year from 1, and tm_isdst=0.
Important
For an aware value near datetime.min or datetime.max, shifting to UTC can leave the supported range and raise OverflowError. Naive values are not converted, so a naive local time gives wrong UTC fields; make it aware first.
Python Examples 1. Aware values convert 2. Epoch seconds 3. Naive values Example 1 — Compare timetuple() and utctimetuple() An early-morning time at UTC+05:30 is still the previous evening in UTC.
from datetime import datetime, timedelta, timezone
ist = timezone(timedelta(hours=5, minutes=30))
moment = datetime(2024, 3, 15, 2, 0, tzinfo=ist)
local_tt = moment.timetuple()
utc_tt = moment.utctimetuple()
print(local_tt.tm_mday, local_tt.tm_hour, local_tt.tm_isdst)
print(utc_tt.tm_mday, utc_tt.tm_hour, utc_tt.tm_isdst) How It Works Wall-clock tuple. timetuple() keeps 15 March 02:00 and reports DST as unknown (-1).Shifted to UTC. Subtracting 5 hours 30 minutes gives 20:30 on 14 March.DST flag zero. The UTC tuple always reports tm_isdst=0.Example 2 — Get Epoch Seconds with calendar.timegm() calendar.timegm() reads a UTC struct_time, so it pairs with utctimetuple(). Compare it with timestamp(), which keeps fractions.
import calendar
from datetime import datetime, timedelta, timezone
ist = timezone(timedelta(hours=5, minutes=30))
moment = datetime(2024, 3, 15, 16, 0, 45, tzinfo=ist)
seconds = calendar.timegm(moment.utctimetuple())
print(seconds)
print(seconds == int(moment.timestamp()))
precise = moment.replace(microsecond=750000)
print(calendar.timegm(precise.utctimetuple()), precise.timestamp()) 1710498645
True
1710498645 1710498645.75 How It Works UTC first. 16:00:45 at UTC+05:30 is 10:30:45 UTC, which is 1710498645 seconds after the epoch.Matches timestamp(). For a whole-second value the integer from timegm() equals timestamp() .Fractions dropped. struct_time has no microseconds, so timegm() loses the .75 that timestamp() keeps.Example 3 — Naive Values Are Used As They Are A naive datetime is assumed to be UTC already: the fields are copied and the DST flag becomes 0.
from datetime import datetime
naive = datetime(2024, 12, 31, 23, 59, 59)
tt = naive.utctimetuple()
print(tt.tm_year, tt.tm_yday, tt.tm_hour, tt.tm_isdst)
print(naive.timetuple().tm_isdst)
print(tuple(tt)[:6]) 2024 366 23 0
-1
(2024, 12, 31, 23, 59, 59) How It Works No conversion. The naive fields go straight into the tuple; 31 December is day 366 of 2024.Different DST flags. utctimetuple() says 0, while timetuple() says -1 for the same naive value.Plain tuple. tuple(tt) turns the struct into a normal tuple you can slice.Frequently Asked Questions What is the difference between timetuple() and utctimetuple()? timetuple() keeps the wall-clock fields; utctimetuple() converts aware values to UTC and sets tm_isdst to 0.
How do I convert a datetime to epoch seconds without the local zone? For an aware value use calendar.timegm(moment.utctimetuple()) or int(moment.timestamp()).
Does utctimetuple() convert naive datetimes? No. It assumes they are already UTC and copies the fields.
Why is tm_isdst always 0? UTC never has daylight saving time.
Does date have utctimetuple()? No, only datetime does. Use date.timetuple() for dates.
Next: datetime.utcnow() Deprecated classmethod: current UTC time as a naive datetime; use now(timezone.utc).
utcnow() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
16 people found this page helpful
Helpful Share Copy link Suggestion