Converting epoch format to struct object_time in Python
Seconds obtained from the epoch format can be converted to a special object struct_time
. Its data structure is similar to a dictionary. The object struct_time
is needed for more convenient work with dates stored in epoch.
Let's get the struct_time
object:
now = time.time()
res = time.localtime(now)
print(res)
Result of code execution:
time.struct_time(tm_year=2025, tm_mon=12,
tm_mday=31, tm_hour=15, tm_min=40, tm_sec=51, tm_wday=3,
tm_yday=365, tm_isdst=0)
Now let's get the current month from struct_time
. To do this, in the variable res
, we access its property tm_mon
through a dot:
print(res.tm_mon) # 12
Get the current day from struct_time
.
Get the current hour from struct_time
.
The following epoch is given:
dt = 1602314100.0
Get struct_time
from it.