Add 15 minutes to current timestamp using timedelta
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 #############################
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")
Close. There is no need to use fromtimestamp
here. You can reduce the last couple of lines to:
import datetime as dt
nextTime = dt.datetime.now() + dt.timedelta(minutes = 15)
print "Next request @ " + dt.datetime.strftime(nextTime, "%Y-%m-%d %H:%M:%S")
In other words, pass the datetime
object nextTime
as the first parameter to strftime
.