annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/pytz/tzfile.py @ 68:5028fdace37b

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