-- Artbarte Google Code-in, Date formatting-- WIP
local p =
-- Checks if string is a format and returns format string-- Takes string | returns format string to be applied to os.date OR nilfunction parseFormat(str) local formats = formats["month and year"] = "%B %Y" return formats[str]end
-- Checks if string is a month or month abbreviation-- Takes string | returns integer of month OR nilfunction parseMonth(str) str = string.lower(str) local months = for i,v in ipairs(months) do if str
v[2] then return i end end return nilend-- Checks if the string includes on of the "circa words"function checkIfCirca (str) local dateString = str local circa = false local circaStrings = for i,v in ipairs(circaStrings) do dateString, matches = string.gsub(dateString, v, "") if matches >= 1 then circa = true end end return circa, dateString end
p.parseDate = function (frame) local date = frame.args.date or "" local format = frame.args.format or "" -- local circa = false circa, date = checkIfCirca(date) -- Pattern searches for 3 words sperated by spaces, slashes or dashes and puts them into variables: first, second, third local first,second,third = string.match(date, "([%w%d]+)[/-]([%w%d]+)[, /-] ?(%d+)")
-- If pattern didn't find any proper date format display error message local fields = if first ~= nil then table.insert(fields,first) else return "Invalid input
" end if second ~= nil then table.insert(fields,second) end if third ~= nil then table.insert(fields,third) end -- Putting fields into proper value local day,month,year for i,v in ipairs(fields) do local parsedMonth = parseMonth(v) if parsedMonth ~= nil then month = parsedMonth elseif tonumber(v) > 31 then -- year only year = v elseif tonumber(v) > 12 then -- day only day = v elseif month
return p