所述time模块可使用秒因为历元进行初始化:
time
>>> import time >>> t1=time.gmtime(1284286794) >>> t1 time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19, tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0)
有没有一种优雅的方法可以datetime.datetime以相同的方式初始化对象?
datetime.datetime
datetime.datetime.fromtimestamp 如果您知道时区,您将产生与相同的输出 time.gmtime
datetime.datetime.fromtimestamp
time.gmtime
>>> datetime.datetime.fromtimestamp(1284286794) datetime.datetime(2010, 9, 12, 11, 19, 54)
要么
>>> datetime.datetime.utcfromtimestamp(1284286794) datetime.datetime(2010, 9, 12, 10, 19, 54)