Ignacio Torres Masdeu


App Engine timezone woes

After dealing with timezone support in Google App Engine (GAE) I have the content engine in a state I feel comfy enough to start writing entries with it.

I imported all my past entries and noticed that they were recorded into the datastore without timezone information. I checked the Google App Engine documentation and found that it was my fault for not converting them to UTC.

I then decided to have all my timestamps to be UTC so I tried converting them using “astimezone”. And the datastore answered “no cookie for you”.

The thing is that when you persist a datetime object there are two options, as I learned in the article Support Timezones in Google App Engine by Ben Davies:

Without the tzinfo attribute you cannot use the astimezone() method. This is a registered GAE bug. What next?

from datetime import datetime, timedelta, tzinfo
from google.appengine.ext import webapp
from pytz.gae import pytz

register = webapp.template.create_template_register()
utc = pytz.timezone('UTC')

@register.filter
def tolocal(date,tz="CET"):
    local = pytz.timezone(tz)
    return utc.localize(date).astimezone(local)

@register.filter
def isodatetime(date):
    return utc.localize(date).isoformat()

So now my times are correct. And my AJAX administration zone is ugly but functional. Anybody wants a custom CMS for their Google Apps domain?