~ubuntu-branches/ubuntu/precise/python-tz/precise

« back to all changes in this revision

Viewing changes to .pc/samoa-idl/pytz/tzfile.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack, Forest Bond
  • Date: 2012-01-06 10:29:22 UTC
  • Revision ID: package-import@ubuntu.com-20120106102922-bwhn4iwh2qmw2n06
Tags: 2011k-0ubuntu3
[ Forest Bond ]
Add patch samoa-idl (LP: #885163).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
'''
 
3
$Id: tzfile.py,v 1.8 2004/06/03 00:15:24 zenzen Exp $
 
4
'''
 
5
 
 
6
try:
 
7
    from cStringIO import StringIO
 
8
except ImportError:
 
9
    from io import StringIO
 
10
from datetime import datetime, timedelta
 
11
from struct import unpack, calcsize
 
12
 
 
13
from pytz.tzinfo import StaticTzInfo, DstTzInfo, memorized_ttinfo
 
14
from pytz.tzinfo import memorized_datetime, memorized_timedelta
 
15
 
 
16
def _byte_string(s):
 
17
    """Cast a string or byte string to an ASCII byte string."""
 
18
    return s.encode('US-ASCII')
 
19
 
 
20
_NULL = _byte_string('\0')
 
21
 
 
22
def _std_string(s):
 
23
    """Cast a string or byte string to an ASCII string."""
 
24
    return str(s.decode('US-ASCII'))
 
25
 
 
26
def build_tzinfo(zone, fp):
 
27
    head_fmt = '>4s c 15x 6l'
 
28
    head_size = calcsize(head_fmt)
 
29
    (magic, format, ttisgmtcnt, ttisstdcnt,leapcnt, timecnt,
 
30
        typecnt, charcnt) =  unpack(head_fmt, fp.read(head_size))
 
31
 
 
32
    # Make sure it is a tzfile(5) file
 
33
    assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic)
 
34
 
 
35
    # Read out the transition times, localtime indices and ttinfo structures.
 
36
    data_fmt = '>%(timecnt)dl %(timecnt)dB %(ttinfo)s %(charcnt)ds' % dict(
 
37
        timecnt=timecnt, ttinfo='lBB'*typecnt, charcnt=charcnt)
 
38
    data_size = calcsize(data_fmt)
 
39
    data = unpack(data_fmt, fp.read(data_size))
 
40
 
 
41
    # make sure we unpacked the right number of values
 
42
    assert len(data) == 2 * timecnt + 3 * typecnt + 1
 
43
    transitions = [memorized_datetime(trans)
 
44
                   for trans in data[:timecnt]]
 
45
    lindexes = list(data[timecnt:2 * timecnt])
 
46
    ttinfo_raw = data[2 * timecnt:-1]
 
47
    tznames_raw = data[-1]
 
48
    del data
 
49
 
 
50
    # Process ttinfo into separate structs
 
51
    ttinfo = []
 
52
    tznames = {}
 
53
    i = 0
 
54
    while i < len(ttinfo_raw):
 
55
        # have we looked up this timezone name yet?
 
56
        tzname_offset = ttinfo_raw[i+2]
 
57
        if tzname_offset not in tznames:
 
58
            nul = tznames_raw.find(_NULL, tzname_offset)
 
59
            if nul < 0:
 
60
                nul = len(tznames_raw)
 
61
            tznames[tzname_offset] = _std_string(
 
62
                tznames_raw[tzname_offset:nul])
 
63
        ttinfo.append((ttinfo_raw[i],
 
64
                       bool(ttinfo_raw[i+1]),
 
65
                       tznames[tzname_offset]))
 
66
        i += 3
 
67
 
 
68
    # Now build the timezone object
 
69
    if len(transitions) == 0:
 
70
        ttinfo[0][0], ttinfo[0][2]
 
71
        cls = type(zone, (StaticTzInfo,), dict(
 
72
            zone=zone,
 
73
            _utcoffset=memorized_timedelta(ttinfo[0][0]),
 
74
            _tzname=ttinfo[0][2]))
 
75
    else:
 
76
        # Early dates use the first standard time ttinfo
 
77
        i = 0
 
78
        while ttinfo[i][1]:
 
79
            i += 1
 
80
        if ttinfo[i] == ttinfo[lindexes[0]]:
 
81
            transitions[0] = datetime.min
 
82
        else:
 
83
            transitions.insert(0, datetime.min)
 
84
            lindexes.insert(0, i)
 
85
 
 
86
        # calculate transition info
 
87
        transition_info = []
 
88
        for i in range(len(transitions)):
 
89
            inf = ttinfo[lindexes[i]]
 
90
            utcoffset = inf[0]
 
91
            if not inf[1]:
 
92
                dst = 0
 
93
            else:
 
94
                for j in range(i-1, -1, -1):
 
95
                    prev_inf = ttinfo[lindexes[j]]
 
96
                    if not prev_inf[1]:
 
97
                        break
 
98
                dst = inf[0] - prev_inf[0] # dst offset
 
99
 
 
100
                if dst <= 0: # Bad dst? Look further.
 
101
                    for j in range(i+1, len(transitions)):
 
102
                        stdinf = ttinfo[lindexes[j]]
 
103
                        if not stdinf[1]:
 
104
                            dst = inf[0] - stdinf[0]
 
105
                            if dst > 0:
 
106
                                break # Found a useful std time.
 
107
 
 
108
            tzname = inf[2]
 
109
 
 
110
            # Round utcoffset and dst to the nearest minute or the
 
111
            # datetime library will complain. Conversions to these timezones
 
112
            # might be up to plus or minus 30 seconds out, but it is
 
113
            # the best we can do.
 
114
            utcoffset = int((utcoffset + 30) // 60) * 60
 
115
            dst = int((dst + 30) // 60) * 60
 
116
            transition_info.append(memorized_ttinfo(utcoffset, dst, tzname))
 
117
 
 
118
        cls = type(zone, (DstTzInfo,), dict(
 
119
            zone=zone,
 
120
            _utc_transition_times=transitions,
 
121
            _transition_info=transition_info))
 
122
 
 
123
    return cls()
 
124
 
 
125
if __name__ == '__main__':
 
126
    import os.path
 
127
    from pprint import pprint
 
128
    # Patched in Debian, use the system zoninfo from the tzdata package
 
129
    base = '/usr/share/zoneinfo'
 
130
    tz = build_tzinfo('Australia/Melbourne',
 
131
                      open(os.path.join(base,'Australia','Melbourne'), 'rb'))
 
132
    tz = build_tzinfo('US/Eastern',
 
133
                      open(os.path.join(base,'US','Eastern'), 'rb'))
 
134
    pprint(tz._utc_transition_times)
 
135
    #print tz.asPython(4)
 
136
    #print tz.transitions_mapping