[英]Why am I getting wrong times?
這是我的代碼:
x = 10
def get_date(submission):
time = submission.created
time_created = datetime.datetime.fromtimestamp(time)
time_current = datetime.datetime.now()
inbetween = time_created - time_current
inbetween_total = int(inbetween.total_seconds()) / 60
# If submission is older than 10 minutes, return True, if it isn't, return false
if inbetween_total > x:
print(time_created)
print(time_current)
print(inbetween)
print(inbetween_total)
print(inbetween.total_seconds())
return True
else:
print(inbetween)
print(inbetween_total)
print(inbetween.total_seconds())
return False
打印是為了調試。 我試圖獲取介於time_created和time_current之間的分鍾,但是我從不到五分鍾的舊帖子中得到了類似這樣的怪異值:
2017-09-26 16:11:44
2017-09-26 08:29:22.995548
7:42:21.004452
462.35
27741.004452
True
fromtimestamp()
可能正在傳遞一個Unix時代的時間,該時間應該是UTC(無時區)。 但是now()
在本地時區。 要解決此問題,請改用utcnow()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.