from __future__ import annotations from datetime import datetime, timedelta, timezone from pathlib import Path import pytest from somafm_tui.widgets import common BERLIN = Path("/usr/share/zoneinfo/Europe/Berlin") UTC_FILE = Path("/usr/share/zoneinfo/UTC") @pytest.fixture(autouse=True) def clear_zone_cache(): common.local_zone.cache_clear() yield common.local_zone.cache_clear() def _copy_zone(tmp_path: Path, source: Path) -> Path: target = tmp_path / "localtime" target.write_bytes(source.read_bytes()) return target def test_build_time_tz_utc_does_not_hide_the_system_zone(tmp_path, monkeypatch) -> None: if not BERLIN.exists(): pytest.skip("system tzdata is not installed") monkeypatch.setattr(common, "LOCALTIME_PATH", str(_copy_zone(tmp_path, BERLIN))) monkeypatch.setenv("TZ", "UTC") zone = common.local_zone() assert zone.utcoffset(datetime(2026, 1, 1)) == timedelta(hours=1) aware = datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc) assert common.local_time(aware) == "13:00" def test_explicit_non_utc_tz_is_respected(tmp_path, monkeypatch) -> None: if not BERLIN.exists() or not UTC_FILE.exists(): pytest.skip("system tzdata is not installed") monkeypatch.setattr(common, "LOCALTIME_PATH", str(_copy_zone(tmp_path, UTC_FILE))) monkeypatch.setenv("TZ", "Europe/Berlin") zone = common.local_zone() assert zone.utcoffset(datetime(2026, 1, 1)) == timedelta(hours=1) assert common.local_time(datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc)) == "13:00"