python - Trouble comparing datetime objects -


i have python application logging successes , fails of try block in database. use dateime.timedelta in script calculate datetime.datetime.now minus 5 minutes. later use datetime.datetime.strptime(last_time, "%m/%d/%y %h:%m:%s %p") convert unicode timestamp date. lastly, compare last_run , now_minus_5 variables. script below;

i not receive output conditional statement should true.

start = datetime.datetime.now()     = 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()) , success = 'no'"""         sql_clause = (none,'order {} desc'.format(order_fld))         last_row = ''         arcpy.da.searchcursor(atable, return_flds, where_clause=where_str, sql_clause=sql_clause) 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"` 

the issue you're seeing because of incorrect inputs strptime().

referencing python docs on strftime , strptime behavior, notes column %p refers notes 1 , 2. specific situation note 2, "when used strptime() method, %p directive affects output hour field if %i directive used parse hour."

updating strptime call fixes invalid behavior:

>>> import datetime >>> last_time = u'4/24/2015 4:34:10 pm' >>> last_run = datetime.datetime.strptime(last_time,"%m/%d/%y %h:%m:%s %p") >>> last_run datetime.datetime(2015, 4, 24, 4, 34, 10) >>> last_run = datetime.datetime.strptime(last_time,"%m/%d/%y %i:%m:%s %p") >>> last_run datetime.datetime(2015, 4, 24, 16, 34, 10) 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -