54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""Swedish public holiday utilities for leave planning.
|
|
|
|
Uses the holidays library to get röda dagar (red days) in Sweden.
|
|
Results are cached per year to avoid repeated parsing.
|
|
"""
|
|
|
|
import datetime
|
|
from typing import Dict
|
|
|
|
import holidays
|
|
|
|
_holiday_cache: Dict[int, Dict[datetime.date, str]] = {}
|
|
|
|
|
|
def get_swedish_holidays(year: int) -> Dict[datetime.date, str]:
|
|
"""Get all Swedish public holidays (röda dagar) for a given year.
|
|
|
|
Returns a mapping of date -> holiday name in Swedish.
|
|
|
|
The result is cached per year; subsequent calls with the same year
|
|
return the cached value without re-parsing.
|
|
|
|
Args:
|
|
year: Calendar year (e.g., 2026)
|
|
|
|
Returns:
|
|
Dict mapping datetime.date to holiday name (e.g. "Juldagen", "Midsommar")
|
|
"""
|
|
if year in _holiday_cache:
|
|
return _holiday_cache[year]
|
|
|
|
# Get Swedish holidays for the year with Swedish-language names.
|
|
se_holidays = holidays.Sweden(years=year, language="sv")
|
|
|
|
# Convert to our format: date -> name
|
|
result: Dict[datetime.date, str] = {}
|
|
for holiday_date, holiday_name in sorted(se_holidays.items()):
|
|
result[holiday_date] = holiday_name
|
|
|
|
_holiday_cache[year] = result
|
|
return result
|
|
|
|
|
|
def is_swedish_holiday(date: datetime.date) -> bool:
|
|
"""Check if a date is a Swedish public holiday."""
|
|
holidays_for_year = get_swedish_holidays(date.year)
|
|
return date in holidays_for_year
|
|
|
|
|
|
def get_holiday_name(date: datetime.date) -> str | None:
|
|
"""Get the name of a Swedish public holiday, or None if not a holiday."""
|
|
holidays_for_year = get_swedish_holidays(date.year)
|
|
return holidays_for_year.get(date)
|