Improve recorder util resolve_period (#132264)

This commit is contained in:
Erik Montnemery
2024-12-06 21:06:56 +01:00
committed by GitHub
parent 40239945c1
commit d26d483a2f
2 changed files with 27 additions and 7 deletions

View File

@ -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)