Module:lnk-date

Revision as of 21:48, 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 -- <- 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,
		["hour"] = 12,
		["minute"] = 0,
	}
	
	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,
		["hour"] = 12,
		["minute"] = 0,
	}
	
	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)
	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] .. " nyentusk " .. greg_year .. "șk"
			else
				return data["eng"]["days"][greg_wday] .. ", " .. greg_day .. " " .. data["eng"]["months"][greg_month] .. " " .. greg_year
			end
		else
			return greg_year .. "-" .. greg_month .. "-" .. greg_day .. " (" .. greg_wday .. ")"
		end
	
	-- 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] .. " nyentusk " .. 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] .. " nyentusk " .. laenk_date.year .. "șk"
				else -- traditional format
					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] .. " nyentusk " .. 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
		
	-- 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={}})
]]