Module:lnk-date

Revision as of 18:16, 27 August 2023 by TheNightAvl (talk | contribs)

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 date_today = os.date("*t")
local year_offset = 0

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 > 78 then -- if after March 19
		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)) + 1
	
	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
	
	if type(args[1]) == "number" and type(args[2]) == "number" and type(args[3]) == "number" then
		greg_year = args[3]
		greg_month = args[2]
		greg_day = args[1]
		p = 4
	end
	
	local greg_wday = os.date("*t", os.time({["day"] = greg_day, ["month"] = greg_month, ["year"] = greg_year})).wday
	
	-- 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
			return data["eng"]["days"][greg_wday] .. ", " .. greg_day .. " " .. data["eng"]["months"][greg_month] .. " " .. greg_year
		else
			return greg_year .. "-" .. greg_month .. "-" .. greg_day .. " (" .. greg_wday .. ")"
		end
	
	-- Radestrian formatting
	elseif parameters["rad"] then
		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
		
	-- standard YYYY/MM/DD setting
	else
		return laenk_date.year .. "-" .. laenk_date.month .. "-" .. laenk_date.day .. " (" .. laenk_date.wday .. ")"
	end
	
end

return export

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