我有一个时间戳,例如1474398821633L
,我认为它是utc。我想将它与datetime.datetime.now()进行比较以验证它是否已过期。使用utc timestamp与python 2.7比较datetime.now()
我使用Python 2.7
from datetime import datetime
timestamp = 1474398821633L
now = datetime.now()
if datetime.utcfromtimestamp(timestamp) < now:
print "timestamp expired"
但是我试图从时间戳创建日期时间对象时,这个错误:ValueError: timestamp out of range for platform localtime()/gmtime() function
我能做些什么?
@BelowtheRadar - 我猜对了:-)。我知道的其他一些语言往往以毫秒为单位工作(例如Javascript),所以如果时间戳是毫秒时间戳而不是秒时间戳,那么您可能具有超出范围的时间戳看起来很自然。 – mgilson
仅仅将其解释为毫秒是不够的。 OP应该使用UTC时间而不是由'datetime.now()'返回的本地时间。 – jfs
@ J.F.Sebastian - 这是一个很好的观点。 – mgilson