42 lines
1.1 KiB
Text
42 lines
1.1 KiB
Text
local fmt = require "fmt"
|
|
|
|
local anchor_day = |y| -> (2 + 5 * (y % 4) + 4 *(y % 100) + 6 * (y % 400)) % 7
|
|
|
|
local is_leap_year = |y| -> y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
|
|
|
|
local first_days_common = {3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5}
|
|
local first_days_leap = {4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5}
|
|
|
|
local dates = {
|
|
"1800-01-06",
|
|
"1875-03-29",
|
|
"1915-12-07",
|
|
"1970-12-23",
|
|
"2043-05-14",
|
|
"2077-02-12",
|
|
"2101-04-02"
|
|
}
|
|
|
|
local function parse(date)
|
|
local y = tonumber(date:sub(1, 4))
|
|
local m = tonumber(date:sub(6, 7))
|
|
local d = tonumber(date:sub(9, 10))
|
|
return y, m, d
|
|
end
|
|
|
|
print("Days of week given by Doomsday rule:")
|
|
for dates as date do
|
|
local y, m, d = parse(date)
|
|
local a = anchor_day(y)
|
|
local w = d - (is_leap_year(y) ? first_days_leap[m] : first_days_common[m])
|
|
if w < 0 then w += 7 end
|
|
local dow = (a + w) % 7
|
|
print($"{date} -> {fmt.weekday(dow)}")
|
|
end
|
|
|
|
print("\nDays of week given by standard library functions:")
|
|
for dates as date do
|
|
local y, m, d = parse(date)
|
|
local t = os.time({year = y, month = m, day = d})
|
|
print($"{date} -> {os.date("%A", t)}")
|
|
end
|