Module:Sandbox/Safan41/Dates

local p = {} -- p stands for package

function p.dateformat( frame ) local date, format = frame.args.date or "", frame.args.format -- Arguments local mnthlst = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} function dateextract(date) -- find date pattern, use os.time to retrieve month, day, year = 1, 1, 1 haha = true if string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)") then day, month, year = string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)") elseif string.match(date, "(%a+) (%d+)%a?%a?, (%d+)") then month, day, year = string.match(date, "(%a+) (%d+)%a?%a?, (%d+)") elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") then day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)") elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BC") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) BCE") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) AD") or string.match(date, "(%d+)%a?%a? (%a+) (%d+) CE") then day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)") elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") then day, month, year = string.match(date, "(%d+)%a?%a? (%a+) (%d+)") elseif string.match(date, "(%d+)-(%d+)-(%d+)") then year, month, day = string.match(date, "(%d+)-(%d+)-(%d+)") elseif string.match(date, "(%d+)/(%d+)/(%d+)") then day, month, year = string.match(date, "(%d+)/(%d+)/(%d+)") elseif string.match(date, "(%a+) (%d+)") then month, year = string.match(date, "(%a+) (%d+)") elseif string.match(date, "(%d+)%a?%a? (%d+)") then day, year = string.match(date, "(%d+)%a?%a? (%d+)") elseif string.match(date, "(%d+) BC") or string.match(date, "(%d+) BCE") or string.match(date, "(%d+) AD") or string.match(date, "(%d+) CE") then year = string.match(date, "(%d+)") elseif string.match(date, "(%d+)%a?%a? (%a+)") then year, month = string.match(date, "(%d+)%a?%a? (%a+)") haha = false elseif string.match(date, "(%d+)") then year = string.match(date, "(%d+)") else for mo in mnthlst do       if string.match(date, "(".. mo ..")") then month = string.match(date, "(".. mo ..")") end end year = string.match(date, "(%4d)") day = string.match(date, "(%2d)") or string.match(date, "(%2d)th") end if type(month) == "string" then for k,v in ipairs(mnthlst) do        if string.upper(v) == string.upper(month) or string.upper(string.sub(v, 1, 3)) == string.sub(string.upper(month), 1, 3) then month = k       	haha = true do break end end end end if not haha then return os.time{year=year, month=1, day=day}, haha else return os.time{year=year, month=month, day=day}, haha end end a, b, c = pcall(dateextract, date) if a == false then return "Invalid entry" end yeet = b --date information if format then -- if there is a format, find format date using os.date as specified if format == "mdy" then text = os.date("%B %d, %Y", yeet) elseif format == "dmy" then text = os.date("%d %B %Y", yeet) elseif format == "iso" then text = os.date("%Y-%m-%d", yeet) elseif format == "year" then text = os.date("%Y", yeet) elseif format == "monthandyear" then text = os.date("%B %Y", yeet) else -- no format, go off of pattern if string.match(date, "(%a+) (%d+)%a?%a?, (%d+)") then text = os.date("%B %d, %Y", yeet) elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) AD") then text = os.date("%d %B %Y", yeet) .. " AD" elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BCE") then text = os.date("%d %B %Y", yeet) .. " BCE" elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) CE") then text = os.date("%d %B %Y", yeet) .. " CE" elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+) BC") then text = os.date("%d %B %Y", yeet) .. " BC" elseif string.match(date, "(%d+) AD") then text = os.date("%Y", yeet) .. " AD" elseif string.match(date, "(%d+) BCE") then text = os.date("%Y", yeet) .. " BCE" elseif string.match(date, "(%d+) CE") then text = os.date("%Y", yeet) .. " CE" elseif string.match(date, "(%d+) BC") then text = os.date("%Y", yeet) .. " BC" elseif string.match(date, "(%d+)%a?%a? (%a+) (%d+)") or string.match(date, "on the (%d+)%a?%a? of (%a+) in the year of our Lord (%d+)") then text = os.date("%d %B %Y", yeet) elseif string.match(date, "(%d+)-(%d+)-(%d+)") or string.match(date, "(%d+)/(%d+)/(%d+)") then text = os.date("%Y-%m-%d", yeet) elseif string.match(date, "(%d+) (%d+)") then text = os.date("%Y", yeet) elseif string.match(date, "(%d+)%a?%a? (%a+)") then if not c then text = os.date("%Y", yeet) else text = os.date("%Y %B", yeet) end elseif string.match(date, "(%a+) (%d+)") then if not pcall(os.date("%B %Y", yeet)) then text = string.match(date, "(%d+)") else text = os.date("%B %Y", yeet) end elseif string.match(date, "(%d+)") then if not pcall(os.date("%Y", yeet)) then --If os.date can't validate any numbers, return the numbers only text = string.match(date, "(%d+)") else text = os.date("%Y", yeet) end end end end if string.match(date, "^sometime around ") or string.match(date, " %(uncertain%)$") then text = "circa " .. text end text = text:gsub('^0', ''):gsub(' 0', ' ') return text end

return p