如何获取忏悔星期二的日期(12/02/2013
、04/03/2014
、17/02/2015
> 等)在 Lua 中从提供的年份?如果可能的话,能否解释清楚,以便适应复活节、母亲节和其他每年变化的节日?网上有一些脚本可以支持复活节,但它们的解释不是很清楚,我不明白如何更改它们以适应忏悔星期二和其他假期。
最佳答案
根据Wikipedia ,忏悔星期二正好是复活节星期日之前的 47 天。所以关键在于如何计算复活节这个可移动的盛宴。您可以修改code to calculate Easter获得忏悔星期二。
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
您可以轻松地使用day
和month
来使用string.format("%02d/%02d/%04d", day,月、年)
或您需要的任何内容。
关于date - 在 Lua 中获得忏悔星期二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122881/