Module:lnk-date

See {{date}}. All language data can be found at Module:lnk-date/data.


local export = {}
local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData("Module:lnk-date/data")
local m_links = require("Module:links")
local m_languages = require("Module:languages")

local date_today = os.date("*t")
local year_offset = 0 -- <- a positive offset will add to the current year

function get_laenk_date(d_value, m_value, y_value)
	local greg_date = {
		["day"] = d_value,
		["month"] = m_value,
		["year"] = y_value,
	}
	
	local laenk_date = { 
		["day"] = 0,
		["month"] = 0,
		["year"] = greg_date.year + year_offset,
		["yday"] = 0,
		["week"] = 0,
		["mweek"] = 0,
		["wday"] = 0,
		["is_leapyear"] = false,
	}
	
	local last_laenk_ny = {
		["day"] = 20,
		["month"] = 3,
		["year"] = greg_date.year - 1,
	}
	
	if os.date("*t", os.time(greg_date)).yday > 79 or (os.date("*t", os.time(greg_date)).yday == 79 and os.date("*t", os.time({["day"] = 31, ["month"] = 12, ["year"] = greg_date.year})).yday == 365) then
		laenk_date.year = laenk_date.year + 1
		last_laenk_ny.year = last_laenk_ny.year + 1
	end
	
	if os.date("*t", os.time({["day"] = 31, ["month"] = 12, ["year"] = laenk_date.year})).yday == 366 then
		laenk_date.is_leapyear = true
	end
	
	laenk_date.yday = math.floor((os.difftime(os.time(greg_date), os.time(last_laenk_ny)) / (24 * 60 * 60)) + 0.5) -- sometimes it returns 5.98... instead of 6, hence +0.5
	laenk_date.month = math.floor(laenk_date.yday / 30) + 1
	laenk_date.day = (laenk_date.yday % 30) + 1
	laenk_date.week = math.floor(laenk_date.yday / 6) + 1
	laenk_date.mweek = math.floor(laenk_date.yday / 6) + 1 - (laenk_date.month-1) * 5
	laenk_date.wday = (laenk_date.yday % 6) + 1
	
	return laenk_date
end

