What time is it? Converting time from UTC to what you want.

What Time Is itTick tock…. time is a real bummer now that Esri implemented UTC time being recorded in the time tracker settings. Now the create date and last edit date are recorded in a time zone that most of us don’t work in. ( I understand why, I just want to grumble a little bit.) So… that opened up a new opportunity to learn about time shuffling in python.

What I wanted to do was write a script that was going to be run every so often. Maybe every 5 minutes, maybe every 30. It needed to check for changed data in an SDE feature class and archive whatever changes had been made.

Originally I wanted the script to figure out what time it was five minutes ago, but in UTC time so that I can query the last edit and last modified date out of a feature class stored in SDE. I did a lot of reading about time formatting. The most straightforward explanation I could find was here: http://www.doughellmann.com/PyMOTW/datetime

The best thing I have to tell you is that there is a built in UTC converter in Python! Hooray!

It works like this –

import datetime

This results in :

datetime.datetime(2013, 3, 27, 6, 19, 22, 43000)

Esri and ArcMap are looking for a formatted date that looks like MM-DD-YYYY HH:MM:SS.sss

so we need to use the string formatting tools for date and time.

import datetime
UTCNow = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

which gives us a nice output like ArcMap is looking for:

‘2013-03-27 06:24:29’

The really great thing about this is that it handles Daylight Savings Time issues for you. I was concerned about that in the beginning because I figured I’d have to remember to redo the math every time daylight savings switched on or off.

The other consideration for subtracting or adding time is that you need to create a “time delta”, which is a time object that can be added or subtracted to times.

So first we make a 5 minute object that can be subtracted from our current UTC time and then subtract it from the current time.

MinutesChange = datetime.timedelta(minutes=5)
FiveMinutesAgo = datetime.datetime.utcnow() - MinutesChange
then = FiveMinutesAgo.strftime("%Y-%m-%d %H:%M:%S")

Voila! perfectly formatted 5 minutes ago in London. Just what we were looking for

You can check you work here: http://www.whattimeisit.com/

Now, taking it to the useful step…

To use it in an expression to select features, you would do this:

expression = "last_edited_date > '" + str(then) + "' OR created_date > '" + str(then) +"'"
arcpy.SelectLayerByAttribute_management("c:/your/path/Your_FeatureClass", "NEW_SELECTION", expression)

The full script is here:

import datetime
UTCNow = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
MinutesChange = datetime.timedelta(minutes=5)
then = datetime.datetime.utcnow() - MinutesChange
print then
print then.strftime("%Y-%m-%d %H:%M:%S")
expression = "last_edited_date > '" + str(then) + "' OR created_date > '" + str(then) +"'"
arcpy.SelectLayerByAttribute_management("c:/your/path/Your_FeatureClass", "NEW_SELECTION", expression)

In the next post I’ll tell you how I came up with a better way to accomplish this idea of checking data since the last time data was checked.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s