a = time.time()
time.sleep(3)
b = time.time()
time_consuming = time.strftime("%H:%M:%S", time.localtime(b - a))
print(time_consuming)
用以上程式碼去跑time.strftime格式化時間後小時處總有個預設值,這個8小時的預設值怎麼來的
迷茫2017-05-18 10:52:41
因為時區的問題,不應該用 time,應該用 datetime.timedelta 來計算
參考下面程式碼
import time
import datetime
a = time.time()
time.sleep(3)
b = time.time()
print datetime.timedelta(seconds=(b -a )) # <-- 推荐
time_consuming = time.strftime("%H:%M:%S", time.gmtime(b - a)) # <-- 不推荐
print(time_consuming)
輸出如下:
0:00:03.004802
00:00:03