我有一个Python应用程序,我正在记录数据库中try块的成功和失败。我最初在我的脚本中使用dateime.timedelta
来计算datetime.datetime.now减去5分钟。我稍后使用datetime.datetime.strptime(last_time,“%m /%d /%Y%H:%M:%S%p”)将unicode时间戳转换为日期。最后,我想比较一下我的last_run和now_minus_5变量。我的剧本如下;
我没有收到条件语句的输出,它应该是真的。
Start = datetime.datetime.now()
i = 5
i2= 10
now_minus_5 = Start - datetime.timedelta(minutes =i)
now_minus_10 = Start - datetime.timedelta(minutes =i2)
order_fld = "Time"
return_flds = ["Time", "SUCCESS"]
where_str = """Time >= DATEADD(minute, -5, GETDATE()) AND SUCCESS = 'NO'"""
sql_clause = (None,'ORDER BY {} DESC'.format(order_fld))
last_row = ''
with arcpy.da.SearchCursor(aTable, return_flds, where_clause=where_str, sql_clause=sql_clause) as cursor:
last_row = cursor.next()
last_time = last_row[0]
last_run = datetime.datetime.strptime(last_time,"%m/%d/%Y %H:%M:%S %p")
last_success = last_row[1]
print last_run
print last_success
if last_run >= now_minus_5:
print "true"`
答案 0 :(得分:2)
您看到的问题是由于strptime()
的输入不正确。
引用Python docs on strftime and strptime behavior,%p
的注释列引用注释1和2.特定于您的情况的是注释2,“当与strptime()
方法一起使用时,{{如果%p
指令用于解析小时,则1}}指令仅影响输出小时字段。“
更新strptime调用可修复无效行为:
%I