-1

I will compare a dataset server time and the actual time. If the different greater than 1 minute, the script is continued. Else a waiting loop for example 10 sec. will start.

But I can't execute the if function. Thanks for your help!

That's the output:

last importtime: 2019-07-05 14:16:07
actual time:  2019-07-05 18:30:21
1 day, 4:14:14

Error:

TypeError: '>' not supported between instances of 'datetime.timedelta' and 'datetime.datetime'

Code:

from datetime import datetime
import mysql.connector
import pandas as pd

#db-connection#
mydb = mysql.connector.connect(host="localhost",port="xx",user="xx",passwd="xxx",database="traiding")
mycursor = mydb.cursor()
mycursor.execute("select max(imported) from import_log")

data_sql=mycursor.fetchall()

#last import from database'
data=pd.DataFrame(data_sql, columns=['date'])


#close connection#
mycursor.close()
mydb.close()


#last import date#
lastimported=datetime.strftime(data_sql[0][0], "%Y-%m-%d %H:%M:%S")
print("last importtime:",lastimported)

#lastimport=datetime.strptime(lastimported, "%Y-%m-%d %H:%M:%S")


current_time=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

print ("actual time: ", current_time)


s1 = lastimported
s2 = current_time
FMT = '%Y-%m-%d %H:%M:%S'

tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
print (tdelta)

min_time=datetime.strptime('00:01:00', "%H:%M:%S")

if tdelta > min_time :
    print (">0") # Do Something
else:
    print ("else") # Waiting loop, for example 10sec
chries8
  • 7
  • 2
  • 1
    `'datetime.timedelta' and 'datetime.datetime'` the error tells you that you're not trying to get the difference between two times, you're trying to compare a time-difference with an actual datetime. – roganjosh Jul 05 '19 at 18:35
  • Why are you converting datetime objects to strings and then back to datetime objects again? `current_time=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")`, to then do `datetime.strptime(s2, FMT)` where `s2` is `current_time` which was _already_ a datetime object if you hadn't called `strftime` at the end – roganjosh Jul 05 '19 at 18:38
  • In any case, I think the only thing that's throwing me here is the hard-coded value for `s1`. If you could clarify why that is being used then I kinda get what you're trying to do and will try answer – roganjosh Jul 05 '19 at 18:43
  • Possible duplicate of [Comparing a time delta in python](https://stackoverflow.com/questions/2591845/comparing-a-time-delta-in-python) – Akaisteph7 Jul 05 '19 at 18:44
  • @Akaisteph7 you answer and flag as a dupe within less than 1 minute? – roganjosh Jul 05 '19 at 18:46
  • oh sorry, my mistake. I'm tested a lot of possibilities. The correct s1 = lastimported. I've changed in the code above. – chries8 Jul 05 '19 at 18:51
  • @roganjosh: When I used strptime, i can't calculate the different between s1 and s2. I don't no why, but does't work! It works only with strftime and than back to strptime ... – chries8 Jul 05 '19 at 19:03
  • That's an XY problem. You're misdiagnosing the issue – roganjosh Jul 05 '19 at 19:15

1 Answers1

1

You have to compare the timedelta object to another timedelta object.

import datetime as dt

#... Your code

if tdelta > dt.timedelta(minutes=1):
    print (">0") # Do Something
else:
    print ("else") # Waiting loop, for example 10sec
Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
  • It is a part of a bigger script. This part will control the import part to get data from a api. Like a cronjob with adjustable set of parameters. I will look on my last import timestamp (s1) and compare this with the actual time (s2). If the different greater than 1 minute, than the script will be start. Do you understand that? – chries8 Jul 05 '19 at 19:06
  • @roganjosh What is the issue with this code? I am not sure I understand how this does not solve exactly what you are asking for – Akaisteph7 Jul 05 '19 at 19:17