Skip to content

Instantly share code, notes, and snippets.

@jethac
Created February 4, 2015 05:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jethac/ee03aff52176fb7cc9fe to your computer and use it in GitHub Desktop.
Save jethac/ee03aff52176fb7cc9fe to your computer and use it in GitHub Desktop.
Olson timezone retriever
# olsongrabber.py - Olson timezone retriever
# Grabs the latest timezone information from the IANA and concatenates files together
# for use with Moodle, see:
# https://docs.moodle.org/dev/index.php?title=Timezone_information
import os
import shutil
from ftplib import FTP
import tarfile
filename = '';
foldertarget = 'temp';
def mahfilename(line):
global filename
if (len(filename) == 0):
tokens = line.split(' ')
filename = tokens[-1]
# connect to the IANA's ftp server
ianaftp = FTP('ftp.iana.org')
ianaftp.login()
ianaftp.cwd('/tz/releases/')
# identify the newest data file
ianaftp.retrlines('LIST -t tzdata*.tar.gz', mahfilename)
filename_data = filename;
filebasename_data = os.path.splitext(os.path.splitext(filename)[0])[0]
filename_out = '%s-concatenated.txt' % filebasename_data;
# kill existing local files
if (os.path.exists(filename_out)):
os.remove(filename_out)
if (os.path.exists(filename_data)):
os.remove(filename_data)
# grab the latest timezone data release
print ('Latest tz database file: %s' % filebasename_data)
ianaftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
# leave the IANA's server
ianaftp.quit()
# untar the file
if (os.path.exists(foldertarget)):
shutil.rmtree(foldertarget)
os.mkdir(foldertarget)
dfnfile = tarfile.open(filename_data, mode='r:gz')
dfnfile.extractall(foldertarget)
# concatenate the source files
sources = ['africa', 'antarctica', 'asia', 'australasia', 'europe', 'northamerica', 'southamerica', 'etcetera']
with open(filename_out, 'w') as outfile:
for source in sources:
with open('%s/%s' % (foldertarget, source)) as infile:
for line in infile:
outfile.write(line)
# clean up afterwards
os.remove(filename_data)
shutil.rmtree(foldertarget)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment