local main = {};
local monthIndices = {
['january'] = 1,
['february'] = 2,
['march'] = 3,
['april'] = 4,
['may'] = 5,
['june'] = 6,
['july'] = 7,
['august'] = 8,
['september'] = 9,
['october'] = 10,
['november'] = 11,
['december'] = 12
}
local monthDays = {
[1] = 31,
[2] = 29, -- will check below
[3] = 31,
[4] = 30,
[5] = 31,
[6] = 30,
[7] = 31,
[8] = 31,
[9] = 30,
[10] = 31,
[11] = 30,
[12] = 31
}
function tryParseDate(input)
-- First try dMy
local matchDay, matchMonth, matchYear = input:match('^([0-9][0-9]?) ([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')
if (matchDay ~= nil) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return { tonumber(matchDay), month, tonumber(matchYear) }
end
return nil
end
function main.hello(frame)
--[[
local args = frame.args
local arg1 = args[1]
local arg2 = args[2]
local arg3 = args[3]
local argNamed = args['named']
local s = 'Params: '
if (arg1 ~= nil) then s = s .. '1 = ' .. arg1 end
if (arg2 ~= nil) then s = s .. '; 2 = ' .. arg2 end
if (arg3 ~= nil) then s = s .. '; 3 = ' .. arg3 end
if (argNamed ~= nil) then s = s .. '; named = ' .. argNamed end
return s
]]
local args = frame.args
local input = args[1]
local day, month, year = tryParseDate(input)
if (day == nil) then
return input .. ' -> is not a recognized pattern match' end
if (year < 1760) then
return input .. ' -> year is too early (calendar discrepancy)' end
if (year > 2100) then
return input .. ' -> year is too far into future' end
if (day == 0) then
return input .. ' -> day is invalid == 0' end
if (day > monthDays[month]) then
return input .. ' -> day is invalid > ' .. monthDays[month] end
-- February leap year check
if (month == 2) then
if (day == 29 and not ((year % 4 == 0) and (year % 100 ~= 0) or (year % 400 == 0))) then
return input .. ' -> day is invalid > 28'
end
end
return input .. ' -> parsed to ' .. year .. '-' .. month .. '-' .. day; -- won't add 0s
end
return main