2017-09-26 29 views
0

這是我的代碼:爲什麼我錯了時間?

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分鐘,但我從不到5分鐘舊帖子越來越怪異值是這樣的:

2017-09-26 16:11:44 
2017-09-26 08:29:22.995548 
7:42:21.004452 
462.35 
27741.004452 
True 
+2

'betweenbetween_total'爲479分鐘。這意味着'time_created - time_current'是479分鐘。向我們展示'time_created'和'time_current'。也許你有一個時區問題。另外,'time_created - time_current'可能應該被顛倒過來。 –

+0

「if」中的''x''定義在哪裏? –

+0

'submission.created'是一個'date'對象嗎? – pstatix

回答

1

fromtimestamp()可能正在通過的Unix紀元時間,這是應該是UTC(沒有時區)。但now()在當地時區。要解決此問題,請改用utcnow()