Python Create unix timestamp five minutes in the future Python Create unix timestamp five minutes in the future python python

Python Create unix timestamp five minutes in the future


Another way is to use calendar.timegm:

future = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)return calendar.timegm(future.timetuple())

It's also more portable than %s flag to strftime (which doesn't work on Windows).


Now in Python >= 3.3 you can just call the timestamp() method to get the timestamp as a float.

import datetimecurrent_time = datetime.datetime.now(datetime.timezone.utc)unix_timestamp = current_time.timestamp() # works if Python >= 3.3unix_timestamp_plus_5_min = unix_timestamp + (5 * 60)  # 5 min * 60 seconds


Just found this, and its even shorter.

import timedef expires():    '''return a UNIX style timestamp representing 5 minutes from now'''    return int(time.time()+300)