将Shrove Tuesday在Lua中实现

如何从提供的年份中获取Shrove Tuesday(12/02/201304/03/201417/02/2015等)的日期?如果可能的话,能否清楚地说明,以便适应复活节,母亲节和其他每年变化的假期?网上有一些获取复活节的脚本,但它们没有被解释得很清楚,我不明白如何将它们改为Shrove Tuesday和其他假期。

点赞
用户1009479
用户1009479

根据 维基百科,肥肉星期二就是复活节星期天的前47天。所以关键在于如何计算复活节,一个可移动的节日。你可以修改计算复活节的代码来获取肥肉星期二。

function shrove_tuesday(year)
    local leap_year
    if year % 4 == 0 then
        if year % 100 == 0 then
            if year % 400 == 0 then
                leap_year = true
            else
                leap_year = false
            end
        else
            leap_year = true
        end
    else
        leap_year = false
    end
    local a = year % 19
    local b = math.floor(year / 100)
    local c = year % 100
    local d = math.floor(b / 4)
    local e = b % 4
    local f = math.floor((b + 8) / 25)
    local g = math.floor((b - f + 1) / 3)
    local h = (19 * a + b - d - g + 15) % 30
    local i = math.floor(c / 4)
    local k = c % 4
    local L = (32 + 2 * e + 2 * i - h - k) % 7
    local m = math.floor((a + 11 * h + 22 * L) / 451)
    local month = math.floor((h + L - 7 * m + 114 - 47) / 31)
    local day = (h + L - 7 * m + 114 - 47) % 31 + 1
    if month == 2 then    --adjust dates in February
        day = leap_year and day - 2 or day - 3
    end
    return day, month
end

计算看起来很复杂,因为计算复活节的日期很复杂。这个函数遵循了Computus算法的算法。

测试:

print(shrove_tuesday(2012))
print(shrove_tuesday(2013))
print(shrove_tuesday(2014))
print(shrove_tuesday(2015))

输出:

21      2
12      2
4       3
17      2

你可以轻松使用 daymonth来获取格式化字符串,使用string.format("%02d/%02d/%04d", day, month, year)或其他你需要的方式。

2014-03-02 02:14:01