I am trying to calculate (using Arcpy) the time difference between two fields that are in string data type and they have the following time format "YYY-MM-DD HH:mm:SS.sss"
I have achieved that between two fields with data type "Datetime" with the following script,
import arcpy
from arcpy import da
import datetime
in_table = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)
#define table into dictionary
tf = datetime.datetime.strptime([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeFinal"])], "%Y-%m%-%d %H:%M:%S.%f")
ti = datetime.datetime.strptime([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeInitial"])], "%Y-%m%-%d %H:%M:%S.%f")
#set firstRow variable to zero
counter = 1
index = 0
with arcpy.da.UpdateCursor(in_table, ["TimeDiff"]) as cursor:
for row in cursor:
if index<=len(ti)-2:
n0 = ti[index]
n1 = tf[index+1]
if n1 >= n0:
row[0] = int((n1 - n0).total_seconds())
else:
counter+=1
cursor.updateRow(row)
index+=1
else:
cursor.updateRow(row)
break
with arcpy.da.UpdateCursor(in_table, ["TimeDiff"]) as cursor:
for row in cursor:
if row[0] == None:
cursor.deleteRow()
else:
cursor.updateRow(row)
arcpy.CopyRows_management(in_table, out_table)
When I try to read the fields using update cursor using datetime.strptime the script send the error "strptime() argument 1 must be str, not list", does anyone know another approach to calculate the time difference in seconds or microseconds?