Date and time calculations

2133
5
Jump to solution
01-22-2014 09:25 PM
TimothyElliott
New Contributor III
Just wondering if anyone can tell me why my expression is not working to calculate some values in a table?

Below the !DateTime! and !Time! are fields in a File geodatabase. Basically I need to combine the two fields, where DateTime currently has the correct date for the record and !Time! has a random date and the correct time I need combined with the first field.

 datetime.combine (datetime.strptime ( !DateTime!, %Y %m %d), datetime.strptime (!Time!, %H:%M:%S)) 
Tags (2)
1 Solution

Accepted Solutions
TimothyElliott
New Contributor III
after lots of hunting and a ESRI support ticket later I have found out that


the Field Caclulator and by extension, the Calculate Field geoprocessing tool access the date and time values as strings. In a file geodatabase these are all unicode strings, though you may find they're not in the case of a personal geodatabase. 

We can use datetime.strptime to convert the two strings of text into datetime objects. Then we can access the properties of each of these objects to pull out information about the day / month / year in the case of our field with only the date, in it, or hour / minutes / seconds in the case of our field containing both a date and time. When the Calculate Field goes to fill in the time information in the combined field, it wants this data to be provided as a datetime object though. So this makes our best bet, to take the information we're interested from each of our two existing datetime objects, and create a new datetime object using datetime.datetime(yeah, month, day, hour, minute, second).


Expression: combine_datefields(!Time_!, !DateTime_!) Codeblock: def combine_datefields(time_value, date_value):     t = datetime.datetime.strptime( time_value.encode('utf-8'), "%d/%m/%Y %I:%M:%S %p")      d =  datetime.datetime.strptime( date_value.encode('utf-8'), "%d/%m/%Y")     combined_dt = datetime.datetime(d.year, d.month, d.day, t.hour, t.minute, t.second)     return combined_dt


So as a result the Datetime.combine was not the right tool for the job

View solution in original post

5 Replies
markdenil
Occasional Contributor III
Well, the strptime patterns are strings. They need to be quoted.
as in: "%Y %m %d" and "%H:%M:%S"

Then, argument 2 for combine() must be datetime.time, not datetime.datetime...

... are the !DateTime! and !Time! fields strings, or are they actual date fields in the table?

If the dates are date objects with 00:00:00 time components,
You might be able to add the time field value to the dateTime field value
0 Kudos
TimothyElliott
New Contributor III

... are the !DateTime! and !Time! fields strings, or are they actual date fields in the table?

If the dates are date objects with 00:00:00 time components,
You might be able to add the time field value to the dateTime field value


The fields are both dates DateTime has values: 20/01/2014 and Time has values: 23/01/2014 8:52:03 AM
The problem is that the Time field should have no date in it but part of the export process it gets a date added and is incorrectly given the date the file was processed.

Essentially what I want is a field with 20/01/2014 8:52:03 AM, generated from the two fields. I tried the addition but I can't get it to ignore the date in the Time field.
0 Kudos
markdenil
Occasional Contributor III
take each time object and deconstruct it to its component parts with datetime.timetuple()
>>> import datetime
>>> d = datetime.datetime.now()
>>> d
datetime.datetime(2014, 1, 28, 15, 28, 21, 470000)
>>> tup = d.timetuple()
>>> tup
time.struct_time(tm_year=2014, tm_mon=1, tm_mday=28, tm_hour=15, tm_min=28, tm_sec=21, tm_wday=1, tm_yday=28, tm_isdst=-1)
>>> for t in tup:
 print t
2014
1
28
15
28
21
1
28
-1


assign the date parts from the !Date! field to variables and the time parts from the !Time! field
then assemble a new new date-time object to insert in the desired field
using strptime()
or use datetime.replace to replace the time parts of the original date
0 Kudos
markdenil
Occasional Contributor III
This may illustrate:
###      dateAndHour.py
import datetime

hrObj = datetime.datetime(1999, 02, 06, 05, 40, 29)
dtObj = datetime.datetime(2014, 1, 29, 00, 00, 00)

t = hrObj.timetuple()
d = dtObj.timetuple()

newDate = datetime.datetime(
                            d[0],
                            d[1],
                            d[2],
                            t[3],
                            t[4],
                            t[5])
print hrObj
print dtObj
print newDate

input your own date and time field values.
0 Kudos
TimothyElliott
New Contributor III
after lots of hunting and a ESRI support ticket later I have found out that


the Field Caclulator and by extension, the Calculate Field geoprocessing tool access the date and time values as strings. In a file geodatabase these are all unicode strings, though you may find they're not in the case of a personal geodatabase. 

We can use datetime.strptime to convert the two strings of text into datetime objects. Then we can access the properties of each of these objects to pull out information about the day / month / year in the case of our field with only the date, in it, or hour / minutes / seconds in the case of our field containing both a date and time. When the Calculate Field goes to fill in the time information in the combined field, it wants this data to be provided as a datetime object though. So this makes our best bet, to take the information we're interested from each of our two existing datetime objects, and create a new datetime object using datetime.datetime(yeah, month, day, hour, minute, second).


Expression: combine_datefields(!Time_!, !DateTime_!) Codeblock: def combine_datefields(time_value, date_value):     t = datetime.datetime.strptime( time_value.encode('utf-8'), "%d/%m/%Y %I:%M:%S %p")      d =  datetime.datetime.strptime( date_value.encode('utf-8'), "%d/%m/%Y")     combined_dt = datetime.datetime(d.year, d.month, d.day, t.hour, t.minute, t.second)     return combined_dt


So as a result the Datetime.combine was not the right tool for the job