14

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.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
philiporlando
  • 941
  • 4
  • 19
  • 31
  • 1
    As an aside to your main question, there is no need to mix `time` and `datetime` modules here. Just stick to `datetime` – roganjosh Apr 03 '17 at 19:26

4 Answers4

9

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")
Derlin
  • 9,572
  • 2
  • 32
  • 53
4

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 #############################
Joabe da Luz
  • 1,030
  • 2
  • 18
  • 32
1

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.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • I was close, I just figured it out! Glad to see another way of doing it too. Thanks! – philiporlando Apr 03 '17 at 19:26
  • @spacedSparking as I saw, your answer appeared a second after mine, glad you fixed it :) As you found, you can also call `strftime` method on `nextTime` without the need for `dt.datetime.strftime()`. – roganjosh Apr 03 '17 at 19:27
0

As soon as I asked this question I found out what I was doing wrong...

For those who are curious, here is the solution:

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(nextTime.strftime("%Y-%m-%d %H:%M:%S"))
    print "############################ DONE #############################"
    time.sleep(900) ## call the api every 15 minutes
philiporlando
  • 941
  • 4
  • 19
  • 31