Time zones in Google App Engine

Use this to try out time zone handling in the Google App Engine runtime and datastore. See the DateTimeProperty docs for details. You can also download this app's source.

The runtime's TZ environment variable is set to UTC, and can't be changed. Timestamps returned by e.g. time.time() and datetime.datetime.now() will always be in UTC. Similarly, datetime properties in the datastore will always be stored and returned as UTC.

You can change the time zone of a datetime in memory with the astimezone() method. If datetime's tzinfo member isn't set, you'll first need to set it to a UTC tzinfo with the replace() method.

The third-party pytz module has a comprehensive set of tzinfo classes. If your app has complex time zone needs, consider including it with your app's code. It's heavyweight, though - over 500 files! - so if you have simpler needs, consider writing just the few tzinfo classes you need by hand, following the spec.

In the table below, if a datetime has a +HH:MM or -HH:MM suffix, e.g. +00:00 for UTC, that means it has a tzinfo (an explicit time zone) attached. Also, assume utc is a tzinfo for the UTC time zone, and assume the Foo class has this definition:

class Foo(db.Model):
  timestamp = db.DateTimeProperty(auto_now=True)

os.environ['TZ'] UTC
time.time() 1669455279.01
datetime.now() 2022-11-26 09:34:39.009561
Foo().put().timestamp 2022-11-26 09:34:39.009561
Raw timestamp in datastore 1669455279.01
datetime.fromtimestamp(raw_timestamp, utc) 2022-11-26 09:34:39.009561+00:00
Foo.get(foo_key).timestamp 2022-11-26 09:34:39.009561
no translation N/A

Translate to
Translate with