Skip to content

Commit

Permalink
Improve recorder util resolve_period (#132264)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Dec 6, 2024
1 parent 4023994 commit d26d483
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 6 additions & 7 deletions homeassistant/components/recorder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions tests/components/recorder/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
),
(
Expand All @@ -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"),
},
),
],
Expand Down

0 comments on commit d26d483

Please sign in to comment.