Module:Sandbox/Redlead07/Dates

local p = {}

function p.dmyformat(frame) local dmydate, format = frame.args.dmydate or "", frame.args.format or "" local a = string.match(dmydate, "(%d+) %w+ %d+") or 	string.match(dmydate, "(%w+) %d+, %d+") or	string.match(dmydate, "(%w+) %d+ %d+") or 	string.match(dmydate, "(%d+)/%d+/%d+") or 	string.match(dmydate, "(%d+)-%d+-%d+") or	string.match(dmydate, "(%d+)%w+ %w+ %d+") or	string.match(dmydate, "(%w+) %d+") or 	string.match(dmydate, "(%d+) %w+") or 	string.match(dmydate, "(%d+) %d+") or	string.match(dmydate, "(%w+)") or 	string.match(dmydate, "(%d+) %w+ %w+") or 	string.match(dmydate, "(%d+)%w+ %u%w+ %d+") or "" local b = string.match(dmydate, "%d+ (%w+) %d+") or 	string.match(dmydate, "%w+ (%d+), %d+") or	string.match(dmydate, "%w+ (%d+) %d+") or 	string.match(dmydate, "%d+/(%d+)/%d+") or 	string.match(dmydate, "%d+-(%d+)-%d+") or	string.match(dmydate, "%d+%w+ (%w+) %d+") or 	string.match(dmydate, "%w+ (%d+)") or 	string.match(dmydate, "%d+ (%w+)") or	string.match(dmydate, "%d+ (%d+)") or	string.match(dmydate, "%d+%w+ (%u)(%w+) %d+") or "" local c = string.match(dmydate, "%d+ %w+ (%d+)") or 	string.match(dmydate, "%d+ %d+ (%d+)") or 	string.match(dmydate, "%w+ %d+, (%d+)") or string.match(dmydate, "%d+/%d+/(%d+)") or 	string.match(dmydate, "%d+-%d+-(%d+)") or	string.match(dmydate, "%w+ %d+ (%d+)") or	string.match(dmydate, "%d+%w+ %w+ (%d+)") or 	string.match(dmydate, "%d+%w+ %u%w+ (%d+)") or "" local d = string.match(dmydate, "%d+ %w+ %d+ (%a+)") or "" local e = string.match(dmydate, "(%d+)-(%d+)-(%d+)") or "" local f = string.match(dmydate, "(%w+) (%d+), (%d+)") or "" local g = string.match(dmydate, "(%d+)/(%d+)/(%d+)") or "" local h = string.match(dmydate, "(%d+) (%w+) (%d+)") or "" local i = string.match(dmydate, "(%d+)") or "" local j = string.match(dmydate, "(%d+) (%w+)") or "" local k = string.match(dmydate, "(%d+) (%d+)") or "" local l = string.match(dmydate, "(%d+)(%w+) (%w+) (%d+)") or "" local m = string.match(dmydate, "(%d+) (%w+) (%d+) (%w+)") or "" local n = string.match(dmydate, "(%w+) (%w+) (%w+) (%w+)") or "" local o = string.match(dmydate, "(%d+)%w+ (%u)(%w+) (%d+)") or "" local p = string.match(dmydate, "(%d+)%U+(%a+)%D+(%d+)") or "" local a2 = tonumber(a) or 0 local a3 = string.find(a, "%d+") or "" local a4 = string.find(a, "%d?") or "" local a5 = string.find(a, "%a+") or "" local b2 = tonumber(b) or 0 local b3 = string.find(b, "%a+") or "" local c2 = tonumber(c) or 0 local c3 = string.find(a, "%d+") or "" local day = "" local month = "" local year = "" if i ~= "" and a2>0 and a2>31 then year = a2 end if a2>0 and a4 ~= "" then day = a2 end if a5 == "" then a5 = "" else month = a end if c2>0 and c2>31 and c3 ~= "" then year = c end if year == c and a2>0 and a4 ~= "" then day = a2		month = b		end if year == a2 then month = b 		day = c 		end if day == a2 then month = b 		year = c 		end if month == a2 and c3 ~= "" then day = b 		year = c 		end if month == a2 and c == "" then year = b 		end if day == a2 and c == "" and a3 ~= "" then year = b 		end if e ~= "" then year = a2		month = b		day = c		end if f ~= "" then month = a		day = b		year = c		end if g ~= "" then day = a2		month = b		year = c		end if k ~= "" then day = a		year = b		end if o ~= "" then day = a2		month = b		year = c		end if a2 ~= day and b ~= day and c ~= day then day = "" end if a2 ~= year and b ~= year and c ~= year then year = "" end local years = string.match(dmydate, "BCE") or string.match(dmydate, "BC") or	string.match(dmydate, "CE") or string.match(dmydate, "AD") or "" if j ~= "" and c == "" and d == "" and years ~= "" then year = a2		years = b		end local days = tonumber(day) or 0 year = tonumber(year) or 0 local mnthnumber = string.match(dmydate, "%d+/(%d+)/%d+") or string.match(dmydate, "%d+-(%d+)-%d+") mnthnumber = tonumber(mnthnumber) or 0 if 	mnthnumber ~= 0 then mnthname = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} month = mnthname[mnthnumber] end month = month:gsub("^%l", string.upper) local monthname = {Jan = "January", Feb = "February", Mar = "March", Apr = "April", May = "May", Jun = "June", Jul = "July", Aug = "August", Sep = "September", Oct = "October", Nov = "November", Dec = "December"} or "" local monthname2 = {January = "January", February = "February", March = "March", April = "April", May = "May", June = "June", July = "July", August = "August", September = "September", October = "October", November = "November", December = "December"} or "" local monthnum = {Jan = "01", Feb = "02", Mar = "03", Apr = "04", May = "05", Jun = "06", Jul = "07", Aug = "08", Sep = "09", Oct = "10", Nov = "11", Dec = "12"} local monthnum2 = {January = "01", February = "02", March = "03", April = "04", May = "05", June = "06", July = "07", August = "08", September = "09", October = "10", November = "11", December = "12"} or "" monthnumber = monthnum[(month)] or monthnum2[(month)] months = monthname[(month)] or monthname2[(month)] if month == "feb" and (year % 4 == 0) and (year % 100 ~= 0) or (year % 400) == 0 then days_in_month = 29 else days_in_month = 28 end if month == "February" and (year % 4 == 0) and (year % 100 ~= 0) or (year % 400) == 0 then days_in_month = 29 else days_in_month = 28 end if months == "jan" or months == "January" then days_in_month = 31 end if months == "mar" or months == "March" then days_in_month = 31 end if months == "apr" or months == "April" then days_in_month = 30 end if months == "may" or months == "May" then days_in_month = 31 end if months == "jun" or months == "June" then days_in_month = 30 end if months == "jul" or months == "July" then days_in_month = 31 end if months == "aug" or months == "August" then days_in_month = 31 end if months == "sep" or months == "September" then days_in_month = 30 end if months == "oct" or months == "October" then days_in_month = 31 end if months == "nov" or months == "November" then days_in_month = 30 end if months == "dec" or months == "December" then days_in_month = 31 end if format == "" and i ~= "" and j ~= "" and years ~= "" and m == "" then local no = string.format("%u %s", year, years) return no.." " end if year ~= 0 and month ~= "" and days ~= 0 and years == "" and h ~= "" and j == "" and days days_in_month then return "Invalid entry".." " end local circa = string.match(dmydate, "around") or string.match(dmydate, "sometime") or 	string.match(dmydate, "uncertain") or "" if p ~= "" and years == "" and circa == "" and m == "" and h == "" then d, m, y = string.match(dmydate, "(%d+)%U+(%a+)%D+(%d+)") return d.." "..m.." "..y.." " end if circa == "" and m == "" and n ~= "" then return "Invalid entry".." " end if circa == "" and h ~= "" and format == "" and m ~= "" and years == "" and o == "" then return "Invalid entry".." " end if circa ~= "" and format == "" and h ~= "" then local no = string.format("%u %s %u", day, months, year) return "circa".." "..no.." " end if circa ~= "" and format == "mdy" then local mdy = string.format("%s %u, %u", months, day, year) return "circa".." "..mdy.." " end if circa ~= "" and format == "year" and format ~= "" then local y = string.format("%u", year) return "circa".." "..y.." " end if circa ~= "" and format == "" and i ~= "" and l == "" then local no = string.format("%u", year) return "circa".." "..no.." " end if circa ~="" and format == "" and l ~= "" then local no = string.format("%u %s %u", day, months, year) return "circa".." "..no.." " end

