Module:Sandbox/RexxS/Dates

This is an old revision of this page, as edited by RexxS (talk | contribs) at 16:52, 11 November 2018 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[
test functions related to dates
-]]

local p = {}


local function leapd(y)
	if y % 1000 == 0 then return 29 end
	if y % 100 == 0 then return 28 end
	if y% 4 == 0 then return 29 end
	return 28
end

local months = { "jan", "feb", "mar", "apr", "may", "jun",
	"jul", "aug", "sep", "oct", "nov", "dec" }
local days_in_month = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
local month_idx = {}
for i, v in ipairs(months) do
	month_idx[v] = i
end

local function day_try( d, m, y)
	days_in_month[2] = leapd(y)
	if d <= days_in_month[m] then return "Valid" else return "Invalid" end
end

function p.dayTry(frame)
	local date = frame.args.date or mw.text.trim(frame.args[1] or "")
	local d, y = date:match("(%d+)%D+(%d+)")
	local m = date:match("%a+") or ""
	local mnth = m:sub(1,3):lower()
	-- return day_try(d, mnth, y)
	return d .. "<br>" .. m .. "<br>" .. y .. "<br>" .. mnth .. "<br>"
end


return p