time.gmtime and time.strftime
time.gmtime([secs])
time.strftime(format[, t])Convert two fixed UTC timestamps to struct_time and format them as ISO-like text.
| Parameter | Description |
|---|---|
secs | Seconds since the Unix epoch. The example passes 1_000_000_000 and 0. If omitted, gmtime would use the current time, which this page does not print. |
format, t | A strftime format string and a struct_time. When t is omitted, strftime would use the local time; this example always passes the gmtime result. |
Returns: gmtime returns a struct_time. strftime returns a string.
Raises
ValueError / OverflowError— Raised for a timestamp outside the range the platform can represent, or for an invalid format string.
- These official signatures are written as in the Python 3.13 time documentation, which uses optional arguments in brackets.
import time
ts = 1_000_000_000
utc = time.gmtime(ts)
print(utc.tm_year, utc.tm_mon, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, utc.tm_wday, utc.tm_yday, utc.tm_isdst)
print(time.strftime("%Y-%m-%dT%H:%M:%SZ", utc))
print(time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(0)))Expected output
2001 9 9 1 46 40 6 252 0
2001-09-09T01:46:40Z
1970-01-01T00:00:00ZRelated: datetime (dates and timezones), zoneinfo