-- smsingh Task 7, Date Formattinglocal p = p.formatdate = function(frame) local unformatteddate = frame.args.unformatteddate local outputformat = frame.args.outputformat-- extracting day, month, and year function intable(tabl, value) for i = 1, #tabl do if tabl[i]
nil) then message = "circa " unformatteddate = string.gsub(unformatteddate, "uncertain", "", 1) elseif not (string.match(unformatteddate, "sometime around")
nil then if outputformat
"" then outputformat = "iso" end if tonumber(string.match(unformatteddate, "(%d+)")) < 32 then day = string.match(unformatteddate, "(%d+)") unformatteddate = string.gsub(unformatteddate, day, "", 1) elseif tonumber(string.match(unformatteddate, "(%d+)")) > 31 then year = string.match(unformatteddate, "(%d+)") unformatteddate = string.gsub(unformatteddate, year, "", 1) else day = "" end if (tonumber(string.match(unformatteddate, "(%d+)")) or 14) < 13 then numericalmonth = string.match(unformatteddate, "(%d+)") or "" month = fullmonths[tonumber(numericalmonth)] or "" unformatteddate = string.gsub(unformatteddate, numericalmonth, "", 1) else month = "" end if year
nil) and not (tonumber(string.match(unformatteddate, "(%d+)"))
"" or month
"" then if day
"" or day
nil then return "Invalid entry" elseif tonumber(string.match(unformatteddate, "(%d+)")) < 32 then day = string.match(unformatteddate, "(%d+)") unformatteddate = string.gsub(unformatteddate, day, "", 1) else day = "" end potentialmonth = string.match(unformatteddate, "(%a+)") if tonumber(intable(months, string.lower(potentialmonth))) > 0 then numericalmonth = intable(months, string.lower(potentialmonth)) month = fullmonths[tonumber(numericalmonth)] -- add a zero to number if necessary if tonumber(numericalmonth) < 10 then numericalmonth = 0 .. numericalmonth end unformatteddate = string.gsub(unformatteddate, month, "", 1) else month = "" end year = string.match(unformatteddate, "(%d+)") or "" if not (year
"" or month
"" then if day
"" or day
-- special cases thirtydays = if not (numericalmonth
2 then if tonumber(year) % 4
nil or outputformat
"mdy" then message = message .. month .. " " .. day .. ", " .. year elseif outputformat
nil or month
nil then message = message .. day else message = message .. year end else message = message .. year .. "-" .. numericalmonth .. "-" .. day end elseif outputformat
"month and year" then message = message .. month .. " " .. year end if string.match(unformatteddate, "AD") then message = message .. " AD" elseif string.match(unformatteddate, "BCE") then message = message .. " BCE" elseif string.match(unformatteddate, "BC") then message = message .. " BC" elseif string.match(unformatteddate, "CE") then message = message .. " CE" end if invalid then message = "Invalid entry" end return message endreturn p