2017-09-26 72 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()