From 1329ba5a07ae87ce8e1bb49ded52f84177ca2ba8 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Tue, 20 Oct 2026 23:05:26 -0400 Subject: [PATCH] Fix time pattern missing repeated match at non-hourly DST end (#187678) Co-authored-by: Claude Opus 5.5 --- homeassistant/util/dt.py | 30 ++++++++++++++-------- tests/util/test_dt.py | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/homeassistant/util/dt.py b/homeassistant/util/dt.py index 1c2ae249448d..cf2387c3e053 100644 --- a/homeassistant/util/dt.py +++ b/homeassistant/util/dt.py @@ -449,12 +449,23 @@ def parse_time_expression(parameter: Any, min_value: int, max_value: int) -> lis return res -def _dst_offset_diff(dattim: dt.datetime) -> dt.timedelta: - """Return the offset when crossing the DST barrier.""" - delta = dt.timedelta(hours=24) - return (dattim + delta).utcoffset() - ( # type: ignore[operator] - dattim - delta - ).utcoffset() +def _second_fold_start(dattim: dt.datetime) -> dt.datetime: + """Return the first wall time of the second fold of an ambiguous datetime. + + The result has fold=1. The transition is searched in UTC, between the + first and second fold readings of dattim, to the second. + """ + tzinfo = dattim.tzinfo + low = dattim.replace(microsecond=0, fold=0).astimezone(UTC) + first_fold_offset = low.astimezone(tzinfo).utcoffset() + high = dattim.replace(microsecond=0, fold=1).astimezone(UTC) + while (gap := int((high - low).total_seconds())) > 1: + middle = low + dt.timedelta(seconds=gap // 2) + if middle.astimezone(tzinfo).utcoffset() == first_fold_offset: + low = middle + else: + high = middle + return high.astimezone(tzinfo).replace(fold=1) def _lower_bound(arr: list[int], cmp: int) -> int | None: @@ -573,15 +584,14 @@ def find_next_time_expression_time( # longer matches within the fold). # -> Check if result matches in the next fold. If so, emit that match - # Turn back the time by the DST offset, effectively run the algorithm on - # the first fold. If it matches on the first fold, that means it will also - # match on the second one. + # Run the algorithm from the start of the second fold. The whole second + # fold comes after `now`, so its first match is the next one. # Example: 2021.10.31 02:45:00+02:00 with pattern 02:30 # -> 2021.10.31 02:30:00+01:00 check_result = find_next_time_expression_time( - now + _dst_offset_diff(now), seconds, minutes, hours + _second_fold_start(now), seconds, minutes, hours ) if _datetime_ambiguous(check_result): return check_result.replace(fold=1) diff --git a/tests/util/test_dt.py b/tests/util/test_dt.py index 8be826a83f5f..541a76de76f1 100644 --- a/tests/util/test_dt.py +++ b/tests/util/test_dt.py @@ -592,6 +592,61 @@ def test_find_next_time_expression_exiting_dst(now_dt, expected_dt) -> None: assert dt_util.as_utc(res_dt) == dt_util.as_utc(expected_dt) +# DST transitions that are not on a whole hour boundary. Lord Howe turns back +# 30 minutes on 2024.04.07 2:00, so 1:30-2:00 is ambiguous; Chatham turns back +# 1h on 2024.04.07 3:45, so 2:45-3:45 is ambiguous. +@pytest.mark.parametrize( + ("time_zone", "now_dt", "hour", "minute", "expected_dt"), + [ + pytest.param( + "Australia/Lord_Howe", + datetime(2024, 4, 7, 1, 50, 0, fold=0), + 1, + [25, 35], + datetime(2024, 4, 7, 1, 35, 0, fold=1), + id="lord_howe_skip_match_before_fold", + ), + pytest.param( + "Pacific/Chatham", + datetime(2024, 4, 7, 3, 0, 0, fold=0), + 2, + "*", + datetime(2024, 4, 7, 2, 45, 0, fold=1), + id="chatham_start_of_fold", + ), + pytest.param( + "Pacific/Chatham", + datetime(2024, 4, 7, 3, 30, 0, fold=0), + [2, 3], + 15, + datetime(2024, 4, 7, 3, 15, 0, fold=1), + id="chatham_match_in_fold", + ), + ], +) +def test_find_next_time_expression_exiting_dst_not_hour_aligned( + time_zone: str, + now_dt: datetime, + hour: int | list[int], + minute: int | list[int] | str, + expected_dt: datetime, +) -> None: + """Test exiting DST by less than or off an hour boundary.""" + tz = dt_util.get_time_zone(time_zone) + dt_util.set_default_time_zone(tz) + pattern_seconds = dt_util.parse_time_expression(0, 0, 59) + pattern_minutes = dt_util.parse_time_expression(minute, 0, 59) + pattern_hours = dt_util.parse_time_expression(hour, 0, 23) + + now_dt = now_dt.replace(tzinfo=tz) + expected_dt = expected_dt.replace(tzinfo=tz) + + res_dt = dt_util.find_next_time_expression_time( + now_dt, pattern_seconds, pattern_minutes, pattern_hours + ) + assert dt_util.as_utc(res_dt) == dt_util.as_utc(expected_dt) + + def test_find_next_time_expression_time_dst_chicago() -> None: """Test daylight saving time for find_next_time_expression_time.""" tz = dt_util.get_time_zone("America/Chicago")