38 lines
1008 B
Python
38 lines
1008 B
Python
from datetime import date
|
|
|
|
|
|
DEFAULT_WORKING_DAYS = (0, 1, 2, 3, 4)
|
|
|
|
|
|
def normalize_working_days(days: list[int] | set[int] | tuple[int, ...]) -> list[int]:
|
|
normalized = sorted({int(day) for day in days if 0 <= int(day) <= 6})
|
|
if not normalized:
|
|
return list(DEFAULT_WORKING_DAYS)
|
|
return normalized
|
|
|
|
|
|
def serialize_working_days(days: list[int] | set[int] | tuple[int, ...]) -> str:
|
|
return ",".join(str(day) for day in normalize_working_days(days))
|
|
|
|
|
|
def parse_working_days_csv(value: str | None) -> set[int]:
|
|
if not value:
|
|
return set(DEFAULT_WORKING_DAYS)
|
|
|
|
parsed: list[int] = []
|
|
for part in value.split(","):
|
|
item = part.strip()
|
|
if not item:
|
|
continue
|
|
try:
|
|
parsed.append(int(item))
|
|
except ValueError:
|
|
continue
|
|
|
|
normalized = normalize_working_days(parsed)
|
|
return set(normalized)
|
|
|
|
|
|
def is_workday(day: date, relevant_weekdays: set[int]) -> bool:
|
|
return day.weekday() in relevant_weekdays
|