Module:Calendar

--[=[

Lua Module for Calendar Functions

--]=]

local p = {} local util = require("Module:Utility")

local function day_of_week(dd, mm, yy) dw=os.date('*t',os.time{year=yy,month=mm,day=dd})['wday'] return ({"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" })[dw] .. " " .. util.ordinal(dd) end

local function days_in_month(month, year) local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} local d = days_in_month[month] if (month == 2) then if year % 4 == 0 then if year % 100 == 0 then if year % 400 == 0 then d = 29 end else d = 29 end end end return d end

function p.main(frame) local tArgs = util.getArgs(frame) local year = tArgs[1] or 2016 local tOut = {'{| class="article-table dmk-calendar" style="width: 100%;"\n'} local tOut1 = {'|-\n! style="width: 12%"|Week 1 January\n'} local tOut2 = {"|-\n! Reward \n"}

local dw = os.date('*t',os.time{year=year,month=1,day=1})['wday'] dw = dw - 1 for n=1, dw do table.insert(tOut1, '! style="width: 12%"|\n') table.insert(tOut2, "|\n") end local d = dw - 1

local months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} local i = 2 local a = 1

for k, v in ipairs(months) do		local monthdays = days_in_month(k, year) for n=1, monthdays do			d = d + 1 if d == 7 then table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) tOut1 = {'|-\n! style="width: 12%"|Week ' .. i .. " " .. v .. "\n"} tOut2 = {"|-\n! Reward \n"} d = 0 i = i + 1 end table.insert(tOut1, '! style="width: 12%"| ' .. day_of_week(n, k, year) .. " \n") table.insert(tOut2, "|" .. (tArgs[a + 1] or " Unknown Reward ") .. "\n") a = a + 1 end end

if d < 7 then d = 7 - d - 1 for n=1, d do table.insert(tOut1, '! style="width: 12%"|\n') table.insert(tOut2, "|\n") end table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) end

return table.concat(tOut) .. "|}" end

return p