annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/pytz/tzfile.py @ 69:33d812a61356

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