Files

305 lines
12 KiB
Lua

-- Norway: public holidays, flag days, other days people mark and name days, for any year, with no
-- network.
-- Every date is worked out from its rule, Easter's by calculation, so nothing is downloaded or
-- kept.
-- Dates are days since 1970-01-01, so adding days is adding numbers and no time zone gets in the
-- way. These are Howard Hinnant's days_from_civil and civil_from_days.
local function day_number(y, m, d)
if m <= 2 then y = y - 1 end
local era = math.floor(y / 400)
local yoe = y - era * 400
local doy = math.floor((153 * (m > 2 and m - 3 or m + 9) + 2) / 5) + d - 1
return era * 146097 + yoe * 365 + math.floor(yoe / 4) - math.floor(yoe / 100) + doy - 719468
end
local function civil(n)
local z = n + 719468
local era = math.floor(z / 146097)
local doe = z - era * 146097
local yoe = math.floor((doe - math.floor(doe / 1460) + math.floor(doe / 36524) - math.floor(doe / 146096)) / 365)
local doy = doe - (365 * yoe + math.floor(yoe / 4) - math.floor(yoe / 100))
local mp = math.floor((5 * doy + 2) / 153)
local d = doy - math.floor((153 * mp + 2) / 5) + 1
local m = mp < 10 and mp + 3 or mp - 9
return yoe + era * 400 + (m <= 2 and 1 or 0), m, d
end
-- weekday is 0 for Sunday: 1 January 1970 was a Thursday.
local function weekday(n) return (n + 4) % 7 end
-- easter is Easter Sunday in the Gregorian calendar, by the Meeus/Jones/Butcher algorithm.
local function easter(y)
local a, b, c = y % 19, math.floor(y / 100), y % 100
local d, e = math.floor(b / 4), 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, k = math.floor(c / 4), c % 4
local l = (32 + 2 * e + 2 * i - h - k) % 7
local m = math.floor((a + 11 * h + 22 * l) / 451)
local n = h + l - 7 * m + 114
return day_number(y, math.floor(n / 31), n % 31 + 1)
end
local function fixed(m, d) return function(y) return day_number(y, m, d) end end
local function from_easter(days) return function(y) return easter(y) + days end end
-- sunday is the nth Sunday of a month, or with a negative n, counted from its end: -1 is the last.
local function sunday(m, n)
return function(y)
if n < 0 then
local last = (m == 12 and day_number(y + 1, 1, 1) or day_number(y, m + 1, 1)) - 1
return last - weekday(last) + 7 * (n + 1)
end
local first = day_number(y, m, 1)
return first + (7 - weekday(first)) % 7 + 7 * (n - 1)
end
end
-- advent is the nth Sunday of Advent: the fourth is the last Sunday before Christmas Day.
local function advent(n)
return function(y)
local christmas = day_number(y, 12, 25)
local back = weekday(christmas)
if back == 0 then back = 7 end
return christmas - back - 7 * (4 - n)
end
end
local HOLIDAY, FLAG, NOTABLE = "holiday", "flag", "notable"
-- days are the public holidays (lov om helligdager, and 1 and 17 May), the official flag days
-- (flaggforskriften § 4), and other days people mark, as flaggdager.no lists them, with a few
-- more. The royal family is named as it is since Kong Harald V died on 28 August 2026 and Haakon
-- became king; flaggforskriften, last changed in 2005, hasn't caught up yet. `through` is the
-- last year a day is marked, `born` a royal's year, for the age a birthday is.
local days = {
{ "1. nyttårsdag", { HOLIDAY, FLAG }, fixed(1, 1) },
{ "Kronprinsesse Ingrid Alexandra", { FLAG }, fixed(1, 21), born = 2004 },
{ "Samefolkets dag", { FLAG }, fixed(2, 6) },
{ "Morsdag", { NOTABLE }, sunday(2, 2) },
{ "Valentinsdag", { NOTABLE }, fixed(2, 14) },
{ "Kong Harald V", { FLAG }, fixed(2, 21), born = 1937, through = 2026 },
{ "Fastelavnssøndag", { NOTABLE }, from_easter(-49) },
{ "Kvinnedagen", { NOTABLE }, fixed(3, 8) },
{ "Sommertid", { NOTABLE }, sunday(3, -1), note = "clocks-forward" },
{ "Palmesøndag", { NOTABLE }, from_easter(-7) },
{ "Skjærtorsdag", { HOLIDAY }, from_easter(-3) },
{ "Langfredag", { HOLIDAY }, from_easter(-2) },
{ "1. påskedag", { HOLIDAY, FLAG }, from_easter(0) },
{ "2. påskedag", { HOLIDAY }, from_easter(1) },
{ "Siste dag med piggdekk", { NOTABLE }, from_easter(8), note = "studded-tyres-end" },
{ "Båndtvang for hunder starter", { NOTABLE }, fixed(4, 1), note = "national-rule" },
{ "Bålforbud i skog og mark starter", { NOTABLE }, fixed(4, 15), note = "national-rule" },
{ "Arbeidernes dag", { HOLIDAY, FLAG }, fixed(5, 1) },
{ "Frigjøringsdagen", { FLAG }, fixed(5, 8) },
{ "Grunnlovsdagen", { HOLIDAY, FLAG }, fixed(5, 17) },
{ "Kristi himmelfartsdag", { HOLIDAY }, from_easter(39) },
{ "1. pinsedag", { HOLIDAY, FLAG }, from_easter(49) },
{ "2. pinsedag", { HOLIDAY }, from_easter(50) },
{ "Unionsoppløsningen", { FLAG }, fixed(6, 7) },
{ "Sankthansaften", { NOTABLE }, fixed(6, 23) },
{ "Dronning Sonja", { FLAG }, fixed(7, 4), born = 1937 },
{ "Kong Haakon", { FLAG }, fixed(7, 20), born = 1973 },
{ "Olsokdagen", { FLAG }, fixed(7, 29) },
{ "Dronning Mette-Marit", { FLAG }, fixed(8, 19), born = 1973 },
{ "Båndtvang for hunder opphører", { NOTABLE }, fixed(8, 21), note = "national-rule" },
{ "Bålforbud i skog og mark opphører", { NOTABLE }, fixed(9, 16), note = "national-rule" },
{ "FN-dagen", { NOTABLE }, fixed(10, 24) },
{ "Vintertid", { NOTABLE }, sunday(10, -1), note = "clocks-back" },
{ "Halloween", { NOTABLE }, fixed(10, 31) },
{ "Piggdekk tillatt", { NOTABLE }, fixed(11, 1), note = "southern-norway" },
{ "Allehelgensdag", { NOTABLE }, sunday(11, 1) },
{ "Farsdag", { NOTABLE }, sunday(11, 2) },
{ "1. søndag i advent", { NOTABLE }, advent(1) },
{ "2. søndag i advent", { NOTABLE }, advent(2) },
{ "3. søndag i advent", { NOTABLE }, advent(3) },
{ "Luciadagen", { NOTABLE }, fixed(12, 13) },
{ "4. søndag i advent", { NOTABLE }, advent(4) },
{ "Julaften", { NOTABLE }, fixed(12, 24) },
{ "1. juledag", { HOLIDAY, FLAG }, fixed(12, 25) },
{ "2. juledag", { HOLIDAY }, fixed(12, 26) },
{ "Nyttårsaften", { NOTABLE }, fixed(12, 31) },
}
local tags = {
{ kind = HOLIDAY, tint = "red", icon = "calendar" },
{ kind = FLAG, tint = "blue", icon = "flag" },
{ kind = NOTABLE, tint = "gray" },
}
local function is(day, kinds)
for _, k in ipairs(day[2]) do
if kinds[k] then return true end
end
return false
end
-- read_year takes a year out of what was typed; the other words look among the names.
local function read_year(rest)
local year, words = nil, {}
for w in rest:gmatch("%S+") do
local n = tonumber(w)
if not year and n and #w == 4 and n >= 1583 then year = n else table.insert(words, w) end
end
return year, table.concat(words, " ")
end
local function when(today, n)
local diff = n - today
if diff == 0 then return toolshed.t("today") end
if diff == 1 then return toolshed.t("tomorrow") end
if diff == -1 then return toolshed.t("yesterday") end
if diff < 0 then return toolshed.t("days-ago", { count = -diff }) end
return toolshed.t("in-days", { count = diff })
end
local function date(n)
local y, m, d = civil(n)
return toolshed.t("date", { weekday = toolshed.t("weekday." .. weekday(n)), day = d, month = toolshed.t("month." .. m), year = y })
end
local function iso(n)
return string.format("%04d-%02d-%02d", civil(n))
end
local function jig(day, n, today)
local y = civil(n)
local about = { date(n), when(today, n) }
if day.born then table.insert(about, toolshed.t("turns", { age = y - day.born })) end
if day.note then table.insert(about, toolshed.t("note." .. day.note)) end
local on = {}
for _, t in ipairs(tags) do
if is(day, { [t.kind] = true }) then
table.insert(on, { text = toolshed.t("tag." .. t.kind), tint = t.tint, icon = t.icon })
end
end
return {
key = day[1], title = day[1], subtitle = table.concat(about, ", "), tags = on,
data = { date = iso(n) },
}
end
-- upcoming lists the days of some kinds in a year, or without one, in the year from today, so
-- the next is first.
local function upcoming(rest, kinds)
local now = os.date("*t")
local today = day_number(now.year, now.month, now.day)
local year, filter = read_year(rest)
local from, to = today, day_number(now.year + 1, now.month, now.day)
if year then from, to = day_number(year, 1, 1), day_number(year + 1, 1, 1) end
local found = {}
for y = civil(from), civil(to) do
for i, day in ipairs(days) do
local n = day[3](y)
if is(day, kinds) and (not day.through or y <= day.through) and n >= from and n < to
and toolshed.match(filter, day[1]) > 0 then
table.insert(found, { day = day, n = n, order = i })
end
end
end
table.sort(found, function(a, b)
if a.n ~= b.n then return a.n < b.n end
return a.order < b.order
end)
local jigs = {}
for _, f in ipairs(found) do table.insert(jigs, jig(f.day, f.n, today)) end
return jigs
end
local name_days = require("name_days")
local function today()
local now = os.date("*t")
return day_number(now.year, now.month, now.day)
end
-- next_name_day is the day a month * 100 + day comes next, today or later.
local function next_name_day(md, from)
local y, m, d = civil(from), math.floor(md / 100), md % 100
local n = day_number(y, m, d)
return n >= from and n or day_number(y + 1, m, d)
end
-- named lists the name days of the next two weeks, or with a name typed, the names like it, the
-- closest first, each with the day it's next.
local function named(rest)
local from, query = today(), rest:match("^%s*(.-)%s*$")
local jigs = {}
if query == "" then
for n = from, from + 13 do
local _, m, d = civil(n)
local names = name_days[m * 100 + d]
if names then
local title = table.concat(names, ", ")
table.insert(jigs, { key = title, title = title, subtitle = date(n) .. ", " .. when(from, n), data = { date = iso(n) } })
end
end
return jigs
end
local found = {}
for md, names in pairs(name_days) do
for _, name in ipairs(names) do
local score = toolshed.match(query, name)
if score > 0 then table.insert(found, { name = name, n = next_name_day(md, from), score = score }) end
end
end
table.sort(found, function(a, b)
if a.score ~= b.score then return a.score > b.score end
return a.n < b.n
end)
for _, f in ipairs(found) do
table.insert(jigs, { key = f.name, title = f.name, subtitle = date(f.n) .. ", " .. when(from, f.n), data = { date = iso(f.n) } })
end
return jigs
end
local copy_date = { key = "copy", title = "i18n:action.copy", icon = "copy", run = function(ctx)
toolshed.clipboard.copy(ctx.data.date)
return toolshed.t("copied", { date = ctx.data.date })
end }
local function days_tool(key, icon, kinds, words, keywords)
return {
key = key,
title = "i18n:tool." .. key .. ".title",
description = "i18n:tool." .. key .. ".description",
icon = icon,
words = words,
keywords = keywords,
examples = { words[1], words[1] .. " 2027" },
items = true,
search = function(rest) return upcoming(rest, kinds) end,
actions = { copy_date },
}
end
return toolshed.toolbox {
key = "norway",
title = "i18n:toolbox.title",
description = "i18n:toolbox.description",
icon = "assets/flag.svg",
tint = "red",
tools = {
days_tool("holidays", "calendar", { [HOLIDAY] = true }, { "helligdager", "holidays" },
{ "røde dager", "public holidays", "påske", "easter", "pinse" }),
days_tool("flag-days", "flag", { [FLAG] = true }, { "flaggdager", "flagdays" },
{ "flag days", "flagging", "flagg" }),
days_tool("notable-days", "star", { [HOLIDAY] = true, [FLAG] = true, [NOTABLE] = true }, { "merkedager" },
{ "notable days", "morsdag", "farsdag", "advent", "sommertid", "vintertid", "båndtvang", "bålforbud", "piggdekk" }),
{
key = "name-days",
title = "i18n:tool.name-days.title",
description = "i18n:tool.name-days.description",
icon = "users",
words = { "navnedag", "nameday" },
keywords = { "navnedager", "name day", "name days" },
examples = { "navnedag", "navnedag karl" },
items = true,
search = named,
actions = { copy_date },
},
},
}