function export.show(frame)
	local args = getArgs(frame)
	local greg_year = date_today.year
	local greg_month = date_today.month
	local greg_day = date_today.day
	
	local parameters = {}
	local p = 1
	
	local greg_wday = os.date("*t", os.time({["day"] = greg_day, ["month"] = greg_month, ["year"] = greg_year})).wday
	
	if args[1] and args[2] and args[3] then
		if mw.ustring.find(args[1], "([0-9]+)") and mw.ustring.find(args[2], "([0-9]+)") and mw.ustring.find(args[3], "([0-9]+)") then
			greg_year = tonumber(args[3])
			greg_month = tonumber(args[2])
			greg_day = tonumber(args[1])
			p = 4
		end
	end
	
	-- process all parameters --
	while args[p] do
		parameters[args[p]] = true
		p = p + 1
	end
	
	local laenk_date = get_laenk_date(greg_day, greg_month, greg_year)
	
	if parameters["greg"] then
		if parameters["word"] then
			if parameters["rad"] then
				return data["rad"]["greg"]["days"][greg_wday] .. ", " .. data["rad"]["date"][greg_day] .. " " .. data["rad"]["greg"]["months"][greg_month] .. " nventusk " .. greg_year .. "șk"
			else
				return data["eng"]["days"][greg_wday] .. ", " .. greg_day .. " " .. data["eng"]["months"][greg_month] .. " " .. greg_year
			end
		else
			return greg_day .. "/" .. greg_month .. "/" .. greg_year .. " (" .. greg_wday .. ")"
		end
	
	--[[ The possible variables you can call are as follows:
			laenk_date.year
			laenk_date.month
			laenk_date.day
			laenk_date.week (calls the week number within the whole year)
			laenk_date.mweek (calls the week number within the month as values 1–5)
			laenk_date.wday (calls the day of the week as values 1–6)
			
			These will always return a number. You can use this number to index your language data using data["lfv"]["months"][laenk_date.month], for example, or just use it on its own when formatting dates with numbers.
			
	★ EXAMPLE FORMATTING ★
	
			elseif parameters["lfv"] then
				if parameters["word"] then -- word format
					if laenk_date.month == 13 then -- 13th month date format
						
					else -- if months 1-12
							
					end
				else -- number format
					-- PUT YOUR CODE TO FORMAT THE DATE HERE
				end
				
		You can use any word as a parameter. Just set up an if parameters[" ... "] then … with a word or phrase you want to use as a calling argument and it will be recognised. For example, Rad has a "mod" parameter, which is only mentioned in the if statements below, and then I can call it in the {{date}} template.
	]]
	
	-- Radestrian formatting
	elseif parameters["rad"] then
		if parameters["word"] then -- word format
			if laenk_date.month == 13 then -- 13th month date format doesn't differ between modern and traditional
				if laenk_date.is_leapyear == false and laenk_date.day == 5 then
					laenk_date.day = 6
				end
				return  data["rad"]["days-13"][laenk_date.day] .. " " .. data["rad"]["months"][13] .. " nventusk " .. laenk_date.year .. "șk"
			else -- if months 1-12
				if parameters["mod"] then -- modern format
					return  data["rad"]["days"][laenk_date.wday] .. ", " .. data["rad"]["date"][laenk_date.day] .. " " .. data["rad"]["months"][laenk_date.month] .. " nventusk " .. laenk_date.year .. "șk"
				else -- traditional format
					local temp_first = ""
					if laenk_date.mweek == 1 then temp_first = " helișe" end
					return mw.ustring.gsub(data["rad"]["week"][laenk_date.mweek] .. " " .. data["rad"]["days"][laenk_date.wday], "(a a)", "ag a") .. " " .. data["rad"]["months"][laenk_date.month] .. temp_first .. " nventusk " .. laenk_date.year .. "șk"
				end
			end
		else -- date format
			if parameters["mod"] then -- modern format
				return laenk_date.day .. "/" .. laenk_date.month .. "/" .. laenk_date.year
			else -- traditional format
				if laenk_date.month == 13 then
					return laenk_date.wday .. "/" .. laenk_date.month .. "/" .. laenk_date.year
				else
					return laenk_date.mweek .. "·" .. laenk_date.wday .. "/" .. laenk_date.month .. "/" .. laenk_date.year
				end
			end
		end
	-- end of Radestrian formatting
	
	-- Riyan formatting
	elseif parameters["ryn"] then
		if parameters["word"] then -- word format
			if laenk_date.month == 13 then -- 13th month date format doesn't differ between modern and traditional
				if laenk_date.is_leapyear == false and laenk_date.day == 5 then
					laenk_date.day = 6
				end
				return  data["ryn"]["days-13"][laenk_date.day] .. " " .. data["ryn"]["months"][13] .. " nynt " .. laenk_date.year
			else -- if months 1-12
					return data["ryn"]["week"][laenk_date.mweek] .. " " .. data["ryn"]["days"][laenk_date.wday] .. " " .. data["ryn"]["months"][laenk_date.month] .. " nynt " .. laenk_date.year
			end
		else -- date format
			if laenk_date.month == 13 then
				return laenk_date.wday .. "/" .. laenk_date.month .. "/" .. laenk_date.year
			else
				return laenk_date.wday .. "•" .. laenk_date.mweek .. "/" .. laenk_date.month .. "/" .. laenk_date.year
			end
		end
	-- end of Riyan formatting
	
		-- Laefevian formatting
	elseif parameters["lfv"] then
		if parameters["word"] then -- word format
			if laenk_date.month == 13 then -- 13th month date format doesn't differ between modern and traditional
				if laenk_date.is_leapyear == false and laenk_date.day == 5 then
					laenk_date.day = 6
				end
				return  data["lfv"]["days-13"][laenk_date.day] .. " " .. data["lfv"]["months"][13] .. " " .. laenk_date.year
			else -- if months 1-12
					return data["lfv"]["week"][laenk_date.mweek] .. " " .. data["lfv"]["days"][laenk_date.wday] .. " " .. data["lfv"]["months"][laenk_date.month] .. " " .. laenk_date.year
			end
		else -- date format
			if laenk_date.month == 13 then
				local offset = 6
				if laenk_date.is_leapyear then offset = 7 end
				return (offset - laenk_date.wday) .. "’ " .. laenk_date.month .. "” " .. laenk_date.year
			else
				return laenk_date.mweek .. "·" .. laenk_date.wday .. "’ " .. laenk_date.month .. "” " .. laenk_date.year
			end
		end
	-- end of Laefevian formatting
		
	-- standard DD/MM/YYYY setting
	else
		if parameters["word"] then
			if laenk_date.month == 13 then
				return data["eng"]["days-lnk-13"][laenk_date.day] .. " " .. data["eng"]["months-lnk"][13] .. " " .. laenk_date.year
			else
				return data["eng"]["week"][laenk_date.mweek] .. " " .. data["eng"]["days-lnk"][laenk_date.wday] .. " of " .. data["eng"]["months-lnk"][laenk_date.month] .. " " .. laenk_date.year
			end
		else
			local numdate = laenk_date.day .. "/" .. laenk_date.month .. "/" .. laenk_date.year
			if laenk_date.month ~= 13 then numdate = numdate .. " (" .. laenk_date.wday .. ")" end
			return numdate
		end
	end
	
