I have the following code:
import datetime
from datetime import datetime as dt
def ceil_dt(dt, delta):
return dt + (dt.min - dt) % delta
NextInterval5m = ceil_dt(now, timedelta(minutes=5))
unixtime5m = dt.fromtimestamp(NextInterval5m)
The problem is that i keep getting the following error:
TypeError: an integer is required (got type datetime.datetime)
Can someone help me out on this? I don't understand to what i am supposed to convert NextInterval5m
in order to make it work. I'm trying to convert NextInterval5m
to an Unix timestamp
You should be able to convert it into a unix timestamp by using .timestamp()
on a datetime.datetime
object. However, this function is exclusive to Python 3. If you need something for python 2, you can use .total_seconds()
which requires a datetime.time_delta
object instead.
Documentation: https://docs.python.org/3.8/library/datetime.html#datetime.datetime.timestamp
If you are using python 3.3+, use .timestamp()
import datetime
from datetime import datetime as dt
from datetime import timedelta
def ceil_dt(dt, delta):
return dt + (dt.min - dt) % delta
now = dt.now()
NextInterval5m = ceil_dt(now, timedelta(minutes=5))
unixtime5m = NextInterval5m.timestamp()
print(unixtime5m)
Output:
1596926400.0
OR
import datetime
from datetime import datetime as dt
from datetime import timedelta
def ceil_dt(dt, delta):
return dt + (dt.min - dt) % delta
now = dt.now()
NextInterval5m = ceil_dt(now, timedelta(minutes=5))
unixtime5m = NextInterval5m.timestamp()
print((NextInterval5m - datetime.datetime(1970,1,1)).total_seconds())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.