~stub/pytz/devel

« back to all changes in this revision

Viewing changes to tzfile.py

  • Committer: Arch Librarian
  • Date: 2005-05-10 20:54:17 UTC
  • Revision ID: Arch-1:pytz@products.ubuntu.com%pytz--MAIN--0--base-0
Initial revision
Author: zenzen
Date: 2003-04-23 01:25:54 GMT
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
'''
 
3
$Id: tzfile.py,v 1.1 2003/04/23 01:25:54 zenzen Exp $
 
4
'''
 
5
 
 
6
__rcs_id__  = '$Id: tzfile.py,v 1.1 2003/04/23 01:25:54 zenzen Exp $'
 
7
__version__ = '$Revision: 1.1 $'[11:-2]
 
8
 
 
9
from struct import unpack,calcsize
 
10
 
 
11
class TZFile:
 
12
    ''' Wrapper around a tzfile(5) format file as generated by zic(1) '''
 
13
    def __init__(self,filename):
 
14
        inf = open(filename)
 
15
 
 
16
        # See elsie.nci.nih.gov/src/tzfile.5 for details of the file format
 
17
        
 
18
        head_fmt = '>4s16x6l'
 
19
        head_size = calcsize(head_fmt)
 
20
 
 
21
        (magic,ttisgmtcnt,ttisstdcnt,leapcnt,timecnt,typecnt,charcnt) = \
 
22
            unpack(head_fmt,inf.read(head_size))
 
23
 
 
24
        # Make sure it is a tzinfo(5) file
 
25
        assert magic == 'TZif'
 
26
 
 
27
        # Our transition times
 
28
        transitions_fmt = '>%dl' % (timecnt)
 
29
        transitions_size = calcsize(transitions_fmt)
 
30
        transitions = unpack(transitions_fmt,inf.read(transitions_size))
 
31
 
 
32
        # Our indexes into the local time structures
 
33
        lindexes = unpack('%dB' % (timecnt),inf.read(timecnt))
 
34
 
 
35
        # Our ttinfo structures, detailing gmtoffset, isdst and abbriv index
 
36
        ttinfo_fmt = '>lBB'
 
37
        ttinfo_size = calcsize(ttinfo_fmt)
 
38
        ttinfo = [ 
 
39
            unpack(ttinfo_fmt,inf.read(ttinfo_size)) for i in xrange(0,typecnt)
 
40
            ]
 
41
 
 
42
        # Our timezone abbreviations, stored suckily as null terminated strings
 
43
        raw = inf.read()
 
44
        tznames = raw.split('\0',charcnt)[:-1]
 
45
 
 
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))
 
50
 
 
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)) ]
 
57
 
 
58
    def __repr__(self):
 
59
        return repr(self.transitions)
 
60
 
 
61
if __name__ == '__main__':
 
62
    import os.path
 
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'))
 
67
    print repr(tz)
 
68