Skip to content

Instantly share code, notes, and snippets.

@darthwade
Created July 29, 2014 08:50
Show Gist options
  • Save darthwade/6496646a75a924f4cbcd to your computer and use it in GitHub Desktop.
Save darthwade/6496646a75a924f4cbcd to your computer and use it in GitHub Desktop.
>>> # Grab timezones from http://download.geonames.org/export/dump/
>>> DOWNLOAD_URL = 'http://download.geonames.org/export/dump/timeZones.txt'
>>> from urllib2 import urlopen
>>> lines = urlopen(DOWNLOAD_URL).read().split('\n')[1:]
>>> from apps.gis.models import TimeZone, Country
>>> for l in lines:
>>> if not len(l): break
>>> country_code, id, gmt_offset, dst_offset, raw_offset = l.split()
>>> try:
>>> country = Country.objects.get(id=country_code)
>>> defaults = {'country': country, 'gmt_offset': gmt_offset, 'dst_offset': dst_offset, 'raw_offset': raw_offset}
>>> (obj, created) = TimeZone.objects.get_or_create(id=id, defaults=defaults)
>>> for k,v in defaults.items():
>>> setattr(obj, k, v)
>>> obj.save()
>>> print id, obj
>>> except Country.DoesNotExist:
>>> pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment