From a31ef98e5714480394e413a6f01344344a2ee619 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sun, 17 Mar 2024 15:17:16 +1300 Subject: [PATCH] Calculate Easter date automatically --- LICENSE.md | 2 ++ init.lua | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/LICENSE.md b/LICENSE.md index 20458d3..26b1453 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -8,6 +8,8 @@ The license for the sounds are listed in the sounds folder. The easter stuff is CC0. +The date calculation algorithm in init.lua is [copied from dateutil](https://github.com/dateutil/dateutil/blob/1ae807774053c071acc9e7d3d27778fba0a7773e/src/dateutil/easter.py) and is therefore under the [Apache-2.0 license](https://github.com/dateutil/dateutil/blob/1ae807774053c071acc9e7d3d27778fba0a7773e/LICENSE). + ## Christmas The christmas stuff was written by Billy-S, w/ no given license. diff --git a/init.lua b/init.lua index 8459fe2..6970274 100644 --- a/init.lua +++ b/init.lua @@ -39,9 +39,29 @@ local function or_(...) end end +-- Easter calculation, copied from dateutil (see LICENSE) +local floor = math.floor +local function get_month_and_day(p) + return {month=3 + floor((p + 26)/30), day=1 + (p + 27 + floor((p + 6)/40)) % 31} +end + +local function is_easter(date) + local y = date.year + local g = y % 19 + local c = floor(y / 100) + local h = (c - floor(c/4) - floor((8*c + 13)/25) + 19*g + 15) % 30 + local i = h - (floor(h/28))*(1 - (floor(h/28))*floor(29/(h + 1))*floor((21 - g)/11)) + local j = (y + floor(y/4) + i + 2 - c + floor(c/4)) % 7 + local p = i - j + + local start = get_month_and_day(p - 4) + local stop = get_month_and_day(p + 1) + return date_lte(start, date) and date_lte(date, stop) +end + holidays.schedule = { christmas = date_range_predicate({month=12, day=1}, {month=12, day=26}), -- 2019 date - easter = date_range_predicate({month=3, day=27}, {month=4, day=1}), -- 2024 date + easter = is_easter, fireworks = or_( date_range_predicate({month=7, day=2}, {month=7, day=5}), -- july 4th date_range_predicate({month=12, day=27}, {month=1, day=10}) -- new years