The gmtime method of the time module
The gmtime method of the time module converts seconds since the epoch to the struct_time format. The time is specified in UTC. In the optional parameter of the method, we specify the time in seconds since the epoch. If the parameter is not specified, then the current time is taken.
Syntax
import time
time.gmtime([time in seconds since the beginning of the epoch])
Example
Let's convert seconds to struct_time format:
import time
res = time.gmtime(3432785452)
print(res)
The result of the executed code:
time.struct_time(
tm_year=2078,
tm_mon=10,
tm_mday=12,
tm_hour=7,
tm_min=30,
tm_sec=52,
tm_wday=2,
tm_yday=285,
tm_isdst=0
)
Example
Now let's separately derive the year, month and day from the object obtained in the previous example:
import time
res = time.gmtime(3453253465)
print(res.tm_year)
print(res.tm_mon)
print(res.tm_mday)
The result of the executed code:
2023
7
31
Example
Let's get the current time in the format struct_time:
import time
res = time.gmtime()
print(res)
See also
-
method
timeof moduletime,
which returns the time in seconds since the epoch -
method
ctimeof moduletime,
which converts seconds to a datetime string -
method
localtimeof moduletime,
which converts seconds to local time -
method
mktimeof moduletime,
which converts a date and time string to seconds -
method
sleepof moduletime,
which stops the operation from running for the specified number of seconds