end


-- CALENDAR


function export.calendar()
	local time_data = get_laenk_date(date_today.day, date_today.month, date_today.year)
	--local time_data = get_laenk_date(15, 3, 2023)
	local highlight =  "bgcolor=\"#f5ffcc\" style=\"outline: solid 1px\""
	mw.logObject(time_data)
	
	local return_table = {}
	
	local function add(term) table.insert(return_table, term) end
	
	local function link(lang, word)
		return m_links.full_link{
		    term = word,
		    language = m_languages.get_by_code(lang)
		}
	end
	
	local function highlighter(condition, pre, post)
		if condition then
			add(pre .. " " .. highlight .. post)
		else
			add(pre .. post)
		end
	end
	
	local function grey(pre, post)
		add(pre .. " style=\"color:gray\"" .. post)
	end
	
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding-left: 10px; padding-top: 1px; padding-bottom: 5px; padding-right: 10px; border-spacing: 7px; width: 40%; text-align: center\">")
	add("<th colspan=2><small>LAENKEAN YEAR " .. time_data.year .. "</small></th>")
	add("<tr><th colspan=2>")
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding: 0px; border-spacing: 10px; width: 100%; text-align: center;\"><tr>")
	
	highlighter(time_data.month == 1, "<th width=16.6%", "><small>MARCH<br>" .. link("lfv", "kwilliehda") .. "<br>" .. link("rad", "vanints") .. "<br>" .. link("ryn", "vaenynt") .. "</small></th>")
	highlighter(time_data.month == 2, "<th width=16.6%", "><small>APRIL<br>" .. link("lfv", "ǧiernilda") .. "<br>" .. link("rad", "hrelners") .. "<br>" .. link("ryn", "ŗidrilt") .. "</small></th>")
	highlighter(time_data.month == 3, "<th width=16.6%", "><small>MAY<br>" .. link("lfv", "mialtehda") .. "<br>" .. link("rad", "gjembraus") .. "<br>" .. link("ryn", "vaemille") .. "</small></th>")
	highlighter(time_data.month == 4, "<th width=16.6%", "><small>JUNE<br>" .. link("lfv", "rimpêñilda") .. "<br>" .. link("rad", "dvuoþers") .. "<br>" .. link("ryn", "ŗlent") .. "</small></th>")
	highlighter(time_data.month == 5, "<th width=16.6%", "><small>JULY<br>" .. link("lfv", "hestiehda") .. "<br>" .. link("rad", "hraers") .. "<br>" .. link("ryn", "zionta") .. "</small></th>")
	highlighter(time_data.month == 6, "<th width=16.6%", "><small>AUGUST<br>" .. link("lfv", "buhda") .. "<br>" .. link("rad", "sfvocía") .. "<br>" .. link("ryn", "potta") .. "</small></th>")
	
	add("</tr><tr>")
	
	highlighter(time_data.month == 7, "<th width=16.6%", "><small>SEPTEMBER<br>" .. link("lfv", "šambuhda") .. "<br>" .. link("rad", "alua") .. "<br>" .. link("ryn", "tarmúst") .. "</small></th>")
	highlighter(time_data.month == 8, "<th width=16.6%", "><small>OCTOBER<br>" .. link("lfv", "þahtaida") .. "<br>" .. link("rad", "irgotua") .. "<br>" .. link("ryn", "ŗairt") .. "</small></th>")
	highlighter(time_data.month == 9, "<th width=16.6%", "><small>NOVEMBER<br>" .. link("lfv", "fihda") .. "<br>" .. link("rad", "cynuers") .. "<br>" .. link("ryn", "tynir") .. "</small></th>")
	highlighter(time_data.month == 10, "<th width=16.6%", "><small>DECEMBER<br>" .. link("lfv", "sumpêñilda") .. "<br>" .. link("rad", "háșers") .. "<br>" .. link("ryn", "ŗibioŗ") .. "</small></th>")
	highlighter(time_data.month == 11, "<th width=16.6%", "><small>JANUARY<br>" .. link("lfv", "malluida") .. "<br>" .. link("rad", "mvaþers") .. "<br>" .. link("ryn", "alant") .. "</small></th>")
	highlighter(time_data.month == 12, "<th width=16.6%", "><small>FEBRUARY<br>" .. link("lfv", "ǧiôhda") .. "<br>" .. link("rad", "dzevans") .. "<br>" .. link("ryn", "mentaș") .. "</small></th>")
	
	add("</tr></table></th></tr><tr><td>")
	
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding: 0px; border-spacing: 10px; width: 100%; text-align: center;\"><tr>")
	
	highlighter(time_data.month ~= 13 and time_data.wday == 1, "<th width=16.6%", "><small>PRINDINE<br>" .. link("lfv", "aspai") .. "<br>" .. link("rad", "varyga") .. "<br>" .. link("ryn", "hirta") .. "</small></th>")
	highlighter(time_data.month ~= 13 and time_data.wday == 2, "<th width=16.6%", "><small>SECUNDINE<br>" .. link("lfv", "hrwôpai") .. "<br>" .. link("rad", "atía") .. "<br>" .. link("ryn", "lista") .. "</small></th>")
	highlighter(time_data.month ~= 13 and time_data.wday == 3, "<th width=16.6%", "><small>TERCIDINE<br>" .. link("lfv", "lippai") .. "<br>" .. link("rad", "liría") .. "<br>" .. link("ryn", "muŗta") .. "</small></th>")
	highlighter(time_data.month ~= 13 and time_data.wday == 4, "<th width=16.6%", "><small>QUARTINE<br>" .. link("lfv", "majpai") .. "<br>" .. link("rad", "asedua") .. "<br>" .. link("ryn", "zulta") .. "</small></th>")
	highlighter(time_data.month ~= 13 and time_data.wday == 5, "<th width=16.6%", "><small>QUINTINE<br>" .. link("lfv", "skipôda") .. "<br>" .. link("rad", "laía") .. "<br>" .. link("ryn", "rjyŗta") .. "</small></th>")
	highlighter(time_data.month ~= 13 and time_data.wday == 6, "<th width=16.6%", "><small>SEXTINE<br>" .. link("lfv", "þuôkôda") .. "<br>" .. link("rad", "valygdras") .. "<br>" .. link("ryn", "slyŗta") .. "</small></th>")
	
	add("</tr>")
	
	for i = 1, 30 do
		if i % 6 == 1 then
			add("<tr>")
		end
		highlighter(time_data.month ~= 13 and time_data.day == i, "<td", ">" .. i .. "</td>")
		if i % 6 == 0 then
			add("</tr>")
		end
	end
	
	add("</table></td><td>")
	
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding: 0px; border-spacing: 10px; width: 90%; text-align: center\">")
	add("<th><small>WEEK<br>" .. link("lfv", "najost") .. "<br>" .. link("rad", "areans") .. "<br>" .. link("ryn", "nannaor") .. "</small></th>")
	
	for i = 1, 5 do
		highlighter(time_data.month ~= 13 and time_data.mweek == i, "<tr><td", ">" .. i .. "</td></tr>")
	end
	
	add("</table></td></tr><tr><td colspan=2>")
	
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding: 0px; border-spacing: 10px; width: 100%; text-align: center\">")
	highlighter(time_data.month == 13, "<tr><th", "><center><small>FERIAE<br>" .. link("lfv", "hlwêhda") .. "<br>" .. link("rad", "dzestasans") .. "<br>" .. link("ryn", "hașpar") .. "</small></center></th></tr>")
	
	add("</table></td></tr><tr><td colspan=2>")
	
	add("<table class=\"floatright\" style=\"border:1px solid #AAA; padding: 0px; border-spacing: 10px; width: 100%; text-align: center\">")
	add("<tr><th></th>")
	
	if time_data.is_leapyear then
		highlighter(time_data.month == 13 and time_data.day == 1, "<th width=16.6%", "><small>SEXTA<br>" .. link("lfv", "nael") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 2, "<th width=16.6%", "><small>QUINTA<br>" .. link("lfv", "luol") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 3, "<th width=16.6%", "><small>QUARTA<br>" .. link("lfv", "maol") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 4, "<th width=16.6%", "><small>TERTIA<br>" .. link("lfv", "lêil") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<th width=16.6%", "><small>SECUNDA<br>" .. link("lfv", "adil") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 6, "<th width=16.6%", "><small>PRIMA<br>" .. link("lfv", "assil") .. "</small></th>")
	else
		grey("<th width=16.6%", "><small>SEXTA<br>(" .. link("lfv", "nael") .. ")</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 1, "<th width=16.6%", "><small>QUINTA<br>" .. link("lfv", "luol") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 2, "<th width=16.6%", "><small>QUARTA<br>" .. link("lfv", "maol") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 3, "<th width=16.6%", "><small>TERTIA<br>" .. link("lfv", "lêil") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 4, "<th width=16.6%", "><small>SECUNDA<br>" .. link("lfv", "adil") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<th width=16.6%", "><small>PRIMA<br>" .. link("lfv", "assil") .. "</small></th>")
	end
	
	add("</tr><tr>")
	
	add("<th>[[File:LaefevianFlag.png|36px]]</th>")
	
	if time_data.is_leapyear then
		highlighter(time_data.month == 13 and time_data.day == 1, "<td", ">6</td>")
		highlighter(time_data.month == 13 and time_data.day == 2, "<td", ">5</td>")
		highlighter(time_data.month == 13 and time_data.day == 3, "<td", ">4</td>")
		highlighter(time_data.month == 13 and time_data.day == 4, "<td", ">3</td>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<td", ">2</td>")
		highlighter(time_data.month == 13 and time_data.day == 6, "<td", ">1</td>")
	else
		grey("<td", ">—</td>")
		highlighter(time_data.month == 13 and time_data.day == 1, "<td", ">5</td>")
		highlighter(time_data.month == 13 and time_data.day == 2, "<td", ">4</td>")
		highlighter(time_data.month == 13 and time_data.day == 3, "<td", ">3</td>")
		highlighter(time_data.month == 13 and time_data.day == 4, "<td", ">2</td>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<td", ">1</td>")
	end
	
	add("</tr>")
	
	-- Radestrian 13th month
	
	add("<tr><th></th>")
	
	highlighter(time_data.month == 13 and time_data.day == 1, "<th", "><small>PRIMA<br>" .. link("rad", "asía") .. "<br>" .. link("ryn", "arta") .. "</small></th>")
	highlighter(time_data.month == 13 and time_data.day == 2, "<th", "><small>SECUNDA<br>" .. link("rad", "atía") .. "<br>" .. link("ryn", "rúfta") .. "</small></th>")
	highlighter(time_data.month == 13 and time_data.day == 3, "<th", "><small>TERTIA<br>" .. link("rad", "liría") .. "<br>" .. link("ryn", "leirta") .. "</small></th>")
	highlighter(time_data.month == 13 and time_data.day == 4, "<th", "><small>QUARTA<br>" .. link("rad", "aía") .. "<br>" .. link("ryn", "maida") .. "</small></th>")
	
	if time_data.is_leapyear then
		highlighter(time_data.month == 13 and time_data.day == 5, "<th", "><small>QUINTA<br>" .. link("rad", "vaía") .. "<br>" .. link("ryn", "kyttúl") .. "</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 6, "<th", "><small>ULTIMA<br>" .. link("rad", "valygdras") .. "<br>" .. link("ryn", "ŗúraș") .. "</small></th>")
	else
		grey("<th", "><small>QUINTA<br>(" .. link("rad", "vaía") .. ")<br>(" .. link("ryn", "kyttúl") .. ")</small></th>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<th", "><small>ULTIMA<br>" .. link("rad", "valygdras") .. "<br>" .. link("ryn", "ŗúraș") .. "</small></th>")
	end
	
	add("</tr><tr>")
	
	add("<th>[[File:RadestrianFlag.png|36px]]</th>")
	
	highlighter(time_data.month == 13 and time_data.day == 1, "<td", ">1</td>")
	highlighter(time_data.month == 13 and time_data.day == 2, "<td", ">2</td>")
	highlighter(time_data.month == 13 and time_data.day == 3, "<td", ">3</td>")
	highlighter(time_data.month == 13 and time_data.day == 4, "<td", ">4</td>")
	
	if time_data.is_leapyear then
		
		highlighter(time_data.month == 13 and time_data.day == 5, "<td", ">5</td>")
		highlighter(time_data.month == 13 and time_data.day == 6, "<td", ">6</td>")
	else
		grey("<td", ">—</td>")
		highlighter(time_data.month == 13 and time_data.day == 5, "<td", ">5</td>")
	end
	
	add("</tr></table></td></tr></table>")
	
	return table.concat(return_table, mw.ustring.char(10))
end

return export

--[[
Debug console test string:
=p.show(mw.getCurrentFrame():newChild{title="whatever",args={}})
]]