The title says it all. I am writing a script to make scheduled GET requests to an API. I want to print when the next API call will be made, which would be 15 minutes from the previous call.
I'm very close, but have been running into the following error: TypeError: a float is required
Here's my code:
import time, datetime
from datetime import datetime, timedelta
while True:
## create a timestamp for the present moment:
currentTime = datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")
print "GET request @ " + str(currentTime)
## create a timestamp for 15 minutes into the future:
nextTime = datetime.datetime.now() + datetime.timedelta(minutes = 15)
print "Next request @ " + str(datetime.datetime.fromtimestamp(nextTime).strftime("%Y-%m-%d %H:%M:%S")
print "############################ DONE #############################"
time.sleep(900) ## call the api every 15 minutes
I can get things to work (sort of) when changing the following line:
print "Next request @ " + str(nextTime)
However, this prints a timestamp with six decimal places for milliseconds. I want to keep things in the %Y-%m-%d %H:%M:%S
format.
Use the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10) . The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime. Copied!
The timedelta class stores the difference between two datetime objects. To find the difference between two dates in form of minutes, the attribute seconds of timedelta object can be used which can be further divided by 60 to convert to minutes.
Python timedelta() function is present under datetime library which is generally used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations.
Use the timedelta() class from the datetime module to add hours to datetime, e.g. result = dt + timedelta(hours=10) . The timedelta class can be passed a hours argument and adds the specified number of hours to the datetime. Copied!
You don't need to use datetime.fromtimestamp
since nextTime
is already a datetime object (and not a float). So, simply use:
nextTime = datetime.datetime.now() + datetime.timedelta(minutes = 15)
print "Next request @ " + nextTime.strftime("%Y-%m-%d %H:%M:%S")
You can achieve it just by using timestamp instead:
import time
from datetime import datetime
while True:
# create a timestamp for the present moment:
currentTime_timestamp = time.time()
currentTime = datetime.fromtimestamp(
currentTime_timestamp
).strftime("%Y-%m-%d %H:%M:%S")
print "GET request @ " + str(currentTime)
# create a timestamp for 15 minutes into the future:
nextTime = currentTime_timestamp + 900 # 15min = 900 seconds
print "Next request @ " + str(datetime.fromtimestamp(
nextTime
).strftime("%Y-%m-%d %H:%M:%S"))
print "############################ DONE #############################"
time.sleep(900) # call the api every 15 minutes
The output I got was:
GET request @ 2017-04-03 16:31:34
Next request @ 2017-04-03 16:46:34
############################ DONE #############################
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With