-- Ste1la Google Code-in, Date formatting
local p =
p.formatDate = function(frame) local date = frame.args.date or "" local format = frame.args.format or "" local circa local epoca = nil local out = "" local monthList = local obtain = 0 --> extracting dates --check if is uncertain and if epoca exists -- if date:find("uncertain") or date:find("sometime") or date:find("around") then out = out .. "circa " end epoca = date:find("AD") if epoca ~= nil then epoca = "AD" elseif epoca
nil then epoca = date:find("BC") if epoca ~= nil then epoca = "BC" elseif epoca
0 then month, day, year = date:match("(%w+) (%d+), (%d+)")
if day ~= nil and month ~= nil and year ~= nil then obtain = 1 inputFormat = "mdy" end end --three number cases if obtain
0 then day, month, year = date:match("(%d+)-(%d+)-(%d+)") if day ~= nil and month ~= nil and year ~= nil then obtain = 1 inputFormat = "iso" end end -- not any above, gets two numbers and checks for month if obtain
"year" then inputFormat = "month and year" elseif inputFormat
nil then month = "" end if day
nil then year = 0 end end
if (day
0) and (year
0) then return "Invalid entry" end if inputFormat
nil then monthChange = 1 month = month:lower if month:match("jan") then month = 1 elseif month:match("feb") then month = 2 elseif month:match("mar") then month = 3 elseif month:match("apr") then month = 4 elseif month:match("may") then month = 5 elseif month:match("jun") then month = 6 elseif month:match("jul") then month = 7 elseif month:match("aug") then month = 8 elseif month:match("sep") then month = 9 elseif month:match("oct") then month = 10 elseif month:match("nov") then month = 11 elseif month:match("dec") then month = 12 else month = 0 end end -->switches variables -- sets year day = tonumber(day) month = tonumber(month) if day > 31 then local temp = year year = day day = temp elseif month > 31 then local temp = year year = month month = temp end
if month > 12 then local temp = day day = month month = temp end
--checks leap year if month
2 and day
"dmy" or format
"month and year" or inputFormat
"mdy" or inputFormat
"all" then if month
2 then month = "February" elseif month
4 then month = "April" elseif month
6 then month = "June" elseif month
8 then month = "August" elseif month
10 then month = "October" elseif month
12 then month = "December" end end if day
0 then month = "" end if year
if format
"" then return "Invalid entry" end out = out .. day .. " " .. month .. " " .. year elseif format
"iso" then if month < 10 then month = "0" .. month end out = out .. year .. "-" .. month .. "-" .. day elseif format
"month and year" then out = out .. month .. " " .. year elseif inputFormat
"" then return "Invalid entry" end out = out .. day .. " " .. month .. " " .. year elseif inputFormat
"iso" then if month < 10 then month = "0" .. month end out = out .. year .. "-" .. month .. "-" .. day elseif inputFormat
"month and year" then out = out .. year .. " " .. month else if day ~= "" or day ~= nil then out = out .. day .. " " end if month ~= "" or month ~= nil then out = out .. month .. " " end if year ~= "" or year ~= nil then out = out .. year end end if epoca ~= nil then out = out .. " " .. epoca end return out .. "
"end
return p