Module:lnk-date

From Laenkea
Revision as of 18:07, 27 August 2023 by TheNightAvl (talk | contribs) (Created page with "local export = {} local getArgs = require('Module:Arguments').getArgs 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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


local export = {}
local getArgs = require('Module:Arguments').getArgs

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
	
	-- 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
		return greg_day .. "/" .. greg_month .. "/" .. greg_year
	
	-- 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={}})
]]