Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 15 minutes to current timestamp using timedelta

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.

like image 445
philiporlando Avatar asked Apr 03 '17 19:04

philiporlando


People also ask

How do I add minutes to Timedelta?

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!

How do I convert Timetime to minutes in Timedelta?

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.

What is the use of Timedelta () function?

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.

How do I add hours to Timedelta?

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!


2 Answers

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")
like image 128
Derlin Avatar answered Oct 27 '22 00:10

Derlin


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 #############################
like image 34
Joab Mendes Avatar answered Oct 26 '22 22:10

Joab Mendes