3
$Id: tzfile.py,v 1.1 2003/04/23 01:25:54 zenzen Exp $
6
__rcs_id__ = '$Id: tzfile.py,v 1.1 2003/04/23 01:25:54 zenzen Exp $'
7
__version__ = '$Revision: 1.1 $'[11:-2]
9
from struct import unpack,calcsize
12
''' Wrapper around a tzfile(5) format file as generated by zic(1) '''
13
def __init__(self,filename):
16
# See elsie.nci.nih.gov/src/tzfile.5 for details of the file format
19
head_size = calcsize(head_fmt)
21
(magic,ttisgmtcnt,ttisstdcnt,leapcnt,timecnt,typecnt,charcnt) = \
22
unpack(head_fmt,inf.read(head_size))
24
# Make sure it is a tzinfo(5) file
25
assert magic == 'TZif'
27
# Our transition times
28
transitions_fmt = '>%dl' % (timecnt)
29
transitions_size = calcsize(transitions_fmt)
30
transitions = unpack(transitions_fmt,inf.read(transitions_size))
32
# Our indexes into the local time structures
33
lindexes = unpack('%dB' % (timecnt),inf.read(timecnt))
35
# Our ttinfo structures, detailing gmtoffset, isdst and abbriv index
37
ttinfo_size = calcsize(ttinfo_fmt)
39
unpack(ttinfo_fmt,inf.read(ttinfo_size)) for i in xrange(0,typecnt)
42
# Our timezone abbreviations, stored suckily as null terminated strings
44
tznames = raw.split('\0',charcnt)[:-1]
46
print '%d Transitions' % len(transitions)
47
print '%d lindexes' % len(lindexes)
48
print '%d ttinfo: %s' % (len(ttinfo),repr(ttinfo))
49
print '%d tznames: %s' % (len(tznames),repr(tznames))
51
self.transitions = [ (
52
transitions[i], # epoch time of transition
53
ttinfo[lindexes[i]][0], # utc offset in seconds
54
ttinfo[lindexes[i]][1], # is DST
55
tznames[ttinfo[lindexes[i]][2]] # timezone abbreviation
56
) for i in xrange(0,len(transitions)) ]
59
return repr(self.transitions)
61
if __name__ == '__main__':
63
base = os.path.join('elsie.nci.nih.gov','build','etc','zoneinfo')
64
tz = TZFile(os.path.join(base,'Australia','Melbourne'))
65
tz = TZFile(os.path.join(base,'US','Central'))
66
tz = TZFile(os.path.join(base,'US','Eastern'))