M8-R11: make business timezone deterministic
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user