-
Notifications
You must be signed in to change notification settings - Fork 1
/
mk420tz.py
43 lines (43 loc) · 1.66 KB
/
mk420tz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Generates tz.js[on] from time zone csv files
# get latest files from https://timezonedb.com/download
import csv,json,time,os
countries = {}
for r in csv.reader(open("country.csv")):
countries[r[0]] = r[1]
zones = {}
now = int(time.time())
for r in csv.reader(open("time_zone.csv")):
parts = r[0].replace('_',' ').split('/')
city = parts[-1]
if len(parts)>2: # e.g. America/North_Dakota/New_Salem
country = parts[1] # e.g. North Dakota
else: # e.g. America/Denver
country = countries[r[1]] # e.g. United States
if country==city: # Avoid awkward phrases like "Anguilla, Anguilla"
country = r[1] # Use code instead
d = zones.get(r[0], {"name":', '.join((city,country))})
starttime = int(r[3] or "0")
offs = int(r[4])
if offs < 0:
offs += 60*60*24
if starttime<now and ("starttime" not in d or d["starttime"]<starttime):
d["starttime"] = starttime
d["offs"] = offs
zones[r[0]] = d
offsdict = {}
for k in zones:
d = zones[k]
if d["offs"]%3600: # One a dem funky time zones
continue # Let's not confuse the user :)
offsdict[d["offs"]] = offsdict.get(d["offs"],[])+[d["name"]]
res = sorted([[k,sorted(offsdict[k])] for k in offsdict],key=lambda x:-x[0])
js = open("tz.js","w")
js.write("// Generated by mk420tz.py - see https://github.com/thedod/global420\n")
js.write("// (version: {0})\n".format(time.ctime(time.time())))
js.write("// Data source: https://timezonedb.com/files/timezonedb.csv.zip\n")
js.write("// (version: {0})\n".format(time.ctime(os.stat("time_zone.csv").st_mtime)))
js.write("window.timezones = ")
json.dump(res,js,indent=1)
js.write(";\n")
js.close()
json.dump(res,open('tz.json','w'),indent=1)