holidays.py

61 Zeilen · 2,5 KB · Python
Rohdatei
"""Gesetzliche Feiertage in Deutschland nach Bundesland (ohne regional begrenzte Feiertage
wie Augsburger Friedensfest oder Fronleichnam in Teilen von Sachsen/Thüringen)."""
from datetime import date, timedelta
from functools import lru_cache

STATES = {
    "BW": "Baden-Württemberg", "BY": "Bayern", "BE": "Berlin", "BB": "Brandenburg", "HB": "Bremen",
    "HH": "Hamburg", "HE": "Hessen", "MV": "Mecklenburg-Vorpommern", "NI": "Niedersachsen",
    "NW": "Nordrhein-Westfalen", "RP": "Rheinland-Pfalz", "SL": "Saarland", "SN": "Sachsen",
    "ST": "Sachsen-Anhalt", "SH": "Schleswig-Holstein", "TH": "Thüringen",
}


def easter(year: int) -> date:
    """Ostersonntag (Gauß/Anonymer gregorianischer Algorithmus)."""
    a = year % 19
    b, c = divmod(year, 100)
    d, e = divmod(b, 4)
    f = (b + 8) // 25
    g = (b - f + 1) // 3
    h = (19 * a + b - d - g + 15) % 30
    i, k = divmod(c, 4)
    l = (32 + 2 * e + 2 * i - h - k) % 7
    m = (a + 11 * h + 22 * l) // 451
    month, day = divmod(h + l - 7 * m + 114, 31)
    return date(year, month, day + 1)


@lru_cache(maxsize=64)
def holidays(year: int, state: str = "") -> dict[date, str]:
    state = (state or "").upper()
    e = easter(year)
    h = {
        date(year, 1, 1): "Neujahr",
        e - timedelta(days=2): "Karfreitag",
        e + timedelta(days=1): "Ostermontag",
        date(year, 5, 1): "Tag der Arbeit",
        e + timedelta(days=39): "Christi Himmelfahrt",
        e + timedelta(days=50): "Pfingstmontag",
        date(year, 10, 3): "Tag der Deutschen Einheit",
        date(year, 12, 25): "1. Weihnachtstag",
        date(year, 12, 26): "2. Weihnachtstag",
    }
    if state in ("BW", "BY", "ST"):
        h[date(year, 1, 6)] = "Heilige Drei Könige"
    if state == "BE" or (state == "MV" and year >= 2023):
        h[date(year, 3, 8)] = "Internationaler Frauentag"
    if state in ("BW", "BY", "HE", "NW", "RP", "SL"):
        h[e + timedelta(days=60)] = "Fronleichnam"
    if state == "SL":
        h[date(year, 8, 15)] = "Mariä Himmelfahrt"
    if state == "TH" and year >= 2019:
        h[date(year, 9, 20)] = "Weltkindertag"
    if state in ("BB", "MV", "SN", "ST", "TH") or (state in ("HB", "HH", "NI", "SH") and year >= 2018):
        h[date(year, 10, 31)] = "Reformationstag"
    if state in ("BW", "BY", "NW", "RP", "SL"):
        h[date(year, 11, 1)] = "Allerheiligen"
    if state == "SN":
        nov22 = date(year, 11, 22)
        h[nov22 - timedelta(days=(nov22.weekday() - 2) % 7)] = "Buß- und Bettag"
    return h