feat(timezones): public IANA timezone table with POSIX rules for device clocks

Firmware 3.0.0+ (project-vesper F-079) keeps the RTC in UTC and, in
timezone mode "auto", evaluates a POSIX TZ rule string sent by whoever sets
the timezone. Devices don't carry the IANA database and POSIX signs are
inverted, so the console and apps need one place to get the right rule.

GET /api/public/timezones[?country=GR] returns every country with its zones
(zone.tab), each with the POSIX rule (TZif footer), std/DST offsets in
minutes east of UTC, and the current offset/DST state.
GET /api/public/timezones/zone?name=... resolves one zone, including
aliases zone.tab doesn't list (e.g. a phone reporting Asia/Calcutta).

Public on purpose: IANA data, needed by the phone app (Firebase auth, MQTT)
and for the tablet app's offline bundle (save the full response, ~130 KB).
Source is the tzdata pip package, now pinned (2026.6 = IANA 2026f); bumping
the pin is how new DST rules arrive. Docs in docs/device-timezones.md;
tests in tests/test_timezones.py.

Stage 4 of project-vesper docs/features/timekeeper-overhaul.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 21:01:04 +02:00
co-authored by Claude Opus 5.5
parent 8837eb1e70
commit e4ea43a079
7 changed files with 409 additions and 1 deletions
+117
View File
@@ -0,0 +1,117 @@
"""
Timezone table for device clock setup (timezones/). Pure tzdata — no Firestore, no MQTT.
"""
from datetime import datetime, timezone
import pytest
import tzdata
from fastapi import FastAPI
from fastapi.testclient import TestClient
from timezones import service
from timezones.router import router
SUMMER = datetime(2026, 7, 15, 12, tzinfo=timezone.utc)
WINTER = datetime(2026, 1, 15, 12, tzinfo=timezone.utc)
@pytest.mark.parametrize("posix, expected", [
("EET-2EEST,M3.5.0/3,M10.5.0/4", (120, 60, True)),
("EST5EDT,M3.2.0,M11.1.0", (-300, 60, True)),
("IST-5:30", (330, 0, False)),
("<+0545>-5:45", (345, 0, False)),
("<-03>3", (-180, 0, False)),
("IST-1GMT0,M10.5.0,M3.5.0/1", (60, -60, True)), # Dublin: negative DST
("<+1030>-10:30<+11>-11,M10.1.0,M4.1.0", (630, 30, True)), # Lord Howe: 30 min DST
("<+1245>-12:45<+1345>,M9.5.0/2:45,M4.1.0/3:45", (765, 60, True)), # Chatham
("IST-2IDT,M3.4.4/26,M10.5.0", (120, 60, True)), # Israel extended hours
])
def test_posix_offsets(posix, expected):
assert service.posix_offsets(posix) == expected
def test_posix_offsets_rejects_garbage():
with pytest.raises(ValueError):
service.posix_offsets("not a rule")
def test_table_shape_and_version():
table = service.get_table(now=WINTER)
assert table["tzdata_version"] == tzdata.IANA_VERSION
countries = table["countries"]
assert len(countries) > 200
assert [c["name"] for c in countries] == sorted(c["name"] for c in countries)
zone_count = 0
for country in countries:
assert country["zones"], country["code"]
for zone in country["zones"]:
zone_count += 1
assert zone["posix"] and len(zone["posix"]) < 64 # firmware POSIX_MAX_LEN
assert len(zone["name"]) < 48 # firmware NAME_MAX_LEN
assert country["code"] in zone["countries"]
assert zone_count > 300
def test_greece_and_australia():
gr = service.get_table("gr", now=SUMMER)["countries"]
assert len(gr) == 1 and gr[0]["name"] == "Greece"
athens = gr[0]["zones"][0]
assert athens["name"] == "Europe/Athens"
assert athens["posix"] == "EET-2EEST,M3.5.0/3,M10.5.0/4"
assert (athens["std_offset_min"], athens["dst_offset_min"], athens["has_dst"]) == (120, 60, True)
assert athens["current_offset_min"] == 180 and athens["current_dst"] is True
assert athens["display"] == "(UTC+03:00) Athens"
au = {z["name"]: z for z in service.get_table("AU", now=SUMMER)["countries"][0]["zones"]}
assert au["Australia/Sydney"]["current_offset_min"] == 600 # southern winter
assert au["Australia/Brisbane"]["has_dst"] is False
assert au["Australia/Lord_Howe"]["dst_offset_min"] == 30
def test_current_offset_follows_dst():
athens = service.get_zone("Europe/Athens", now=WINTER)
assert athens["current_offset_min"] == 120 and athens["current_dst"] is False
def test_rule_offsets_match_zoneinfo_once_rules_are_settled():
# By 2030 every zone runs on its POSIX footer, so the offset in force must be
# either the standard offset or standard + DST from that rule.
table = service.get_table()
for when in (datetime(2030, 1, 15, 12, tzinfo=timezone.utc), datetime(2030, 7, 15, 12, tzinfo=timezone.utc)):
for country in table["countries"]:
for zone in country["zones"]:
current = service.get_zone(zone["name"], now=when)["current_offset_min"]
allowed = {zone["std_offset_min"], zone["std_offset_min"] + zone["dst_offset_min"]}
assert current in allowed, (zone["name"], when, current, allowed)
def test_unknown_country_and_zone():
assert service.get_table("XX") is None
assert service.get_zone("Nowhere/Town") is None
assert service.get_zone("../../etc/passwd") is None
def test_alias_not_in_zone_tab_resolves():
zone = service.get_zone("Asia/Calcutta")
assert zone["posix"] == "IST-5:30" and zone["countries"] == []
@pytest.fixture
def client():
app = FastAPI()
app.include_router(router)
return TestClient(app)
def test_routes(client):
r = client.get("/api/public/timezones", params={"country": "GR"})
assert r.status_code == 200
assert r.json()["countries"][0]["zones"][0]["name"] == "Europe/Athens"
assert client.get("/api/public/timezones", params={"country": "XX"}).status_code == 404
r = client.get("/api/public/timezones/zone", params={"name": "Australia/Sydney"})
assert r.status_code == 200 and r.json()["posix"] == "AEST-10AEDT,M10.1.0,M4.1.0/3"
assert client.get("/api/public/timezones/zone", params={"name": "Nowhere/Town"}).status_code == 404