"""United States federal holiday calendar, from the statute, in stdlib only. This module is part of the RELEASED SCORER. It imports nothing outside the standard library, on purpose: a reviewer holding the released files must be able to re-derive every gold answer with a stock Python 3.11 to 3.13 interpreter and no pip install. WHAT IS AND IS NOT IN HERE -------------------------- The eleven legal public holidays for federal employees under 5 U.S.C. 6103(a), and nothing else: New Year's Day January 1 Birthday of Martin Luther King, Jr. third Monday in January Washington's Birthday third Monday in February Memorial Day last Monday in May Juneteenth National Independence Day June 19 (from 2021) Independence Day July 4 Labor Day first Monday in September Columbus Day second Monday in October Veterans Day November 11 Thanksgiving Day fourth Thursday in November Christmas Day December 25 Observance shift, 5 U.S.C. 6103(b) as implemented for all employees by E.O. 11582: a holiday falling on a Saturday is observed on the preceding Friday; one falling on a Sunday is observed on the following Monday. Only the five fixed-date holidays can ever move; the Monday and Thursday holidays cannot. DECLARED EXCLUSIONS, stated here because a silent omission is indistinguishable from a bug: Inauguration Day (5 U.S.C. 6103(c)) is excluded. It is a holiday only for employees in the District of Columbia area, so it is not a nationwide closure, and it would make the calendar depend on where the employee sits. One-off closures created by executive order are excluded. Examples inside the supported range: December 24 2021 (E.O. 14063) and January 9 2025 (national day of mourning). These are not statutory, are not predictable from any rule, and are therefore not part of a calendar that must be computable for 2031 to 2035. Note that December 24 2021 IS in the table below, but for the statutory reason: Christmas Day 2021 fell on a Saturday and was observed on Friday December 24. The executive order was redundant with the statute that year. CALENDAR YEAR VS STATUTORY YEAR. New Year's Day for statutory year Y is observed on December 31 of year Y-1 whenever January 1 of year Y is a Saturday. Inside the supported range that happens twice, producing closures on 2021-12-31 and 2027-12-31. The table is keyed by the OBSERVED date, so those two entries live in the calendar year before the statutory year that produced them. Code that slices by calendar year must account for it; this module does, and its selftest asserts it. VERIFICATION. FROZEN below is the hardcoded 2020-2030 table the released scorer uses. It is asserted equal to the rule generator at import time, so an accidental edit to either one fails immediately rather than quietly changing gold answers. It is separately asserted equal to `holidays.US(observed=True)` for every year 2020 to 2030 by crosscheck_fedcal.py, which is a BUILD-TIME tool and is never imported from here. The `holidays` package is not a dependency of anything in the released scoring path. The cross-check earned its place before it was even written: the first draft of `_last_weekday` returned the last day-of-month congruent to 28 rather than the last day of the month, which put Memorial Day on the fourth Monday in May instead of the last one. It was correct in 2020, 2024, 2025, 2026, 2029 and 2030 and wrong in 2021, 2022, 2023, 2027 and 2028. Nothing about the output looked wrong. """ from __future__ import annotations from datetime import date, timedelta # 5 U.S.C. 6103(a), in statutory order. # (name, rule, first statutory year) # ("fixed", month, day) -> that calendar date, then the observance shift # ("nth", month, weekday, n) -> nth of the month, no shift possible # ("last", month, weekday) -> last of the month, no shift possible # weekday is Monday=0 as in date.weekday(). RULES: tuple[tuple[str, tuple, int], ...] = ( ("New Year's Day", ("fixed", 1, 1), 1871), ("Birthday of Martin Luther King, Jr.", ("nth", 1, 0, 3), 1986), ("Washington's Birthday", ("nth", 2, 0, 3), 1971), ("Memorial Day", ("last", 5, 0), 1971), ("Juneteenth National Independence Day", ("fixed", 6, 19), 2021), ("Independence Day", ("fixed", 7, 4), 1871), ("Labor Day", ("nth", 9, 0, 1), 1894), ("Columbus Day", ("nth", 10, 0, 2), 1971), ("Veterans Day", ("fixed", 11, 11), 1971), ("Thanksgiving Day", ("nth", 11, 3, 4), 1871), ("Christmas Day", ("fixed", 12, 25), 1871), ) # Statutory years the corpus is allowed to touch. Queries outside this window RAISE. # Returning "not a holiday" for an unsupported year would silently produce wrong gold # answers, which is the failure mode this project has been bitten by most often. FIRST_YEAR = 2020 # 2037, not 2035: the corpus questions stop at 2035, but solver_b's F4 checker scans a # deliberately wider month window than solver_a, and from a late-2035 anchor that scan # touches 2036-2037. The guard must cover every date either solver can legitimately # ask about, or range-edge scenarios die on the asymmetry (one did, in the first # corpus build). Cross-checked against the holidays package through 2037. LAST_YEAR = 2037 # Excluded by declaration, asserted absent by the selftest and by the cross-check. EXCLUDED_DATES: dict[str, str] = { "2021-01-20": "Inauguration Day, District of Columbia area only, 5 U.S.C. 6103(c)", "2025-01-09": "national day of mourning, one-off executive order, not statutory", } def _days_in_month(year: int, month: int) -> int: if month == 12: return 31 return (date(year, month + 1, 1) - date(year, month, 1)).days def _nth_weekday(year: int, month: int, weekday: int, n: int) -> date: """The nth of a month, counting from 1.""" first = date(year, month, 1) first += timedelta(days=(weekday - first.weekday()) % 7) out = first + timedelta(days=7 * (n - 1)) if out.month != month: raise ValueError(f"no {n}th weekday {weekday} in {year}-{month:02d}") return out def _last_weekday(year: int, month: int, weekday: int) -> date: """The last of a month. Written as "step back from the last day of the month", not as "step forward from day 28", because the second form silently degrades to the fourth occurrence in every month where a fifth exists. See the module docstring. """ eom = date(year, month, _days_in_month(year, month)) return eom - timedelta(days=(eom.weekday() - weekday) % 7) def actual_date(year: int, rule: tuple) -> date: kind = rule[0] if kind == "fixed": return date(year, rule[1], rule[2]) if kind == "nth": return _nth_weekday(year, rule[1], rule[2], rule[3]) if kind == "last": return _last_weekday(year, rule[1], rule[2]) raise ValueError(f"unknown rule {rule!r}") def observed_date(actual: date) -> date: """5 U.S.C. 6103(b): Saturday moves back to Friday, Sunday moves up to Monday.""" if actual.weekday() == 5: return actual - timedelta(days=1) if actual.weekday() == 6: return actual + timedelta(days=1) return actual def from_rules(year: int) -> dict[date, str]: """The observed closure dates produced by statutory year `year`. Keys can fall in year-1 (New Year's Day observed on December 31). """ out: dict[date, str] = {} for name, rule, first in RULES: if year < first: continue out[observed_date(actual_date(year, rule))] = name return out def _build(y0: int, y1: int) -> dict[date, str]: out: dict[date, str] = {} for y in range(y0, y1 + 1): for d, name in from_rules(y).items(): out[d] = name return out # The hardcoded 2020-2030 table, keyed by observed date. Generated from the rules # above and asserted equal to them at import time. FROZEN: dict[str, str] = { "2020-01-01": "New Year's Day", "2020-01-20": "Birthday of Martin Luther King, Jr.", "2020-02-17": "Washington's Birthday", "2020-05-25": "Memorial Day", "2020-07-03": "Independence Day", "2020-09-07": "Labor Day", "2020-10-12": "Columbus Day", "2020-11-11": "Veterans Day", "2020-11-26": "Thanksgiving Day", "2020-12-25": "Christmas Day", "2021-01-01": "New Year's Day", "2021-01-18": "Birthday of Martin Luther King, Jr.", "2021-02-15": "Washington's Birthday", "2021-05-31": "Memorial Day", "2021-06-18": "Juneteenth National Independence Day", "2021-07-05": "Independence Day", "2021-09-06": "Labor Day", "2021-10-11": "Columbus Day", "2021-11-11": "Veterans Day", "2021-11-25": "Thanksgiving Day", "2021-12-24": "Christmas Day", "2021-12-31": "New Year's Day", "2022-01-17": "Birthday of Martin Luther King, Jr.", "2022-02-21": "Washington's Birthday", "2022-05-30": "Memorial Day", "2022-06-20": "Juneteenth National Independence Day", "2022-07-04": "Independence Day", "2022-09-05": "Labor Day", "2022-10-10": "Columbus Day", "2022-11-11": "Veterans Day", "2022-11-24": "Thanksgiving Day", "2022-12-26": "Christmas Day", "2023-01-02": "New Year's Day", "2023-01-16": "Birthday of Martin Luther King, Jr.", "2023-02-20": "Washington's Birthday", "2023-05-29": "Memorial Day", "2023-06-19": "Juneteenth National Independence Day", "2023-07-04": "Independence Day", "2023-09-04": "Labor Day", "2023-10-09": "Columbus Day", "2023-11-10": "Veterans Day", "2023-11-23": "Thanksgiving Day", "2023-12-25": "Christmas Day", "2024-01-01": "New Year's Day", "2024-01-15": "Birthday of Martin Luther King, Jr.", "2024-02-19": "Washington's Birthday", "2024-05-27": "Memorial Day", "2024-06-19": "Juneteenth National Independence Day", "2024-07-04": "Independence Day", "2024-09-02": "Labor Day", "2024-10-14": "Columbus Day", "2024-11-11": "Veterans Day", "2024-11-28": "Thanksgiving Day", "2024-12-25": "Christmas Day", "2025-01-01": "New Year's Day", "2025-01-20": "Birthday of Martin Luther King, Jr.", "2025-02-17": "Washington's Birthday", "2025-05-26": "Memorial Day", "2025-06-19": "Juneteenth National Independence Day", "2025-07-04": "Independence Day", "2025-09-01": "Labor Day", "2025-10-13": "Columbus Day", "2025-11-11": "Veterans Day", "2025-11-27": "Thanksgiving Day", "2025-12-25": "Christmas Day", "2026-01-01": "New Year's Day", "2026-01-19": "Birthday of Martin Luther King, Jr.", "2026-02-16": "Washington's Birthday", "2026-05-25": "Memorial Day", "2026-06-19": "Juneteenth National Independence Day", "2026-07-03": "Independence Day", "2026-09-07": "Labor Day", "2026-10-12": "Columbus Day", "2026-11-11": "Veterans Day", "2026-11-26": "Thanksgiving Day", "2026-12-25": "Christmas Day", "2027-01-01": "New Year's Day", "2027-01-18": "Birthday of Martin Luther King, Jr.", "2027-02-15": "Washington's Birthday", "2027-05-31": "Memorial Day", "2027-06-18": "Juneteenth National Independence Day", "2027-07-05": "Independence Day", "2027-09-06": "Labor Day", "2027-10-11": "Columbus Day", "2027-11-11": "Veterans Day", "2027-11-25": "Thanksgiving Day", "2027-12-24": "Christmas Day", "2027-12-31": "New Year's Day", "2028-01-17": "Birthday of Martin Luther King, Jr.", "2028-02-21": "Washington's Birthday", "2028-05-29": "Memorial Day", "2028-06-19": "Juneteenth National Independence Day", "2028-07-04": "Independence Day", "2028-09-04": "Labor Day", "2028-10-09": "Columbus Day", "2028-11-10": "Veterans Day", "2028-11-23": "Thanksgiving Day", "2028-12-25": "Christmas Day", "2029-01-01": "New Year's Day", "2029-01-15": "Birthday of Martin Luther King, Jr.", "2029-02-19": "Washington's Birthday", "2029-05-28": "Memorial Day", "2029-06-19": "Juneteenth National Independence Day", "2029-07-04": "Independence Day", "2029-09-03": "Labor Day", "2029-10-08": "Columbus Day", "2029-11-12": "Veterans Day", "2029-11-22": "Thanksgiving Day", "2029-12-25": "Christmas Day", "2030-01-01": "New Year's Day", "2030-01-21": "Birthday of Martin Luther King, Jr.", "2030-02-18": "Washington's Birthday", "2030-05-27": "Memorial Day", "2030-06-19": "Juneteenth National Independence Day", "2030-07-04": "Independence Day", "2030-09-02": "Labor Day", "2030-10-14": "Columbus Day", "2030-11-11": "Veterans Day", "2030-11-28": "Thanksgiving Day", "2030-12-25": "Christmas Day", } # Built once. FIRST_YEAR-1 is included so a December 31 observance produced by # statutory year FIRST_YEAR is present; LAST_YEAR+1 for the same reason at the top. _MAP: dict[date, str] = _build(FIRST_YEAR, LAST_YEAR + 1) # Import-time integrity: the frozen literal and the rules must agree exactly over # 2020-2030. If they ever diverge, every gold answer in the corpus is suspect, so this # is an assert and not a warning. _frozen_dates = {date.fromisoformat(k): v for k, v in FROZEN.items()} _rule_dates = _build(2020, 2030) if _frozen_dates != _rule_dates: only_f = sorted(d.isoformat() for d in set(_frozen_dates) - set(_rule_dates)) only_r = sorted(d.isoformat() for d in set(_rule_dates) - set(_frozen_dates)) raise AssertionError( f"fedcal FROZEN table disagrees with the statutory rules. " f"only in FROZEN: {only_f}; only from rules: {only_r}") def _check_range(d: date) -> None: # New Year's Day for statutory year LAST_YEAR+1 can be observed on Dec 31 of # LAST_YEAR, so the usable date window is [FIRST_YEAR-01-01, LAST_YEAR-12-31]. if not (FIRST_YEAR <= d.year <= LAST_YEAR): raise ValueError( f"{d.isoformat()} is outside the supported range " f"{FIRST_YEAR}-01-01 to {LAST_YEAR}-12-31. Extend LAST_YEAR and re-run " f"crosscheck_fedcal.py rather than assuming the answer.") def is_holiday(d: date) -> bool: _check_range(d) return d in _MAP def holiday_name(d: date) -> str | None: _check_range(d) return _MAP.get(d) def is_business_day(d: date) -> bool: """Monday to Friday and not an observed federal holiday.""" _check_range(d) return d.weekday() < 5 and d not in _MAP def holidays_in_year(year: int) -> list[tuple[date, str]]: """Observed closures falling in CALENDAR year `year`, in date order. Used to build the enumerated-calendar prompt variant, so the list a prompt shows is produced by the same table the scorer uses. A prompt that enumerated a different calendar from the one the gold answer was computed under would be an unfair, and undetectable, corpus bug. """ if not (FIRST_YEAR <= year <= LAST_YEAR): raise ValueError(f"year {year} outside supported range") return sorted((d, n) for d, n in _MAP.items() if d.year == year) def _selftest() -> None: ok = 0 # 1. The frozen table is the size the statute implies: 10 holidays in 2020, # 11 in each of 2021 to 2030. assert len(FROZEN) == 10 + 11 * 10, len(FROZEN) ok += 1 # 2. Memorial Day is the LAST Monday in May, not the fourth. This is the bug the # cross-check caught, so it gets a permanent regression test. for y, want in ((2021, "2021-05-31"), (2022, "2022-05-30"), (2023, "2023-05-29"), (2027, "2027-05-31"), (2028, "2028-05-29")): got = _last_weekday(y, 5, 0).isoformat() assert got == want, f"Memorial Day {y}: got {got}, want {want}" assert _last_weekday(2020, 5, 0).isoformat() == "2020-05-25" ok += 1 # 3. Observance shift in both directions, including the year boundary. assert observed_date(date(2026, 7, 4)).isoformat() == "2026-07-03" # Sat back assert observed_date(date(2021, 7, 4)).isoformat() == "2021-07-05" # Sun forward assert observed_date(date(2024, 7, 4)).isoformat() == "2024-07-04" # weekday assert is_holiday(date(2021, 12, 31)), "New Year's 2022 observed on 2021-12-31" assert is_holiday(date(2027, 12, 31)), "New Year's 2028 observed on 2027-12-31" assert not is_holiday(date(2022, 1, 1)), "the Saturday itself is not the closure" ok += 1 # 4. Juneteenth exists from 2021 and not before. assert not is_holiday(date(2020, 6, 19)) assert is_holiday(date(2021, 6, 18)) assert holiday_name(date(2024, 6, 19)) == "Juneteenth National Independence Day" ok += 1 # 5. Declared exclusions really are absent. for iso, why in EXCLUDED_DATES.items(): assert date.fromisoformat(iso) not in _MAP, f"{iso} must be excluded ({why})" ok += 1 # 6. Business days. assert is_business_day(date(2026, 11, 20)) # a plain Friday assert not is_business_day(date(2026, 11, 21)) # Saturday assert not is_business_day(date(2026, 11, 26)) # Thanksgiving assert not is_business_day(date(2026, 7, 3)) # observed Independence Day assert is_business_day(date(2026, 7, 6)) # the Monday after ok += 1 # 7. Out-of-range queries raise instead of answering "not a holiday". Proving the # guard fires is the point; an unproven guard is not a guard. for bad in (date(FIRST_YEAR - 1, 6, 1), date(LAST_YEAR + 1, 6, 1)): try: is_holiday(bad) raise AssertionError(f"expected ValueError for {bad}") except ValueError: pass ok += 1 # 8. holidays_in_year is keyed by CALENDAR year, so 2021 carries twelve entries: # its own eleven plus New Year's Day 2022 observed on 2021-12-31. y2021 = holidays_in_year(2021) assert len(y2021) == 12, [d.isoformat() for d, _ in y2021] assert y2021[-1][0].isoformat() == "2021-12-31" assert len(holidays_in_year(2020)) == 10 assert len(holidays_in_year(2025)) == 11 ok += 1 # 9. The rules keep working past the frozen table, which is what makes the # 2031-2035 range slice computable at all. assert len(from_rules(2033)) == 11 assert is_holiday(date(2035, 12, 25)) ok += 1 # 10. Prove the import-time integrity assert can fail, by running the same # comparison against a deliberately corrupted copy. corrupt = dict(_frozen_dates) corrupt[date(2026, 7, 3)] = "Wrong Name" assert corrupt != _rule_dates, "the FROZEN-vs-rules comparison cannot detect a change" corrupt2 = dict(_frozen_dates) del corrupt2[date(2026, 11, 26)] assert corrupt2 != _rule_dates ok += 1 print(f"fedcal selftest: {ok}/10 OK " f"({len(FROZEN)} frozen closures 2020-2030, rules cover {FIRST_YEAR}-{LAST_YEAR})") if __name__ == "__main__": _selftest()