47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
# Generates fourTwentyTz.js from time zone csv files
|
|
# get latest files from https://timezonedb.com/download
|
|
import csv,json,time,os,math
|
|
countries = {}
|
|
for r in csv.reader(open("country.csv")):
|
|
countries[r[0]] = r[1]
|
|
zones = {}
|
|
for r in csv.reader(open("zone.csv")):
|
|
parts = r[2].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 searches like "Anguilla, Anguilla"
|
|
country = r[1] # Use code instead
|
|
zones[int(r[0])] = {"name":', '.join((city,country))}
|
|
now = int(time.time())
|
|
for r in csv.reader(open("timezone.csv")):
|
|
code = int(r[0])
|
|
if code not in zones:
|
|
continue
|
|
starttime = int(r[2] or "0") # Bugger. They're feeding us blanks for UTC now
|
|
offs = int(r[3])
|
|
if offs < 0:
|
|
offs += 60*60*24
|
|
d = zones[code]
|
|
if starttime<now and ("starttime" not in d or d["starttime"]<starttime):
|
|
d["starttime"] = starttime
|
|
d["offs"] = math.floor(offs/60)
|
|
offsdict = {}
|
|
for k in zones:
|
|
d = zones[k]
|
|
if d["offs"]%60: # One a dem funky timezone. Ignore.
|
|
continue
|
|
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("fourTwentyTz.js","w")
|
|
js.write("// Generated by mk420tz.py - see https://github.com/thedod/BangleApps/420clock\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("zone.csv").st_mtime)))
|
|
js.write("exports.timezones = ")
|
|
json.dump(res,js,indent=1)
|
|
js.write(";\n")
|
|
js.close()
|