Module:Asof

-- Creates dated statements local p={} local u = require("Dev:Utility") local dateHelper = require("Dev:Date") local tValidMonths = {january=1,february=2,march=3,april=4, may=5,june=6,july=7,august=8,september=9,october=10,november=11,december=12} function p.main(frame) local tArgs = u.getArgs(frame) local sYear = tonumber(tArgs[1]) local sMonth = string.lower(tArgs[2] or"") local sDay = tonumber(tArgs[3] ) local lc = tArgs["lc"] local prefix = "As of " local lang = mw.language.new("en") local sFormat = "Y" local x =dateHelper if lc then prefix = "as of " end local isYear,isMonth,isDay =p.isValidDate(sYear,sMonth,sDay) if not isYear then return error("Invalid date, year is wrong.") end if isMonth then sMonth = tonumber(sMonth) or tValidMonths[sMonth] sFormat = "F " .. sFormat if isDay then sFormat = "d " .. sFormat else sDay = os.date("%d") end else sDay = sDay or os.date("%d") sMonth = os.date("%m") end local sDefOutput = prefix .. lang:formatDate(sFormat,sYear.."/"..sMonth.."/"..sDay) local sOutput = tArgs["alt"] or sDefOutput return sOutput .. " " end

function p.isValidDate(year,month,day) local tValidDays = {31,28,31,30,31,30,31,30,31,30,31,30} local tValidMonths = {january=1,february=2,march=3,april=4, may=5,june=6,july=7,august=8,september=9,october=10,november=11,december=12} local bYear,bMonth,bDay month = tonumber(month) or string.lower(month) month = tonumber(month) or tValidMonths[month] mw.log(year) if tonumber(year) and tonumber(year)>-1 then if dateHelper.isleapyear(year) then tValidDays[2]=29 end bYear = true end if tValidMonths[month] or tValidDays[month] then bMonth =true if tonumber(month) and tonumber(day) and 0=tonumber(day) then bDay = true end end return bYear, bMonth, bDay end -- end date functions return p