Documentation for this module may be created at Module:Sandbox/Ste1la/Dates/doc
-- 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 = {"jan", "january", "feb", "february", "feb", "mar", "march",
"apr", "april", "may", "jun", "june", "jul", "july", "aug", "august",
"sep", "september", "oct", "october", "nov", "november", "dec", "december"}
local obtain = 0
--> extracting dates
--check if is uncertain and if epoca exists
--[[ = date:find("uncertain")
if circa == nil then
circa = date:find("sometime")
end
if circa == nil then
circa = date:find("around")
end
if circa ~= nil then
out = out .. "circa "
end]]
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("BCE")
if epoca ~= nil then
epoca = "BCE"
elseif epoca == nil then
epoca = date:find("BC")
if epoca ~= nil then
epoca = "BC"
elseif epoca == nil then
epoca = date:find("CE")
if epoca ~= nil then
epoca = "CE"
end
end
end
end
--dmy
local day, month, year = date:match("(%d+) (%w+) (%d+)")
if day ~= nil and month ~= nil and year ~= nil then
obtain = 1
inputFormat = "dmy"
end
--mdy
if obtain == 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
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 == 0 then
local i, j = date:find("%d+")
if i ~= nil then
year = date:sub(i, j) or 0
inputFormat = "year"
if i ~= nil then
i, j = date:find("%d+", j + 1)
if i ~= nil then
day = date:sub(i, j) or 0
inputFormat = "day"
end
end
end
for i = 1, 23 do
local temp = date:lower()
if temp:match(monthList[i]) ~= nil then
month = temp:match(monthList[i])
if inputFormat == "year" then
inputFormat = "month and year"
elseif inputFormat == "day" then
inputFormat = "all"
end
end
i = i + 1
end
if month == nil then
month = ""
end
if day == nil then
day = 0
end
if year == nil then
year = 0
end
end
if (day == "" or day == 0) and (year == "" or year == 0) then
return "Invalid entry"
end
if inputFormat == "day" then
inputFormat = "year"
end
--converts month to number
if tonumber(month) == 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 > 29 then
return "Invalid entry"
end
if month == 2 and day == 29 then
local leap = year % 4
if leap ~= 0 then
return "Invalid entry"
end
end
--OUTPUTS DATAAAAAAAAAAA!!!
-->reformat data if necessary
if format ~= "" then
inputFormat = ""
end
if format == "dmy" or format == "mdy" or format == "month and year" or
inputFormat == "dmy" or inputFormat == "mdy" or inputFormat == "month and year" or
inputFormat == "all" then
if month == 1 then
month = "January"
elseif month == 2 then
month = "February"
elseif month == 3 then
month = "March"
elseif month == 4 then
month = "April"
elseif month == 5 then
month = "May"
elseif month == 6 then
month = "June"
elseif month == 7 then
month = "July"
elseif month == 8 then
month = "August"
elseif month == 9 then
month = "September"
elseif month == 10 then
month = "October"
elseif month == 11 then
month = "November"
elseif month == 12 then
month = "December"
end
end
if day == 0 then
day = ""
end
if month == 0 then
month = ""
end
if year == 0 then
year = ""
end
if format == "dmy" then
if day < 31 and year < 31 and month == "" then
return "Invalid entry"
end
out = out .. day .. " " .. month .. " " .. year
elseif format == "mdy" then
out = out .. month .. " " .. day .. ", " .. year
elseif format == "iso" then
if month < 10 then
month = "0" .. month
end
out = out .. year .. "-" .. month .. "-" .. day
elseif format == "year" then
out = out .. year
elseif format == "month and year" then
out = out .. month .. " " .. year
elseif inputFormat == "dmy" then
if tonumber(day) < 31 and tonumber(year) < 31 and month == "" then
return "Invalid entry"
end
out = out .. day .. " " .. month .. " " .. year
elseif inputFormat == "mdy" then
out = out .. month .. " " .. day .. ", " .. year
elseif inputFormat == "iso" then
if month < 10 then
month = "0" .. month
end
out = out .. year .. "-" .. month .. "-" .. day
elseif inputFormat == "year" then
out = out .. year
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 .. "<br>"
end
return p