Python 创建未来五分钟的 unix 时间戳 [英] Python Create unix timestamp five minutes in the future
本文介绍了Python 创建未来五分钟的 unix 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在未来 5 分钟内创建一个Expires"值,但我必须以 UNIX 时间戳格式提供它.到目前为止,我有这个,但它似乎是一个黑客.
I have to create an "Expires" value 5 minutes in the future, but I have to supply it in UNIX Timestamp format. I have this so far, but it seems like a hack.
def expires():
'''return a UNIX style timestamp representing 5 minutes from now'''
epoch = datetime.datetime(1970, 1, 1)
seconds_in_a_day = 60 * 60 * 24
five_minutes = datetime.timedelta(seconds=5*60)
five_minutes_from_now = datetime.datetime.now() + five_minutes
since_epoch = five_minutes_from_now - epoch
return since_epoch.days * seconds_in_a_day + since_epoch.seconds
是否有模块或函数为我进行时间戳转换?
Is there a module or function that does the timestamp conversion for me?
推荐答案
刚发现这个,而且它更短.
Just found this, and its even shorter.
import time
def expires():
'''return a UNIX style timestamp representing 5 minutes from now'''
return int(time.time()+300)
这篇关于Python 创建未来五分钟的 unix 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文