Python Create Unix Timestamp Five Minutes in the Future

Python Create unix timestamp five minutes in the future

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 calculating Unix timestamp difference in Minutes from a given epoch value

it is probably best to use time.time() here [docs] - since this gives you seconds since the epoch, you can directly calculate the desired delta in minutes as (time.time() - timestamp)/60.

Timeit comparison vs. getting the current timestamp asdatetime.datetime.utcnow():

import time
from datetime import datetime

%timeit (time.time() - 1512082800) / 60
221 ns ± 10.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit (datetime.utcnow().timestamp() - 1512082800) / 60
848 ns ± 14.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Python unix timestamp not converting but seems value

with this timestamp i get 08/16/50251 @ 12:00am (UTC) from https://www.unixtimestamp.com, and error that is given says that year 50251 is not in datetime

Converting TimeStamp Series into minutes Series data || Python

  • First, make sure the dtype of the Series (df column in question) is datetime
  • now subtract the date of your choice, e.g. first entry of Series (which you can obtain by normalize())
  • take the total_seconds of resulting timedelta Series and divide by 60 to get minutes (you could also floor-divide // if you don't want the fractional minutes)

Ex:

import pandas as pd

s = pd.Series(pd.to_datetime(["2021-02-01 06:00:00",
"2021-02-01 09:00:00",
"2021-02-02 01:00:00"]))

minutes = (s-s.iloc[0].normalize()).dt.total_seconds()/60

minutes
0 360.0
1 540.0
2 1500.0 # next day -> 1440 + time of current day
dtype: float64

compute a timestamps based on 13-digit unixtime timestamp in ms in python

13-digit Unix-time is already in millisecond, so if you want to get 300 milliseconds or after, you only need to:

X = "1396226255964"
Y1 = int(X) - 300
print("Y1:", Y1)
Y2 = int(X) + 300
print("Y2:", Y2)

This is a bit unusual, X is usually supposed to be integer.



Related Topics



Leave a reply



Submit