Countdown is off by an hour across DST transitions
The finding
The next start time at is built with current.replace(hour=..., minute=...) plus timedelta(days=day). This is wall-clock arithmetic. current is either naive (datetime.now()) or an aware datetime that carries the same tzinfo object as at. In both cases at - current is computed without accounting for any UTC-offset change between the two instants. Python ignores offsets when subtracting two aware datetimes that share a tzinfo. So when a DST change falls between now and the next period start, the returned seconds are wrong by the DST delta, typically ±3600. A start time that does not exist on spring-forward day, such as 02:30, also yields a bogus value. Callers that schedule or brief on 'seconds until the period changes' will be an hour early or late on those days.
1| """Time-of-use energy tariffs.
2|
3| A tariff names its periods by local wall-clock time. Prices are per ``unit``
4| (usually kWh) in the currency's minor unit (cents, pence), so the engine works
5| for any currency. See ``examples/tariff.yaml``.
6|
7| **Showing a rate.** People and the model never see raw minor units: :func:`format_rate` rounds a rate and
8| shows it in the unit people quote it in. A currency with a common minor-unit sign (cents, pence) is shown in
9| it (``23.7 c/kWh``); a zero-decimal currency (JPY, KRW) has no minor unit, so its rate is already in major
10| units (``31.5 JPY/kWh``); any other currency is shown in major units (``0.237 CHF/kWh``). How many decimals
11| the minor unit has comes from the ISO 4217 currency code (:func:`minor_digits`), or the tariff file's
12| ``minor_digits`` for a currency this module doesn't list.
13| """
14|
15| from __future__ import annotations
16|
17| from dataclasses import dataclass
18| from datetime import datetime, time, timedelta
19| from pathlib import Path
20| from typing import Any
21|
22| import structlogThe work pack
Recommendation. Compute the delta in absolute time. Build at in the tariff's timezone, using zoneinfo and fold handling. Then compute (at.astimezone(timezone.utc) - current.astimezone(timezone.utc)).total_seconds(). Alternatively, take now as an aware UTC datetime and convert it to the tariff's local zone only to choose the wall-clock start.
Acceptance criteria
- Address: Countdown is off by an hour across DST transitions
- Keep the change scoped to the cited file/symbols unless impact requires otherwise
- Compute the delta in absolute time. Build
atin the tariff's timezone, using zoneinfo andfoldhandling. Then compute(at.astimezone(timezone.utc) - current.astimezone(timezone.utc)).total_seconds(). Alternatively, takenowas an aw - Re-check
src/argus_scout/tariff.pyafter the fix
Status
- claimed
- resolved
The fix
df4f3be8e3 fix: tariff countdown is in absolute time across DST changes.
@@ -15,7 +15,7 @@ the minor unit has comes from the ISO 4217 currency code (:func:`minor_digits`), from __future__ import annotations from dataclasses import dataclass-from datetime import datetime, time, timedelta+from datetime import UTC, datetime, time, timedelta from pathlib import Path from typing import Any @@ -268,8 +268,17 @@ def current_period(tariff: Tariff, now: datetime | None = None) -> TariffPeriod: def next_period_change(tariff: Tariff, now: datetime | None = None) -> tuple[str, int]:- """The next period's name and the seconds until it starts."""+ """The next period's name and the seconds until it starts.++ Periods start at wall-clock times; with an aware ``now`` the countdown is in absolute time, so a DST change+ before the next start is counted (a naive ``now`` has no zone to know it by).""" current = now or datetime.now()++ def elapsed(at: datetime) -> float:+ if current.tzinfo is None:+ return (at - current).total_seconds()+ return (at.astimezone(UTC) - current.astimezone(UTC)).total_seconds()+ active = current_period(tariff, current) starts = sorted(tariff.periods, key=lambda p: _minutes(p.start)) for day in (0, 1):@@ -279,8 +288,8 @@ def next_period_change(tariff: Tariff, now: datetime | None = None) -> tuple[str at = current.replace(hour=p.start.hour, minute=p.start.minute, second=0, microsecond=0) + timedelta( days=day )- if at > current:- return p.name, int((at - current).total_seconds())+ if elapsed(at) > 0:+ return p.name, int(elapsed(at)) return active.name, 0 # a single all-day period never changes @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import datetime, time from pathlib import Path+from zoneinfo import ZoneInfo import pytest @@ -63,6 +64,14 @@ def test_next_period_change_wraps_to_tomorrow(tariff: Tariff): assert next_period_change(tariff, at(23)) == ("solar_soak", 11 * 3600) +def test_next_period_change_counts_a_dst_change_in_absolute_time(tariff: Tariff):+ london = ZoneInfo("Europe/London")+ # 23:00 BST the night before clocks go back: 10:00 GMT tomorrow is 12h away, not the wall clock's 11h.+ assert next_period_change(tariff, datetime(2026, 10, 24, 23, tzinfo=london)) == ("solar_soak", 12 * 3600)+ assert next_period_change(tariff, datetime(2026, 3, 28, 23, tzinfo=london)) == ("solar_soak", 10 * 3600)+ assert next_period_change(tariff, datetime(2026, 5, 9, 23, tzinfo=london)) == ("solar_soak", 11 * 3600)++ def test_single_all_day_period_never_changes(): flat = tariff_from_dict({"plan": "flat", "periods": {"all": {"start": "00:00", "end": "00:00", "rate": 30}}}) assert current_period(flat, at(13)).name == "all"How it was judged
Proven 1 test(s) fail on dev and pass with the fix: tests/test_tariff.py::test_next_period_change_counts_a_dst_change_in_absolute_time
Judged 24 Nov 2026, 04:51 UTC against dev at d0c0f66.
| Test | on dev | dev + patch |
|---|---|---|
tests/test_tariff.py::test_next_period_change_counts_a_dst_change_in_absolute_time | failed | passed |
The other 19 tests in the run (19 with the same result on both trees)
tests/test_tariff.py::test_active_plan_needs_periods | passed | passed |
tests/test_tariff.py::test_current_period[when0-solar_soak] | passed | passed |
tests/test_tariff.py::test_current_period[when1-solar_soak] | passed | passed |
tests/test_tariff.py::test_current_period[when2-peak] | passed | passed |
tests/test_tariff.py::test_current_period[when3-peak] | passed | passed |
tests/test_tariff.py::test_current_period[when4-off_peak] | passed | passed |
tests/test_tariff.py::test_current_period[when5-off_peak] | passed | passed |
tests/test_tariff.py::test_current_period[when6-off_peak] | passed | passed |
tests/test_tariff.py::test_current_period[when7-off_peak] | passed | passed |
tests/test_tariff.py::test_format_rate[23.6916--23.7/kWh] | passed | passed |
tests/test_tariff.py::test_gap_detected_and_dearest_period_used_inside_it | passed | passed |
tests/test_tariff.py::test_load_example | passed | passed |
tests/test_tariff.py::test_minor_digits_from_the_currency_or_the_file | passed | passed |
tests/test_tariff.py::test_next_period_change_same_day | passed | passed |
tests/test_tariff.py::test_next_period_change_wraps_to_tomorrow | passed | passed |
tests/test_tariff.py::test_overnight_period_contains | passed | passed |
tests/test_tariff.py::test_single_all_day_period_never_changes | passed | passed |
tests/test_tariff.py::test_tiered_cost | passed | passed |
tests/test_tariff.py::test_tiers_must_ascend | passed | passed |