M8-R11: make business timezone deterministic
frontend / frontend (push) Successful in 46s
pytest / test (push) Successful in 3m56s

This commit is contained in:
2026-08-27 12:16:52 +02:00
parent d5623b9fcb
commit 33ca3da593
6 changed files with 65 additions and 10 deletions
+4 -6
View File
@@ -19,8 +19,8 @@ Priority for resolving the local timezone
-----------------------------------------
1. ``TZ`` environment variable — ``ZoneInfo(os.environ["TZ"])``.
Set ``TZ=Europe/Amsterdam`` in the deployment env for correct NL handling.
2. System local timezone fallback: ``datetime.now().astimezone().tzinfo``.
This matches the behaviour callers already relied on implicitly.
2. The DST-aware ``Europe/Amsterdam`` business timezone. This keeps local-day
calculations deterministic when a deployment does not set ``TZ``.
"""
from __future__ import annotations
@@ -40,7 +40,7 @@ def local_tz() -> "tzinfo":
Resolution order:
1. ``TZ`` environment variable (``ZoneInfo(TZ)``). Set
``TZ=Europe/Amsterdam`` in production for correct NL/DST handling.
2. System local timezone via ``datetime.now().astimezone().tzinfo``.
2. The DST-aware ``Europe/Amsterdam`` business timezone.
**Monkeypatch this function in tests** to get deterministic timezone
behaviour regardless of CI host configuration::
@@ -51,9 +51,7 @@ def local_tz() -> "tzinfo":
tz_env = os.environ.get("TZ", "").strip()
if tz_env:
return ZoneInfo(tz_env)
# System fallback — identical to the .astimezone() pattern already used
# in homeassistant_inbound.py and poo.py.
return datetime.now().astimezone().tzinfo # type: ignore[return-value]
return ZoneInfo("Europe/Amsterdam")
def to_local(dt: datetime) -> datetime: