From 6511e32926c9982dc903fb839fe1505148be68dc Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 4 Dec 2024 14:03:53 +0100 Subject: [PATCH] Improve recorder util resolve_period --- homeassistant/components/recorder/util.py | 13 ++++++------- tests/components/recorder/test_util.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 125b354211eb5d..2e7ac0c092dc7c 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -892,15 +892,14 @@ def resolve_period( start_time += timedelta(days=cal_offset * 7) end_time = start_time + timedelta(weeks=1) elif calendar_period == "month": - start_time = start_of_day.replace(day=28) - # This works for up to 48 months of offset - start_time = (start_time + timedelta(days=cal_offset * 31)).replace(day=1) + month_now = start_of_day.month + new_month = (month_now - 1 + cal_offset) % 12 + 1 + new_year = start_of_day.year + (month_now - 1 + cal_offset) // 12 + start_time = start_of_day.replace(year=new_year, month=new_month, day=1) end_time = (start_time + timedelta(days=31)).replace(day=1) else: # calendar_period = "year" - start_time = start_of_day.replace(month=12, day=31) - # This works for 100+ years of offset - start_time = (start_time + timedelta(days=cal_offset * 366)).replace( - month=1, day=1 + start_time = start_of_day.replace( + year=start_of_day.year + cal_offset, month=1, day=1 ) end_time = (start_time + timedelta(days=366)).replace(day=1) diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index 2514c38e105878..99bd5083489b75 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -1062,14 +1062,25 @@ def all(self): { ("hour", 0): ("2022-10-21T07:00:00", "2022-10-21T08:00:00"), ("hour", -1): ("2022-10-21T06:00:00", "2022-10-21T07:00:00"), + ("hour", 1): ("2022-10-21T08:00:00", "2022-10-21T09:00:00"), ("day", 0): ("2022-10-21T07:00:00", "2022-10-22T07:00:00"), ("day", -1): ("2022-10-20T07:00:00", "2022-10-21T07:00:00"), + ("day", 1): ("2022-10-22T07:00:00", "2022-10-23T07:00:00"), ("week", 0): ("2022-10-17T07:00:00", "2022-10-24T07:00:00"), ("week", -1): ("2022-10-10T07:00:00", "2022-10-17T07:00:00"), + ("week", 1): ("2022-10-24T07:00:00", "2022-10-31T07:00:00"), ("month", 0): ("2022-10-01T07:00:00", "2022-11-01T07:00:00"), ("month", -1): ("2022-09-01T07:00:00", "2022-10-01T07:00:00"), + ("month", -12): ("2021-10-01T07:00:00", "2021-11-01T07:00:00"), + ("month", 1): ("2022-11-01T07:00:00", "2022-12-01T08:00:00"), + ("month", 2): ("2022-12-01T08:00:00", "2023-01-01T08:00:00"), + ("month", 3): ("2023-01-01T08:00:00", "2023-02-01T08:00:00"), + ("month", 12): ("2023-10-01T07:00:00", "2023-11-01T07:00:00"), + ("month", 13): ("2023-11-01T07:00:00", "2023-12-01T08:00:00"), + ("month", 14): ("2023-12-01T08:00:00", "2024-01-01T08:00:00"), ("year", 0): ("2022-01-01T08:00:00", "2023-01-01T08:00:00"), ("year", -1): ("2021-01-01T08:00:00", "2022-01-01T08:00:00"), + ("year", 1): ("2023-01-01T08:00:00", "2024-01-01T08:00:00"), }, ), ( @@ -1078,14 +1089,24 @@ def all(self): { ("hour", 0): ("2024-02-28T08:00:00", "2024-02-28T09:00:00"), ("hour", -1): ("2024-02-28T07:00:00", "2024-02-28T08:00:00"), + ("hour", 1): ("2024-02-28T09:00:00", "2024-02-28T10:00:00"), ("day", 0): ("2024-02-28T08:00:00", "2024-02-29T08:00:00"), ("day", -1): ("2024-02-27T08:00:00", "2024-02-28T08:00:00"), + ("day", 1): ("2024-02-29T08:00:00", "2024-03-01T08:00:00"), ("week", 0): ("2024-02-26T08:00:00", "2024-03-04T08:00:00"), ("week", -1): ("2024-02-19T08:00:00", "2024-02-26T08:00:00"), + ("week", 1): ("2024-03-04T08:00:00", "2024-03-11T07:00:00"), ("month", 0): ("2024-02-01T08:00:00", "2024-03-01T08:00:00"), ("month", -1): ("2024-01-01T08:00:00", "2024-02-01T08:00:00"), + ("month", -2): ("2023-12-01T08:00:00", "2024-01-01T08:00:00"), + ("month", -3): ("2023-11-01T07:00:00", "2023-12-01T08:00:00"), + ("month", -12): ("2023-02-01T08:00:00", "2023-03-01T08:00:00"), + ("month", -13): ("2023-01-01T08:00:00", "2023-02-01T08:00:00"), + ("month", -14): ("2022-12-01T08:00:00", "2023-01-01T08:00:00"), + ("month", 1): ("2024-03-01T08:00:00", "2024-04-01T07:00:00"), ("year", 0): ("2024-01-01T08:00:00", "2025-01-01T08:00:00"), ("year", -1): ("2023-01-01T08:00:00", "2024-01-01T08:00:00"), + ("year", 1): ("2025-01-01T08:00:00", "2026-01-01T08:00:00"), }, ), ],