Comparing Time in a Python List/SearchCursor

09-09-2015 09:11 AM
New Contributor II

Hi All,

I have created a script that does some data checks and I am down to my last check. Where I am stuck at is comparing time Values between rows in a Search Cursor.

for example I will have several tables that have 2-10 values in them.

My code looks something like this:

table = 'b3514str133'

field = "INP_DATE"

tempList = []

with arcpy.da.SearchCursor(table, field) as cursor:

     for row in cursor:



# My Templist now looks like this : [(datetime.datetime(2015, 4, 1, 14, 22, 4),),

                                     (datetime.datetime(2015, 4, 1, 14, 22, 10),),

                                     (datetime.datetime(2015, 4, 1, 14, 22, 15),),

                                     (datetime.datetime(2015, 4, 1, 14, 22, 20),),

                                     (datetime.datetime(2015, 4, 1, 14, 22, 37),)]


          for item in tempList:

               if any of the items are within 1 hour of each other:

                    print row

My first question is: Is this the easiest way to do this?

My Second question is: Should I be using a list to do this or can i compare each row by all the other rows?

My third question is: What if statement could i used to compare each row within 1 hour of each other?

My checks go through about 5 different levels of verification so my mind is currently blown!!!!

Any help would be great thanks!


0 Kudos
2 Replies
Esri Regular Contributor


You have arcpy.time.EsriTimeDelta at your disposal, make deltas for each datetime in your list and test for < and >.


0 Kudos
MVP Esteemed Contributor

Since your comparison interval isn't weeks, months, or greater, I don't think EsriTimeDelta is necessary in this case, but you could go that route if the interval may get that large.

If you simply want to compare a row's datetime with the previous row's datetime, something like this should work:

table = 'b3514str133' 
date_field = "INP_DATE" 
interval = datetime.time(1)  #specify 1-hour interval

with arcpy.da.SearchCursor(table, date_field) as cursor:
    previous, = next(cursor)
    for dt, in cursor:
        if dt - previous <= interval:
            print row
        previous = dt

If you want to compare a row's datetime with all previous rows' datetimes, or all rows' datetimes, then the situation gets more involve