Module:lnk-date

From Laenkea
Revision as of 00:02, 1 September 2023 by TheNightAvl (talk | contribs)
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 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,
	}
	
	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] .. " nyentusk " .. 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] .. " 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
	-- end of Radestrian formatting
		
	-- standard DD/MM/YYYY setting
	else
		if parameters["word"] then
if laenk_date.month == 13 then
return laenk_date.day .. " " .. data["eng"]["months-lnk"][13] .. " " .. laenk_date.year
else
			return data["eng"]["days-lnk"][laenk_date.wday] .. ", " .. laenk_date.day .. " " .. data["eng"]["months-lnk"][laenk_date.month] .. " " .. laenk_date.year
end
		else
			return laenk_date.day .. "/" .. laenk_date.month .. "/" .. laenk_date.year .. " (" .. laenk_date.wday .. ")"
		end
	end
	
end

return export

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