0

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

2
  • What is ceil_dt? Commented Aug 8, 2020 at 22:30
  • My bad, it's a function that i forgot to include Commented Aug 8, 2020 at 22:31

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.