"""Smart day filters (owner 2026-11-21): the 1st / last day of the month, its first / last week, and Greek public holidays (fixed dates and those that follow the Orthodox Easter). The same rules in JavaScript: packages/signage/src/logic.js (a screen applies its schedule while the server is down); both sides are tested with the same dates. """ from __future__ import annotations import calendar from datetime import date, timedelta # the filters a schedule line can have: any one that holds is enough, except "not_holiday", # which keeps the line off on public holidays MONTH_DAYS = ("first", "last", "first_week", "last_week", "holiday", "not_holiday") _FIXED = ((1, 1), (1, 6), (3, 25), (5, 1), (8, 15), (10, 28), (12, 25), (12, 26)) # days from the Orthodox Easter Sunday: Clean Monday, Good Friday, Easter Sunday, Easter Monday, Whit Monday _FROM_EASTER = (-48, -2, 0, 1, 50) def orthodox_easter(year: int) -> date: """The Orthodox Easter Sunday (Meeus' Julian algorithm, + 13 days to the Gregorian calendar, 1900-2099).""" a, b, c = year % 4, year % 7, year % 19 d = (19 * c + 15) % 30 e = (2 * a + 4 * b - d + 34) % 7 month = (d + e + 114) // 31 day = (d + e + 114) % 31 + 1 return date(year, month, day) + timedelta(days=13) def is_holiday(day: date) -> bool: """A Greek public holiday (nationwide).""" if (day.month, day.day) in _FIXED: return True easter = orthodox_easter(day.year) return any(day == easter + timedelta(days=n) for n in _FROM_EASTER) def month_days_ok(tokens, day: date) -> bool: """Does a day pass a line's smart filters (empty = every day)?""" tokens = [t for t in (tokens or []) if t in MONTH_DAYS] if not tokens: return True hol = is_holiday(day) if "not_holiday" in tokens and hol: return False wanted = [t for t in tokens if t != "not_holiday"] if not wanted: return True last = calendar.monthrange(day.year, day.month)[1] return any((t == "first" and day.day == 1) or (t == "last" and day.day == last) or (t == "first_week" and day.day <= 7) or (t == "last_week" and day.day > last - 7) or (t == "holiday" and hol) for t in wanted)