skip certain fields

2214
6
04-21-2016 09:23 AM
ChadCunningham
New Contributor

I am able to run a script that will calculate the time spent onsite using the datetime field for our service in and out times.  The issue is the script stops (error) when it encounters a datetime field with no time in it (working on making sure that doesn't happen).  I am wondering if there is a quick python addition I can add that when it encounters a datetime field with no time, it will skip over that record and keep running through the records.  The records are a large amount and this is getting time consuming to keep starting the script at the next record after it error'd out on a record with no time in the field.  I've attached a screenshot of an example.

Thanks!

0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor

datetime objects are the worst.  I usually convert them to strings

This is an obviously awful way to check whether there is a time, in the absence of a... 'has_time'... property

>>> d
datetime.datetime(2016, 4, 21, 13, 16, 22, 961326)
>>> ds = str(d)
>>> ds
'2016-04-21 13:16:22.961326'
>>> if " " in ds: print("no time")
no time

I hope you get the drift, and I only recommend it in the absence a correct answer

if " " in str(yourdate): skip

else:  do stuff

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What about:

def fnElapsed(STRDTTM, ENDDTTM):
    from datetime import datetime
    if all((STRDTTM, ENDDTTM))):
        dateStart = datetime.strptime(STRDTTM, "%m/%d/%Y %I:%M:%S %p")
        dateEnd = datetime.strptime(ENDDTTM, "%m/%d/%Y %I:%M:%S %p")
        timeDiff = dateEnd - dateStart
        elapMin = timeDiff.total_seconds()/60
        return elapMin

UPDATE:  To elaborate a little....  Similar to other data types, when a cursor encounters a Date field with no value, i.e., a NULL, it returns a Python NoneType as opposed to a Python datetime.datetime.  The Python all() built-in will return False if one of the fields returns a NoneType, effectively skipping those records without a start and end date.

DarrenWiens2
MVP Honored Contributor

You can check if the first format throws an error, and fix it if so. Also, you need to return the value in the end.

from datetime import datetime
def fnElapsed(STRDTTM, ENDDTTM):
  try:
    dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
  except:
    dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y")
  try:
    dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
  except:
    dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y")
  timeDiff = dateEnd - dateStart
  return timeDiff.total_seconds()/60

note: my date formats are slightly different than yours.

DarrenWiens2
MVP Honored Contributor

Similar to above, you can return a dummy value instead of changing the datetime format to flag your records.

from datetime import datetime
def fnElapsed(STRDTTM, ENDDTTM):
  try:
    dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
  except:
    return 9999
  try:
    dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
  except:
    return -9999
  timeDiff = dateEnd - dateStart
  return timeDiff.total_seconds()/60
0 Kudos
PeterWilson
Regular Contributor

Hi Chad

The following will deal with the blanks within your date fields:

def fnElapsed(STRDTTM, ENDDTTM):
  from datetime import datetime
  try:
    dateStart  = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
    dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
    timeDiff = dateEnd - dateStart
    elapMin = timeDiff.total_seconds()/60
    return elapMin
  except Exception as inst:
    print type(inst)

fnElapsed(!STRDTTM!, !ENDDTTM!)

PythonCodeBlock.png

curtvprice
MVP Esteemed Contributor

In answer to your original question:

I am wondering if there is a quick python addition I can add that when it encounters a datetime field with no time, it will skip over that record and keep running through the records.

def fnElapsed(STRDTTM, ENDDTTM): 
  from datetime import datetime
  try:
    dateStart = datetime.strptime(STRDTTM, "%m/%d/%Y %I:%M:%S %p") 
    dateEnd = datetime.strptime(ENDDTTM, "%m/%d/%Y %I:%M:%S %p") 
    timeDiff = dateEnd - dateStart 
    elapMin = timeDiff.total_seconds()/60 
    return elapMin
  except:
    pass