if format == "iso" then local iso = string.format("%u-%u-%u", year, monthnumber, day) return iso.." " end if format == "mdy" then local mdy = string.format("%s %u, %u", months, day, year) return mdy.." " end if format == "dmy" then local dmy = string.format("%u %s %u", day, months, year) return dmy.." " end if format == "year" then local y = string.format("%u", year) return y.." " end if format == "" and j ~= "" and c == "" and k == "" and a2<31 then local no = string.format("%u %s", day, months) return no.." " end if format == "" and years ~= "" and m ~= "" then local no = string.format("%u %s %u %s", day, months, year, years) return no.." " end if format == "" and i ~= "" and b == "" and c == "" and a2<31 then local no = string.format("%u", day) return no.." " end if format == "" and i ~= "" and c == "" and b2 == 0 and a2<31 then local no = string.format("%u", day) return no.." " end if format == "" and h ~= "" then local no = string.format("%u %s %u", day, months, year) return no.." " end if format == "" and k ~= "" and c == "" then local no = string.format("%u", year) return no.." " end if format == "" and a2>31 and i ~= "" and j~= "" then local no = string.format("%u", year) return no.." " end if format == "" and c == "" and b == "" and a2>0 and a2>31 then local no = string.format("%u", year) return no.." " end if format == "" and g ~= "" then local no = string.format("%u-%u-%u", year, monthnumber, day) return no.." " end if format == "mdy" and e ~= "" then local mdy = string.format("%s %u, %u", months, day, year) return mdy.." " end if format == "" and e ~= "" then local no = string.format("%u-%u-%u", year, monthnumber, day) return no.." " end if format == "" and o ~= "" then local no = string.format("%u %s %u", day, months, year) return no.." " end end

return p