Download scripts/audit_tables.py from debudash/slm-training: direct link, hf CLI and curl.
- Browser
- Download file 5.89 kB
-
https://huggingface.co/debudash/slm-training/resolve/main/scripts/audit_tables.py
- Command line
-
hf download hf://debudash/slm-training/scripts/audit_tables.py
-
curl -L -o audit_tables.py https://huggingface.co/debudash/slm-training/resolve/main/scripts/audit_tables.py
5.89 kB
| """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() | |