""" 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