File size: 5,894 Bytes
4643f62 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | """Check zones.json and abbreviations.json against tzdata for the spec's date window.
Usage: python scripts/audit_tables.py [--first 2020] [--last 2030]
Exits non-zero if any check fails. Prints candidate abbreviations for supported zones.
"""
import argparse
import json
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
from zoneinfo import ZoneInfo, available_timezones
ROOT = Path(__file__).resolve().parent.parent
def scan(zone, start, end, step):
"""Yield (utc_instant, local_datetime) every `step` across [start, end)."""
zi = ZoneInfo(zone)
t = start
while t < end:
yield t, t.astimezone(zi)
t += step
def transition_days(zone, first, last):
"""Per year, the UTC dates (sampled at 12:00 UTC) on which the zone's offset changed."""
out = {}
prev = None
start = datetime(first, 1, 1, 12, tzinfo=timezone.utc)
end = datetime(last + 1, 1, 1, 12, tzinfo=timezone.utc)
for t, local in scan(zone, start, end, timedelta(days=1)):
off = local.utcoffset()
if prev is not None and off != prev:
out.setdefault(t.year, []).append(t.strftime("%m-%d"))
prev = off
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--first", type=int, default=2020)
ap.add_argument("--last", type=int, default=2030)
args = ap.parse_args()
zones = json.loads((ROOT / "zones.json").read_text(encoding="utf-8"))["zones"]
abbr = json.loads((ROOT / "abbreviations.json").read_text(encoding="utf-8"))
tz_all = available_timezones()
failures = []
# 1. Every supported zone exists, and zones in one DST family switch on the same days.
family_days = {}
for z, fam in zones.items():
if z not in tz_all:
failures.append(f"zone {z} not in tzdata")
continue
days = transition_days(z, args.first, args.last)
if fam == "none":
if days:
failures.append(f"zone {z} is 'none' but changes offset: {days}")
else:
family_days.setdefault(fam, {})[z] = days
for fam, members in family_days.items():
ref_zone, ref = next(iter(members.items()))
for z, days in members.items():
if days != ref:
failures.append(f"family {fam}: {z} differs from {ref_zone}: {days} vs {ref}")
if any(len(v) != 2 for v in days.values()) or len(days) != args.last - args.first + 1:
failures.append(f"family {fam}: {z} does not switch exactly twice every year: {days}")
# 2. Which offsets each abbreviation has anywhere in tzdata within the window.
start = datetime(args.first, 1, 1, tzinfo=timezone.utc)
end = datetime(args.last + 1, 1, 1, tzinfo=timezone.utc)
seen = {} # abbreviation -> {offset: set(zones)}
for z in sorted(tz_all):
for _, local in scan(z, start, end, timedelta(days=2)):
name = local.tzname()
if name and name.isalpha():
seen.setdefault(name, {}).setdefault(local.strftime("%z"), set()).add(z)
# 3. Unambiguous entries: zone supported, abbreviation used by that zone in that season,
# and no zone anywhere uses it with another offset.
for name, e in abbr["unambiguous"].items():
want = e["offset"].replace(":", "")
if e["zone"] not in zones:
failures.append(f"{name}: zone {e['zone']} not in zones.json")
offs = seen.get(name, {})
others = {o: sorted(zs)[:4] for o, zs in offs.items() if o != want}
if others:
failures.append(f"{name}: also used with other offsets {others}")
if name in ("UTC", "GMT"):
continue
used = {(l.tzname(), l.strftime("%z"), bool(l.dst())) for _, l in scan(e["zone"], start, end, timedelta(days=2))}
season_dst = {"standard": False, "daylight": True}.get(e["season"])
hits = [u for u in used if u[0] == name and u[1] == want]
if not hits:
failures.append(f"{name}: {e['zone']} never shows {name} {e['offset']} in window")
elif season_dst is not None and any(u[2] != season_dst for u in hits):
failures.append(f"{name}: season '{e['season']}' does not match tzdata dst flag")
elif e["season"] == "all" and len({u[1] for u in used}) != 1:
failures.append(f"{name}: season 'all' but {e['zone']} changes offset")
# 4. Ambiguous entries on a 'tzdata' basis must have 2+ offsets in the window.
# 'usage' entries are ambiguous in common use, which tzdata cannot confirm.
for name, e in abbr["ambiguous"].items():
if e["basis"] == "tzdata" and len(seen.get(name, {})) < 2:
failures.append(f"{name}: basis 'tzdata' but only {seen.get(name)} in window ({e['reason']})")
# 5. Candidates: alphabetic abbreviations used by supported zones but not in the table.
listed = set(abbr["unambiguous"]) | set(abbr["ambiguous"])
print("Candidate abbreviations for supported zones (not in table):")
for z in zones:
for _, local in scan(z, start, end, timedelta(days=30)):
name = local.tzname()
if name and name.isalpha() and name not in listed:
offs = {o: sorted(zs)[:3] for o, zs in seen[name].items()}
print(f" {name:<5} via {z:<22} {'UNAMBIGUOUS' if len(offs) == 1 else 'AMBIGUOUS'} {offs}")
listed.add(name)
print()
if failures:
print("FAIL")
for f in failures:
print(" -", f)
sys.exit(1)
print(f"OK: {len(zones)} zones, {len(abbr['unambiguous'])} unambiguous, "
f"{len(abbr['ambiguous'])} ambiguous abbreviations, {args.first}-{args.last}")
if __name__ == "__main__":
main()
|