1 | n/a | import sys |
---|
2 | n/a | import os |
---|
3 | n/a | import struct |
---|
4 | n/a | from array import array |
---|
5 | n/a | from collections import namedtuple |
---|
6 | n/a | from datetime import datetime, timedelta |
---|
7 | n/a | |
---|
8 | n/a | ttinfo = namedtuple('ttinfo', ['tt_gmtoff', 'tt_isdst', 'tt_abbrind']) |
---|
9 | n/a | |
---|
10 | n/a | class TZInfo: |
---|
11 | n/a | def __init__(self, transitions, type_indices, ttis, abbrs): |
---|
12 | n/a | self.transitions = transitions |
---|
13 | n/a | self.type_indices = type_indices |
---|
14 | n/a | self.ttis = ttis |
---|
15 | n/a | self.abbrs = abbrs |
---|
16 | n/a | |
---|
17 | n/a | @classmethod |
---|
18 | n/a | def fromfile(cls, fileobj): |
---|
19 | n/a | if fileobj.read(4).decode() != "TZif": |
---|
20 | n/a | raise ValueError("not a zoneinfo file") |
---|
21 | n/a | fileobj.seek(20) |
---|
22 | n/a | header = fileobj.read(24) |
---|
23 | n/a | tzh = (tzh_ttisgmtcnt, tzh_ttisstdcnt, tzh_leapcnt, |
---|
24 | n/a | tzh_timecnt, tzh_typecnt, tzh_charcnt) = struct.unpack(">6l", header) |
---|
25 | n/a | transitions = array('i') |
---|
26 | n/a | transitions.fromfile(fileobj, tzh_timecnt) |
---|
27 | n/a | if sys.byteorder != 'big': |
---|
28 | n/a | transitions.byteswap() |
---|
29 | n/a | |
---|
30 | n/a | type_indices = array('B') |
---|
31 | n/a | type_indices.fromfile(fileobj, tzh_timecnt) |
---|
32 | n/a | |
---|
33 | n/a | ttis = [] |
---|
34 | n/a | for i in range(tzh_typecnt): |
---|
35 | n/a | ttis.append(ttinfo._make(struct.unpack(">lbb", fileobj.read(6)))) |
---|
36 | n/a | |
---|
37 | n/a | abbrs = fileobj.read(tzh_charcnt) |
---|
38 | n/a | |
---|
39 | n/a | self = cls(transitions, type_indices, ttis, abbrs) |
---|
40 | n/a | self.tzh = tzh |
---|
41 | n/a | |
---|
42 | n/a | return self |
---|
43 | n/a | |
---|
44 | n/a | def dump(self, stream, start=None, end=None): |
---|
45 | n/a | for j, (trans, i) in enumerate(zip(self.transitions, self.type_indices)): |
---|
46 | n/a | utc = datetime.utcfromtimestamp(trans) |
---|
47 | n/a | tti = self.ttis[i] |
---|
48 | n/a | lmt = datetime.utcfromtimestamp(trans + tti.tt_gmtoff) |
---|
49 | n/a | abbrind = tti.tt_abbrind |
---|
50 | n/a | abbr = self.abbrs[abbrind:self.abbrs.find(0, abbrind)].decode() |
---|
51 | n/a | if j > 0: |
---|
52 | n/a | prev_tti = self.ttis[self.type_indices[j - 1]] |
---|
53 | n/a | shift = " %+g" % ((tti.tt_gmtoff - prev_tti.tt_gmtoff) / 3600) |
---|
54 | n/a | else: |
---|
55 | n/a | shift = '' |
---|
56 | n/a | print("%s UTC = %s %-5s isdst=%d" % (utc, lmt, abbr, tti[1]) + shift, file=stream) |
---|
57 | n/a | |
---|
58 | n/a | @classmethod |
---|
59 | n/a | def zonelist(cls, zonedir='/usr/share/zoneinfo'): |
---|
60 | n/a | zones = [] |
---|
61 | n/a | for root, _, files in os.walk(zonedir): |
---|
62 | n/a | for f in files: |
---|
63 | n/a | p = os.path.join(root, f) |
---|
64 | n/a | with open(p, 'rb') as o: |
---|
65 | n/a | magic = o.read(4) |
---|
66 | n/a | if magic == b'TZif': |
---|
67 | n/a | zones.append(p[len(zonedir) + 1:]) |
---|
68 | n/a | return zones |
---|
69 | n/a | |
---|
70 | n/a | if __name__ == '__main__': |
---|
71 | n/a | if len(sys.argv) < 2: |
---|
72 | n/a | zones = TZInfo.zonelist() |
---|
73 | n/a | for z in zones: |
---|
74 | n/a | print(z) |
---|
75 | n/a | sys.exit() |
---|
76 | n/a | filepath = sys.argv[1] |
---|
77 | n/a | if not filepath.startswith('/'): |
---|
78 | n/a | filepath = os.path.join('/usr/share/zoneinfo', filepath) |
---|
79 | n/a | with open(filepath, 'rb') as fileobj: |
---|
80 | n/a | tzi = TZInfo.fromfile(fileobj) |
---|
81 | n/a | tzi.dump(sys.stdout) |
---|