比較 Python 中的 calendar.timegm() 和 time.mktime()?


如果沒有明確宣告,Python 中的日期時間函式/模組會將所有內容假定為當地時區。

time.mktime() 假定傳遞的元組在當地時間,calendar.timegm() 假定是格林威治標準時間/世界標準時間。

根據解釋,元組表示不同的時間,因此函式返回不同的值(自紀元以來的秒數基於 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

更新日期: 2020 年 2 月 19 日

772 次瀏覽

開啟你的事業

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.