| 1 |
import struct |
| 2 |
|
| 3 |
|
| 4 |
def load_tzdata(key): |
| 5 |
from importlib import resources |
| 6 |
|
| 7 |
components = key.split("/") |
| 8 |
package_name = ".".join(["tzdata.zoneinfo"] + components[:-1]) |
| 9 |
resource_name = components[-1] |
| 10 |
|
| 11 |
try: |
| 12 |
return resources.files(package_name).joinpath(resource_name).open("rb") |
| 13 |
except (ImportError, FileNotFoundError, UnicodeEncodeError): |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
raise ZoneInfoNotFoundError(f"No time zone found with key {key}") |
| 25 |
|
| 26 |
|
| 27 |
def load_data(fobj): |
| 28 |
header = _TZifHeader.from_file(fobj) |
| 29 |
|
| 30 |
if header.version == 1: |
| 31 |
time_size = 4 |
| 32 |
time_type = "l" |
| 33 |
else: |
| 34 |
|
| 35 |
time_size = 8 |
| 36 |
time_type = "q" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
skip_bytes = ( |
| 41 |
header.timecnt * 5 |
| 42 |
+ header.typecnt * 6 |
| 43 |
+ header.charcnt |
| 44 |
+ header.leapcnt * 8 |
| 45 |
+ header.isstdcnt |
| 46 |
+ header.isutcnt |
| 47 |
) |
| 48 |
|
| 49 |
fobj.seek(skip_bytes, 1) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
header = _TZifHeader.from_file(fobj) |
| 54 |
|
| 55 |
typecnt = header.typecnt |
| 56 |
timecnt = header.timecnt |
| 57 |
charcnt = header.charcnt |
| 58 |
|
| 59 |
|
| 60 |
if timecnt: |
| 61 |
trans_list_utc = struct.unpack( |
| 62 |
f">{timecnt}{time_type}", fobj.read(timecnt * time_size) |
| 63 |
) |
| 64 |
trans_idx = struct.unpack(f">{timecnt}B", fobj.read(timecnt)) |
| 65 |
else: |
| 66 |
trans_list_utc = () |
| 67 |
trans_idx = () |
| 68 |
|
| 69 |
|
| 70 |
if typecnt: |
| 71 |
utcoff, isdst, abbrind = zip( |
| 72 |
*(struct.unpack(">lbb", fobj.read(6)) for i in range(typecnt)) |
| 73 |
) |
| 74 |
else: |
| 75 |
utcoff = () |
| 76 |
isdst = () |
| 77 |
abbrind = () |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
abbr_vals = {} |
| 84 |
abbr_chars = fobj.read(charcnt) |
| 85 |
|
| 86 |
def get_abbr(idx): |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if idx not in abbr_vals: |
| 98 |
span_end = abbr_chars.find(b"\x00", idx) |
| 99 |
abbr_vals[idx] = abbr_chars[idx:span_end].decode() |
| 100 |
|
| 101 |
return abbr_vals[idx] |
| 102 |
|
| 103 |
abbr = tuple(get_abbr(idx) for idx in abbrind) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if header.version >= 2: |
| 109 |
|
| 110 |
skip_bytes = header.isutcnt + header.isstdcnt + header.leapcnt * 12 |
| 111 |
fobj.seek(skip_bytes, 1) |
| 112 |
|
| 113 |
c = fobj.read(1) |
| 114 |
assert c == b"\n", c |
| 115 |
|
| 116 |
tz_bytes = b"" |
| 117 |
while (c := fobj.read(1)) != b"\n": |
| 118 |
tz_bytes += c |
| 119 |
|
| 120 |
tz_str = tz_bytes |
| 121 |
else: |
| 122 |
tz_str = None |
| 123 |
|
| 124 |
return trans_idx, trans_list_utc, utcoff, isdst, abbr, tz_str |
| 125 |
|
| 126 |
|
| 127 |
class _TZifHeader: |
| 128 |
__slots__ = [ |
| 129 |
"version", |
| 130 |
"isutcnt", |
| 131 |
"isstdcnt", |
| 132 |
"leapcnt", |
| 133 |
"timecnt", |
| 134 |
"typecnt", |
| 135 |
"charcnt", |
| 136 |
] |
| 137 |
|
| 138 |
def __init__(self, *args): |
| 139 |
for attr, val in zip(self.__slots__, args, strict=True): |
| 140 |
setattr(self, attr, val) |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def from_file(cls, stream): |
| 144 |
|
| 145 |
if stream.read(4) != b"TZif": |
| 146 |
raise ValueError("Invalid TZif file: magic not found") |
| 147 |
|
| 148 |
_version = stream.read(1) |
| 149 |
if _version == b"\x00": |
| 150 |
version = 1 |
| 151 |
else: |
| 152 |
version = int(_version) |
| 153 |
stream.read(15) |
| 154 |
|
| 155 |
args = (version,) |
| 156 |
|
| 157 |
|
| 158 |
args = args + struct.unpack(">6l", stream.read(24)) |
| 159 |
|
| 160 |
return cls(*args) |
| 161 |
|
| 162 |
|
| 163 |
class ZoneInfoNotFoundError(KeyError): |
| 164 |
"""Exception raised when a ZoneInfo key is not found.""" |