Today while coding my first Facebook application running on Google App Engine I was getting odd errors related to time from the datastore.

The problem was that Google App Engine’s time is always running from Coordinated Universal Time (UTC), even when you use something like:

1
2
import datetime
datetime.datetime.now()

How you should and I have compensate for this is taking the difference of the time you want to UTC time in order to shift the time to the time zone of interest:

1
2
3
4
5
import datetime
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 5) # for East Coast United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 6) # for Central United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 7) # for Rocky Mountains United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 8) # for West Coast United States

This will give you the correct time in your time zone

Be Sociable, Share!
Tagged with:  

One Response to Google App Engine Python Time and Datetime Confusion

  1. Ben says:

    thanks, this cleared my headache up!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>