From e40ff7f6e357e134a3daa4b7e8e20f99eb53d868 Mon Sep 17 00:00:00 2001 From: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Date: Sat, 14 Nov 2026 10:28:19 +0100 Subject: [PATCH] fix(genetic): date a run in progress in the time zone of the horizon The start day of a run in progress decides which DAILY cycle it counts for. Take it in the time zone of slot 0, which also dates the cycles. The cycle counting test gave its times with a +01:00 offset, so on a runner in UTC the run started on the previous day and was rightly not counted for today. It now uses local times without offset and passes in local time zones from UTC-12 to UTC+14. Refs: #1548 Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 Assisted-by: Claude Code (claude-opus-5-5) --- src/akkudoktoreos/devices/genetic/homeappliance.py | 6 +++++- tests/test_genetichomeappliance.py | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/akkudoktoreos/devices/genetic/homeappliance.py b/src/akkudoktoreos/devices/genetic/homeappliance.py index 6ba3fd17..74f1101b 100644 --- a/src/akkudoktoreos/devices/genetic/homeappliance.py +++ b/src/akkudoktoreos/devices/genetic/homeappliance.py @@ -483,7 +483,11 @@ class HomeAppliance: ) self.pinned_start_datetime = started self.pinned_end_datetime = max(started.add(seconds=self.run_duration_seconds), now) - self.pinned_run_date = started.date() + # The start day in the time zone of the horizon, which also dates the cycles. + horizon_timezone = slot0_datetime.timezone + self.pinned_run_date = ( + started.in_timezone(horizon_timezone) if horizon_timezone is not None else started + ).date() if elapsed >= self.run_duration_seconds: logger.info( "Home appliance '{}': run started {} is past its duration of {} s " diff --git a/tests/test_genetichomeappliance.py b/tests/test_genetichomeappliance.py index a83d2afe..c5a6363d 100644 --- a/tests/test_genetichomeappliance.py +++ b/tests/test_genetichomeappliance.py @@ -459,13 +459,13 @@ class TestRunInProgress: SLOT0 = "2026-11-14T01:00:00+01:00" - def pinned(self, started: str, now: str, **overrides) -> HomeAppliance: + def pinned(self, started: str, now: str, slot0: str = SLOT0, **overrides) -> HomeAppliance: appliance = make_appliance( prediction_hours=overrides.pop("prediction_hours", 6), run_started_datetime=started, **overrides, ) - appliance.pin_run_in_progress(to_datetime(self.SLOT0), to_datetime(now)) + appliance.pin_run_in_progress(to_datetime(slot0), to_datetime(now)) return appliance def test_remaining_flat_run_is_placed_from_now(self): @@ -542,17 +542,21 @@ class TestRunInProgress: assert appliance.get_load_curve()[0] == pytest.approx(1000 * 55 / 60) def test_counts_as_a_cycle_of_its_start_day_and_delays_the_next(self): + # Local times without offset, so the start day is the same in every local timezone of the test run. + slot0 = "2026-11-14T01:00:00" appliance = self.pinned( - "2026-11-14T00:47:00+01:00", - "2026-11-14T01:05:00+01:00", + "2026-11-14T00:47:00", + "2026-11-14T01:05:00", + slot0=slot0, num_cycles=2, min_cycle_gap_h=1, schedule_mode="DAILY", ) - today = to_datetime(self.SLOT0).date() + today = to_datetime(slot0).date() assert appliance.completed_cycles_on(today) == 1 assert appliance.completed_cycles_on(today.add(days=1)) == 0 + assert appliance.completed_cycles_on(today.subtract(days=1)) == 0 # Ends 02:47 (slot 2 is the first free slot) plus 1 h gap. assert appliance.next_cycle_earliest_slot() == 3