-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosure-tz.py
71 lines (54 loc) · 1.53 KB
/
closure-tz.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import datetime
from pprint import pprint
import json
from pytz import timezone, all_timezones
from icu import TimeZone, Locale
epoch = datetime.datetime(1970,1,1)
def build_names(zone):
names = []
tzicu = TimeZone.createTimeZone(zone)
names.append(tzicu.getDisplayName(False, 1))
names.append(tzicu.getDisplayName(False, 2))
if tzicu.useDaylightTime():
names.append(tzicu.getDisplayName(True, 1))
names.append(tzicu.getDisplayName(True, 2))
return names
def build_std_offset(zone):
tzicu = TimeZone.createTimeZone(zone)
return tzicu.getRawOffset() / 1000 / 60
def build_transitions(tz):
trans = []
if not hasattr(tz, '_utc_transition_times'):
return trans
for idx, d1 in enumerate(tz._utc_transition_times):
if (d1.year < 1970):
continue
dst_offset = int(tz._transition_info[idx][1].total_seconds() / 60)
utc_offset = int((d1 - epoch).total_seconds() / 3600)
trans.append(utc_offset)
trans.append(dst_offset)
return trans
def build_zone(zone):
tz = timezone(zone)
std_offset = build_std_offset(zone)
trans = build_transitions(tz)
names = build_names(zone)
return {
'id': tz.zone,
'transitions': trans,
'std_offset': std_offset,
'names': names
}
def main():
import os
import shutil
shutil.rmtree('data')
for zone in all_timezones:
path = 'data/' + zone + '.json'
dir_path = os.path.dirname(path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
with open(path, 'w+') as f:
json.dump(build_zone(zone), f, sort_keys=True, separators=(',', ':'))
if __name__ == "__main__":
main()