utctimetuple
--> calendar.timegm
--> utcfromtimestamp
形成一个往返:
import calendar
import datetime as DT
start = DT.datetime(2000, 1, 1, 0, 0, 0)
utc_tuple = start.utctimetuple()
utc_timestamp = calendar.timegm(utc_tuple)
startDifferent = DT.datetime.utcfromtimestamp(utc_timestamp)
print(startDifferent)
# 2000-01-01 00:00:00
timestamp
--> fromtimestamp
还有往返:
asFloat = start.timestamp()
startDifferent = DT.datetime.fromtimestamp(asFloat)
print(startDifferent)
# 2000-01-01 00:00:00
没有utc
相当于timestamp
它直接来自datetime.datetime
到一个时间戳。最接近的等价物是calendar.timegm(date.utctimetuple())
.
这大致描述了方法之间的关系:
o------------o
| | DT.datetime.utcfromtimestamp (*)
| |<-----------------------------------o
| | |
| | DT.datetime.fromtimestamp |
| datetime |<-------------------------------o |
| | | |
| | .timestamp | |
| |----------------------------o | |
| | | | |
o------------o | | |
| ^ | | |
.timetuple | | | | |
.utctimetuple (*) | | DT.datetime(*tup[:6]) | | |
v | v | |
o------------o o------------o
| |-- calendar.timegm (*) -->| |
| | | |
| |---------- time.mktime -->| |
| timetuple | | timestamp |
| |<-- time.localtime -------| |
| | | |
| |<-- time.gmtime (*)-------| |
o------------o o------------o
(*) 将其输入解释为 UTC 格式,并返回应解释为 UTC 格式的输出。