Reputation: 1151
In Python, how do I convert a time.struct_time
such as
time.struct_time(tm_year=2012, tm_mon=10, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=275, tm_isdst=-1)
to a list such as
[2012, 10, 1, 0, 0, 0, 0, 275, -1]
My motivation: I'm storing time.struct_time
objects like the former in a json file and, when I retrieve them, they are lists like the latter. I need to compare one such list with a time.struct_time
; hence my question.
Upvotes: 0
Views: 1304
Reputation: 3807
Convert it with list(obj)
, ex:
>>>
a = time.gmtime()
>>>
a
time.struct_time(tm_year=2012, tm_mon=10, tm_mday=31, tm_hour=2, tm_min=53, tm_sec=4, tm_wday=2, tm_yday=305, tm_isdst=0)
>>>
list(a)
[2012, 10, 31, 2, 53, 4, 2, 305, 0]
Upvotes: 1
Reputation: 137438
Calling the list()
constructor will do what you need:
>>> t = time.localtime()
>>> list(t)
[2012, 10, 30, 22, 54, 25, 1, 304, 1]
Upvotes: 1