title: Forcing Google Cloud's Datastore datetimes to default to UTC
body: <p>According to the datastore <a href="https://cloud.google.com/appengine/docs/standard/python/datastore/typesandpropertyclasses#DateTimeProperty" rel="nofollow noreferrer">docs</a>, datetimes are always stored and returned in UTC.</p>
  
  <p>However, in the cloud dashboard, all my datetimes are displayed in <code>CEST</code>, i.e. <code>UTC+2</code>.</p>
  
  <p>When saving the entities, I do not include any timezone and use <code>datetime.utcnow()</code>.</p>
  
  <p>To be more specific, here is my <code>TimeZonedDateTimeProperty</code> model, extending/partially overriding google's own <code>db.DateTimeProperty</code>:</p>
  
  <pre><code>class TimeZonedDateTimeProperty(db.DateTimeProperty):
      def __init__(self, *args, **kwargs):
          super(TimeZonedDateTimeProperty, self).__init__(*args, **kwargs)
  
      def __get__(self, model_instance, model_class):
          """Returns the value for this property on the given model instance."""
  
          if model_instance is None:
              return self
  
          attr_to_return = None
  
          try:
              # access the db.DateTimeProperty's getter
  
              attr_to_return = getattr(model_instance, super(TimeZonedDateTimeProperty, self)._attr_name())
          except AttributeError:
              attr_to_return = None
  
          if attr_to_return:
              # super methods of db.DateTimeProperty will be calling this 
              # getter too and we don't wanna mess up
              # the UTC convention when working with datetimes
  
              includes_correct_caller = None
  
              try:
                  # after noticing that this getter is also being called
                  # outside of user requests, inspect the caller and decide
                  # whether to offset the UTC datetime or not 
  
                  includes_correct_caller = "main_class_name" in str(inspect.stack()[1][0].f_locals["self"].__class__)
              except Exception:
                  includes_correct_caller = False
  
              if includes_correct_caller:
                  return attr_to_return + relativedelta(hours=+2)
              else:
                  return attr_to_return
          else:
              return None
  </code></pre>
  
  <p>And here's an example usage in a subclassed <code>db.Model</code>:</p>
  
  <pre><code>class Message(db.Model):
      ...
      sent = TimeZonedDateTimeProperty()
      ...
  
  
  m = Message(...
              sent=datetime.utcnow(),
              ...)
  m.put()
  </code></pre>
  
  <p>Now, if I call a <code>m.sent</code> running on localhost, things go as planned, i.e. the <code>sent</code> property is saved in UTC and the getter returns an offset datetime.</p>
  
  <p>In the live/production environment, the properties are <strong>not</strong> being saved in UTC and, consequently, the overridden getter unnecessarily adds 2 hours.</p>
  
  <p>So:</p>
  
  <ul>
  <li><p>How can I force the live datastore to always use UTC when saving and serving my datetimes? </p></li>
  <li><p>Is my approach correct? Assuming it is, is there a more elegant way to handle timezoned datetimes in GAE?</p></li>
  </ul>
  
  <p>Thanks!</p>