如何在 Python 中比較 calendar.timegm() 和 time.mktime()?
除非明確宣告,否則 Python 中的日期時間函式/模組假定本地時區中的所有內容。
time.mktime() 假定傳遞的元組在本地時間,calendar.timegm() 假定它在 GMT/UTC 中。
根據解釋,元組代表不同的時間,因此函式返回不同的值 (自紀元以來的秒數基於 UTC)。
值之間的差異應等於本地時區的時區偏移。
示例
import calendar import time from datetime import datetime dt = datetime(2017, 12, 31) print(time.mktime(dt.timetuple())) print(calendar.timegm(dt.timetuple()))
輸出
這將給出以下輸出 -
1514658600.0 1514678400
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP