How to take a picture of Unix timestamp in 5 minutes

Asked 1 years ago, Updated 1 years ago, 101 views

Based on now, we are creating a expires() function that returns Unix timestamp in 5 minutes. I didn't know what to do, so I made it like C++ Is there a more Python way? Please help me if you know the module and function related to it.

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

python datetime unix-timestamp

2022-09-21 17:05

1 Answers

import datetime
def expires():
    current_time = datetime.datetime.now(datetime.timezone.utc)
    unix_timestamp = current_time.timestamp() # Only supported on Python 3.3 and later
    unix_timestamp_plus_5_min = unix_timestamp+ (5 * 60) # 5 minutes * 60 seconds

*Take time tuple as a factor and return the corresponding POSIX timestamp value to int type.

import calendar
import datetime

def expires():
    future = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
    calendar.timegm(future.timetuple())
import time
def expires():
    return int(time.time()+300) #Type conversion of 300(5 minutes*60 seconds) from the current time to int


2022-09-21 17:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.