Module:Calendar widget

This is an old revision of this page, as edited by RexxS (talk | contribs) at 16:10, 29 June 2019 (debug blank value). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
Module to create Calendar widget

--]]

local p = {}

local daysinmonth = {
	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}
local monthname = {
	"January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November", "December"
}
local dayname = {
	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
}

local function isleap(year)
	year = tonumber(year) or 1
	return year % 4 == 0
	and year % 100 ~= 0
	or year % 1000 == 0
end

--[[
Sakamoto's method: 1 <= month <= 12; year is Gregorian
dayofweek returns 0 to 6 or nil if bad arguments
--]]
local function dayofweek(year, month, day)
	local y = tonumber(year)
	local m = tonumber(month)
	local d = tonumber(day)
	if not (y and m and d) then return year .. month .. day end -- debug
	local t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
	if m < 3 then y = y - 1 end
	return (y + y/4 - y/100 + y/400 + t[m] + d) % 7
end

local function monthstart(year, month)
	return dayofweek(year, month, 1)
end

--[[
Sakamoto's method: ISO date
returns day name or nil if bad arguments
--]]
function p.dayofweek(frame)
	local isodate = mw.text.trim(frame.args[1] or "")
	local y, m, d = isodate:match("(%d+)%p(%d+)%p(%d+)")
	-- debug
	if not (y and m and d) then
		return (y or "no year") .. (m or "no month") .. (d or "no day")
	else
		--return y .. "-" .. m .. "-" .. d
	end -- debug
	local dow = dayofweek(y, m, d)
	if not dow then return y .. m .. d else return dow end
	return dayname[dow + 1]
end

--[[
isleap returns "leap" if passed a leap year
otherwise returns nothing
]]
function p.isleap(frame)
	if isleap(frame.args[1]) then return "leap" end
	return ""
end


return p