MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / build_tzinfo

Function build_tzinfo

tests/test_code/py/pytz/tzfile.py:24–123  ·  view source on GitHub ↗
(zone, fp)

Source from the content-addressed store, hash-verified

22
23
24def build_tzinfo(zone, fp):
25 head_fmt = '>4s c 15x 6l'
26 head_size = calcsize(head_fmt)
27 (magic, format, ttisgmtcnt, ttisstdcnt, leapcnt, timecnt,
28 typecnt, charcnt) = unpack(head_fmt, fp.read(head_size))
29
30 # Make sure it is a tzfile(5) file
31 assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic)
32
33 # Read out the transition times, localtime indices and ttinfo structures.
34 data_fmt = '>%(timecnt)dl %(timecnt)dB %(ttinfo)s %(charcnt)ds' % dict(
35 timecnt=timecnt, ttinfo='lBB' * typecnt, charcnt=charcnt)
36 data_size = calcsize(data_fmt)
37 data = unpack(data_fmt, fp.read(data_size))
38
39 # make sure we unpacked the right number of values
40 assert len(data) == 2 * timecnt + 3 * typecnt + 1
41 transitions = [memorized_datetime(trans)
42 for trans in data[:timecnt]]
43 lindexes = list(data[timecnt:2 * timecnt])
44 ttinfo_raw = data[2 * timecnt:-1]
45 tznames_raw = data[-1]
46 del data
47
48 # Process ttinfo into separate structs
49 ttinfo = []
50 tznames = {}
51 i = 0
52 while i < len(ttinfo_raw):
53 # have we looked up this timezone name yet?
54 tzname_offset = ttinfo_raw[i + 2]
55 if tzname_offset not in tznames:
56 nul = tznames_raw.find(_NULL, tzname_offset)
57 if nul < 0:
58 nul = len(tznames_raw)
59 tznames[tzname_offset] = _std_string(
60 tznames_raw[tzname_offset:nul])
61 ttinfo.append((ttinfo_raw[i],
62 bool(ttinfo_raw[i + 1]),
63 tznames[tzname_offset]))
64 i += 3
65
66 # Now build the timezone object
67 if len(ttinfo) == 1 or len(transitions) == 0:
68 ttinfo[0][0], ttinfo[0][2]
69 cls = type(zone, (StaticTzInfo,), dict(
70 zone=zone,
71 _utcoffset=memorized_timedelta(ttinfo[0][0]),
72 _tzname=ttinfo[0][2]))
73 else:
74 # Early dates use the first standard time ttinfo
75 i = 0
76 while ttinfo[i][1]:
77 i += 1
78 if ttinfo[i] == ttinfo[lindexes[0]]:
79 transitions[0] = datetime.min
80 else:
81 transitions.insert(0, datetime.min)

Callers 2

timezoneFunction · 0.90
tzfile.pyFile · 0.85

Calls 5

memorized_datetimeFunction · 0.90
memorized_timedeltaFunction · 0.90
memorized_ttinfoFunction · 0.90
_byte_stringFunction · 0.85
_std_stringFunction · 0.85

Tested by

no test coverage detected