使用if函数两次不同

时间:2019-07-05 18:32:18

标签: python mysql datetime

我将比较数据集服务器时间和实际时间。如果相差大于1分钟,则继续执行脚本。否则等待一个循环,例如10秒。将开始。

但是我不能执行if函数。感谢您的帮助!

这是输出:

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

错误:

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

代码:

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

1 个答案:

答案 0 :(得分:1)

您必须将timedelta对象与另一个timedelta对象进行比较。

FluentValidation.ValidationException: Validation failed: 
 -- Code: 'Code' must not be empty when Customer is in 'Existing' status.