1. http://takashi-matsuo.blogspot.com/2008/07/using-zipped-pytz-on-gae.html
It works well with older versions of pytz. Stefano pointed out that my method doesn't work with the newest pytz distribution. This article describes how to use the newest zipped pytz on GAE.
First, retrieve the newest pytz from pypi[2] and extract the archive.
2. http://pypi.python.org/pypi/pytz/
$ tar xjf pytz-2008c.tar.bz2
$ cd pytz-2008c/pytz
$ zip -q zoneinfo.zip `find zoneinfo -type f ! -name '*.pyc' -print`
$ rm -rf zoneinfo
After that, you have to edit pytz/__init__.py and modify open_resource function to use zipped zoneinfo database like following:
(pkg_resources stuff were struck out. Thank you again Stefano!)
def open_resource(name):
"""Open a resource from the zoneinfo subdir for reading.
Uses the pkg_resources module if available.
"""
import zipfile
from cStringIO import StringIO
if resource_stream is not None:
return resource_stream(__name__, 'zoneinfo/' + name)
else:
name_parts = name.lstrip('/').split('/')
for part in name_parts:
if part == os.path.pardir or os.path.sep in part:
raise ValueError('Bad path segment: %r' % part)
zoneinfo = zipfile.ZipFile(os.path.join(os.path.dirname(__file__),
'zoneinfo.zip'))
return StringIO(zoneinfo.read(os.path.join('zoneinfo', *name_parts)))
Then, the only thing to do is to copying your original pytz directory into your application directory.
$ cd ../
$ cp -r pytz /your/application/directory
If you'd like to avoid using CPU to unzip zoneinfo data every time, perhaps you could use following function:
from google.appengine.api import memcache
import logging
import pytz
from pytz import timezone
def getTimezone(tzname):
try:
tz = memcache.get("tz:%s" % tzname)
except:
tz = None
logging.debug("timezone get failed: %s" % tzname)
if tz is None:
tz = timezone(tzname)
memcache.add("tz:%s" % tzname, tz, 86400)
logging.debug("timezone memcache added: %s" % tzname)
else:
logging.debug("timezone memcache hit: %s" % tzname)
return tz
Happy coding :-)