diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 95f41379ff5e..ecddaf952e8e 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -1003,6 +1003,10 @@ PERIOD_SCHEMA = probatio.Schema( ) +_HOUR = timedelta(hours=1) +_OFFSET_CHANGE_SCAN_STEP = timedelta(days=1) + + def _utc_offset_change(low: datetime, high: datetime) -> datetime: """Return when the local UTC offset changes between two UTC times. @@ -1036,15 +1040,68 @@ def _start_of_local_hour(utc_time: datetime) -> datetime: return start -def _start_of_next_local_hour(start: datetime) -> datetime: - """Return the start of the local calendar hour after the one starting at start.""" - next_start = _start_of_local_hour(start + timedelta(hours=1)) - if ( - dt_util.as_local(start).utcoffset() - != dt_util.as_local(next_start - timedelta(microseconds=1)).utcoffset() - ): - return _utc_offset_change(start, next_start - timedelta(microseconds=1)) - return next_start +def _local_hours_later(start: datetime, hours: int) -> datetime: + """Return the start of the local calendar hour a number of hours later. + + Jumps whole hours in UTC while the UTC offset is unchanged and only searches + for offset changes, which are assumed to be more than a day apart. + """ + while True: + local_start = dt_util.as_local(start) + offset = local_start.utcoffset() + next_top = ( + start + - timedelta( + minutes=local_start.minute, + seconds=local_start.second, + microseconds=local_start.microsecond, + ) + + _HOUR + ) + target = next_top + (hours - 1) * _HOUR + low = start + while low < target: + high = min(low + _OFFSET_CHANGE_SCAN_STEP, target) + if dt_util.as_local(high).utcoffset() != offset: + break + low = high + else: + return target + start = _utc_offset_change(low, high) + # Skip the hours that started before the offset change, and the change + hours -= max(0, -((next_top - start) // _HOUR)) + 1 + if not hours: + return start + + +def _local_hours_earlier(start: datetime, hours: int) -> datetime: + """Return the start of the local calendar hour a number of hours earlier. + + Jumps whole hours in UTC while the UTC offset is unchanged and only searches + for offset changes, which are assumed to be more than a day apart. + """ + while True: + high = start - timedelta(microseconds=1) + local_high = dt_util.as_local(high) + offset = local_high.utcoffset() + last_top = high - timedelta( + minutes=local_high.minute, + seconds=local_high.second, + microseconds=local_high.microsecond, + ) + target = last_top - (hours - 1) * _HOUR + while high > target: + low = max(high - _OFFSET_CHANGE_SCAN_STEP, target) + if dt_util.as_local(low).utcoffset() != offset: + break + high = low + else: + return target + start = _utc_offset_change(low, high) + # Skip the hours that started after the offset change, and the change + hours -= max(0, -((start - last_top) // _HOUR)) + 1 + if not hours: + return start def resolve_period( @@ -1060,13 +1117,11 @@ def resolve_period( cal_offset = period_def["calendar"].get("offset", 0) if calendar_period == "hour": start_time = _start_of_local_hour(dt_util.utcnow()) - for _ in range(-cal_offset): - start_time = _start_of_local_hour( - start_time - timedelta(microseconds=1) - ) - for _ in range(cal_offset): - start_time = _start_of_next_local_hour(start_time) - end_time = _start_of_next_local_hour(start_time) + if cal_offset > 0: + start_time = _local_hours_later(start_time, cal_offset) + elif cal_offset < 0: + start_time = _local_hours_earlier(start_time, -cal_offset) + end_time = _local_hours_later(start_time, 1) elif calendar_period == "day": start_time = start_of_day start_time += timedelta(days=cal_offset) diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index 859278192e3c..ac637ddf386b 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -1743,6 +1743,96 @@ def test_retryable_database_job_method( "2022-10-01T16:01:00+00:00", id="offset_change_within_hour", ), + pytest.param( + "Pacific/Chatham", + # 03:50 +13:45, after the spring-forward from 02:45 +12:45 + datetime(2022, 9, 24, 14, 5, tzinfo=UTC), + 0, + "2022-09-24T14:00:00+00:00", + "2022-09-24T14:15:00+00:00", + id="three_quarter_hour_spring_forward", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 9, 24, 14, 5, tzinfo=UTC), + -1, + "2022-09-24T13:15:00+00:00", + "2022-09-24T14:00:00+00:00", + id="three_quarter_hour_spring_forward_previous_hour", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 9, 24, 14, 5, tzinfo=UTC), + 1, + "2022-09-24T14:15:00+00:00", + "2022-09-24T15:15:00+00:00", + id="three_quarter_hour_spring_forward_next_hour", + ), + pytest.param( + "Pacific/Chatham", + # 02:55 +12:45, after the fall-back from 03:45 +13:45 + datetime(2022, 4, 2, 14, 10, tzinfo=UTC), + 0, + "2022-04-02T14:00:00+00:00", + "2022-04-02T14:15:00+00:00", + id="three_quarter_hour_fall_back", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 4, 2, 14, 10, tzinfo=UTC), + -1, + "2022-04-02T13:15:00+00:00", + "2022-04-02T14:00:00+00:00", + id="three_quarter_hour_fall_back_previous_hour", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 4, 2, 14, 10, tzinfo=UTC), + 1, + "2022-04-02T14:15:00+00:00", + "2022-04-02T15:15:00+00:00", + id="three_quarter_hour_fall_back_next_hour", + ), + pytest.param( + "US/Pacific", + datetime(2022, 11, 6, 9, 30, tzinfo=UTC), + 10**6, + "2136-12-05T01:00:00+00:00", + "2136-12-05T02:00:00+00:00", + id="huge_offset", + ), + pytest.param( + "Australia/Lord_Howe", + datetime(2022, 10, 1, 15, 45, tzinfo=UTC), + 100000, + "2034-02-26T20:00:00+00:00", + "2034-02-26T21:00:00+00:00", + id="large_offset_half_hour_dst", + ), + pytest.param( + "Australia/Lord_Howe", + datetime(2022, 10, 1, 15, 45, tzinfo=UTC), + -100000, + "2011-05-06T10:30:00+00:00", + "2011-05-06T11:30:00+00:00", + id="large_negative_offset_half_hour_dst", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 9, 24, 14, 5, tzinfo=UTC), + 100000, + "2034-02-19T07:15:00+00:00", + "2034-02-19T08:15:00+00:00", + id="large_offset_three_quarter_hour_dst", + ), + pytest.param( + "Pacific/Chatham", + datetime(2022, 9, 24, 14, 5, tzinfo=UTC), + -100000, + "2011-04-29T20:15:00+00:00", + "2011-04-29T21:15:00+00:00", + id="large_negative_offset_three_quarter_hour_dst", + ), ], ) async def test_resolve_period_hour_dst(