3
$Id: tzfile.py,v 1.8 2004/06/03 00:15:24 zenzen Exp $
7
from cStringIO import StringIO
9
from io import StringIO
10
from datetime import datetime, timedelta
11
from struct import unpack, calcsize
13
from pytz.tzinfo import StaticTzInfo, DstTzInfo, memorized_ttinfo
14
from pytz.tzinfo import memorized_datetime, memorized_timedelta
17
"""Cast a string or byte string to an ASCII byte string."""
18
return s.encode('US-ASCII')
20
_NULL = _byte_string('\0')
23
"""Cast a string or byte string to an ASCII string."""
24
return str(s.decode('US-ASCII'))
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))
32
# Make sure it is a tzfile(5) file
33
assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic)
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))
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]
50
# Process ttinfo into separate structs
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)
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]))
68
# Now build the timezone object
69
if len(transitions) == 0:
70
ttinfo[0][0], ttinfo[0][2]
71
cls = type(zone, (StaticTzInfo,), dict(
73
_utcoffset=memorized_timedelta(ttinfo[0][0]),
74
_tzname=ttinfo[0][2]))
76
# Early dates use the first standard time ttinfo
80
if ttinfo[i] == ttinfo[lindexes[0]]:
81
transitions[0] = datetime.min
83
transitions.insert(0, datetime.min)
86
# calculate transition info
88
for i in range(len(transitions)):
89
inf = ttinfo[lindexes[i]]
94
for j in range(i-1, -1, -1):
95
prev_inf = ttinfo[lindexes[j]]
98
dst = inf[0] - prev_inf[0] # dst offset
100
if dst <= 0: # Bad dst? Look further.
101
for j in range(i+1, len(transitions)):
102
stdinf = ttinfo[lindexes[j]]
104
dst = inf[0] - stdinf[0]
106
break # Found a useful std time.
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))
118
cls = type(zone, (DstTzInfo,), dict(
120
_utc_transition_times=transitions,
121
_transition_info=transition_info))
125
if __name__ == '__main__':
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