azube/core/util.py
2024-12-05 13:45:51 +01:00

32 lines
1 KiB
Python

import datetime
def next_date(year: int, week: int) -> (int, int):
if week >= 52:
return (year + 1, 1)
else:
return (year, week + 1)
def first_day_of_iso_week(year, week):
# Create a date object for January 4th of the given year
# This date is always in the first week of the year according to ISO 8601
jan4 = datetime.datetime(year, 1, 4)
# Get the ISO week number and the weekday of January 4th
start_iso_week, start_iso_day = jan4.isocalendar()[1:3]
# Calculate the difference in weeks, then adjust for the start of the week
weeks_diff = week - start_iso_week
days_to_monday = datetime.timedelta(days=(1 - start_iso_day))
# Calculate the first day of the given ISO week
return jan4 + days_to_monday + datetime.timedelta(weeks=weeks_diff)
def get_week_range(p_year, p_week):
firstdayofweek = datetime.datetime.strptime(
f"{p_year}-W{int(p_week)}-1", "%Y-W%W-%w"
).date()
lastdayofweek = firstdayofweek + datetime.timedelta(days=6.9)
return firstdayofweek, lastdayofweek