Google App Engine Sydney timezone support

Tech Notes

To get datetime working well in Python I've always found a struggle. In Google App Engine if you don't want to copy in a whole lot of libraries you need to create your own time zone support. Here are code samples for Australian Eastern Standard Time (AEST).

from datetime import datetime, timedelta, tzinfo

class AEastern_tzinfo(tzinfo):
"""Implementation of the Canberra/Melbourne/Sydney timezone."""
def utcoffset(self, dt):
return timedelta(hours=+10) + self.dst(dt)

def _FirstSunday(self, dt):
"""First Sunday on or after dt."""
return dt + timedelta(days=(6-dt.weekday()))

def dst(self, dt):
# 2am on the first Sunday in October
dst_start = self._FirstSunday(datetime(dt.year, 10, 1, 2))
# 2 am on the first Sunday in April
dst_end = self._FirstSunday(datetime(dt.year, 4, 1, 2))

if dst_start <= dt.replace(tzinfo=None) < dst_end:
return timedelta(hours=1)
return timedelta(hours=0)
def tzname(self, dt):
if self.dst(dt) == timedelta(hours=0):
return "AEST"
return "AEDT"

And use it like this print time