Python将来五分钟创建unix时间戳

本文翻译自:Python Create unix timestamp five minutes in the future

I have to create an "Expires" value 5 minutes in the future, but I have to supply it in UNIX Timestamp format. 我必须在未来5分钟创建一个“Expires”值,但我必须以UNIX Timestamp格式提供它。 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? 是否有为我进行时间戳转换的模块或函数?


#1楼

参考:https://stackoom.com/question/Be80/Python将来五分钟创建unix时间戳


#2楼

The key is to ensure all the dates you are using are in the utc timezone before you start converting. 关键是在开始转换之前确保您使用的所有日期都在utc时区内。 See http://pytz.sourceforge.net/ to learn how to do that properly. 请参阅http://pytz.sourceforge.net/以了解如何正确执行此操作。 By normalizing to utc, you eliminate the ambiguity of daylight savings transitions. 通过标准化为utc,您可以消除夏令时转换的模糊性。 Then you can safely use timedelta to calculate distance from the unix epoch, and then convert to seconds or milliseconds. 然后,您可以安全地使用timedelta来计算与unix时期的距离,然后转换为秒或毫秒。

Note that the resulting unix timestamp is itself in the UTC timezone. 请注意,生成的unix时间戳本身是UTC时区。 If you wish to see the timestamp in a localized timezone, you will need to make another conversion. 如果您希望在本地化时区中查看时间戳,则需要进行另一次转换。

Also note that this will only work for dates after 1970. 另请注意,这仅适用于1970年以后的日期。

   import datetime
   import pytz

   UNIX_EPOCH = datetime.datetime(1970, 1, 1, 0, 0, tzinfo = pytz.utc)
   def EPOCH(utc_datetime):
      delta = utc_datetime - UNIX_EPOCH
      seconds = delta.total_seconds()
      ms = seconds * 1000
      return ms

#3楼

def in_unix(input):
  start = datetime.datetime(year=1970,month=1,day=1)
  diff = input - start
  return diff.total_seconds()

#4楼

Here's a less broken datetime -based solution to convert from datetime object to posix timestamp: 这是一个不太破旧的基于datetime的解决方案,可以从datetime对象转换为posix时间戳:

future = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
return (future - datetime.datetime(1970, 1, 1)).total_seconds()

See more details at Converting datetime.date to UTC timestamp in Python . 有关详细信息,请参阅在Python中将datetime.date转换为UTC时间戳


#5楼

def expiration_time():
    import datetime,calendar
    timestamp = calendar.timegm(datetime.datetime.now().timetuple())
    returnValue = datetime.timedelta(minutes=5).total_seconds() + timestamp
    return returnValue

#6楼

Now in Python >= 3.3 you can just call the timestamp() method to get the timestamp as a float. 现在在Python> = 3.3中,您可以调用timestamp()方法将时间戳作为浮点数。

import datetime
current_time = datetime.datetime.now(datetime.timezone.utc)
unix_timestamp = current_time.timestamp() # works if Python >= 3.3

unix_timestamp_plus_5_min = unix_timestamp + (5 * 60)  # 5 min * 60 seconds
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值