-- Userring Google Code In
-- Lua Task 9 - Date formatsp =
function p.getdate(frame) -- Function to get a date from a given text -- Getting inputs of the text and format local instring = frame.args.text or "" local given_outformat = frame.args.format or "" local given_outvalue = 0 local text_format = "" local text_format_value = 0 -- Assigning general variables which will be later used in the code local months = local mplace, month_number, d, m, y = 0, 0, 0, 0 local certainty = nil -- Functions for processes that are repetitively executed. local function get_month_from_text possible_months = for possible_month in string.gmatch(instring, "%a+") do unchanged_mon = possible_month mon_tocheck = string.lower(possible_month) mon_tocheck_1 = string.sub(mon_tocheck, 1, 1) mon_tocheck_rest = string.sub(mon_tocheck, 2) mon_tocheck = string.sub(string.upper(mon_tocheck_1) .. mon_tocheck_rest, 1, 3) for i=1, table.getn(months) do checking_against = string.sub(months[i], 1, 3) if mon_tocheck
4 or month_number
9 or month_number
2 then if y%400
0) then max_date = 29 else max_date = 28 end else max_date = 31 end if d > max_date then return false else return true end end local function get_output -- Function to return the output certainty = string.find(instring, "uncertain") or string.find(instring, "around") if certainty
"mdy" then verified = verify_date if verified
0 then m = months[tonumber(m)] return certainty .. m .. " " .. d .. ", " .. y .. " " .. addition else return certainty .. months[tonumber(month_number)] .. " " .. d .. ", " .. y .. " " .. addition end end elseif text_format
false then return "Invalid entry." else if month_number
"my" then return certainty .. m .. " " .. y .. " " .. addition elseif text_format
"iso" then if month_number
"Invalid" then return "Invalid entry" end end local function verify_format -- Function to verify the format given if given_outformat
"mdy" then given_outvalue = 3 return true elseif given_outformat
"my" then given_outvalue = 2 return true elseif given_outformat
"" or given_outformat
false then return "Invalid Entry." end if instring
nil then return "Invalid Entry." end local function get_y -- Function to get the year y_tocheck = for pos_y in string.gmatch(instring, "%d+") do table.insert(y_tocheck, tonumber(pos_y)) end if table.getn(y_tocheck)
1 then text_format, text_format_value = "year", 1 y = y_tocheck[1] else pos_y = y_tocheck[table.maxn(y_tocheck)] if tonumber(pos_y) > 31 then text_format, text_format_value = "year", 1 y = pos_y else text_format, text_format_value = "Invalid", 0 end end end -- Checking if the date is in an ISO format. iso_1, iso_2, iso_3 = string.match(instring, "(%d+)%p(%d+)%p(%d+)") -- If not, the date is the in format mdy, dmy, my or year if iso_1
nil or iso_3
false then get_y elseif month_checked
nil) then text_format, text_format_value = "dmy", 3 local pos_dy_string = string.sub(instring, mplace) y = string.match(pos_dy_string, "%d+") if y
0 and not(m
nil then text_format, text_format_value = "my", 2 local pos_my_string = string.sub(instring, mplace) y = string.match(pos_my_string, "%d+") else text_format, text_format_value = "mdy", 3 local pos_y_string = string.sub(instring, tonumber(pos_comma_place)) y = string.match(pos_y_string, "%d+") local pos_d_string = string.sub(instring, mplace, pos_comma_place) local pos_d = string.match(pos_d_string, "%d+") if tonumber(pos_d) <= 31 then d = tonumber(pos_d) end end elseif m
"" or given_outformat