imported>Smsingh34
No edit summary
 
(No difference)

Latest revision as of 18:26, 21 November 2018

Documentation for this module may be created at Module:Sandbox/Smsingh34/Dates/doc

-- smsingh Task 7, Date Formatting
local p = {}
p.formatdate = function(frame)
	local unformatteddate = frame.args.unformatteddate
	local outputformat = frame.args.outputformat
-- extracting day, month, and year
	function intable(tabl, value)
		for i = 1, #tabl do
			if tabl[i] == string.sub(value, 1, 3) then
				return i
			end
		end
		return 0
	end
	
	message = ""
	-- adding circa
	if not (string.match(unformatteddate, "uncertain") == nil) then
		message = "circa "
		unformatteddate = string.gsub(unformatteddate, "uncertain", "", 1)
	elseif not (string.match(unformatteddate, "sometime around") == nil) then
		message = "circa "
		unformatteddate = string.gsub(unformatteddate, "sometime around", "", 1)
	end
	invalid = false
	
	
	months = {'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'}
	fullmonths = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'}
	if string.match(unformatteddate, "(%a+)") == nil then
		if outputformat == nil or outputformat == "" then
			outputformat = "iso"
		end
		if tonumber(string.match(unformatteddate, "(%d+)")) < 32 then
			day = string.match(unformatteddate, "(%d+)")
			unformatteddate = string.gsub(unformatteddate, day, "", 1)
		elseif tonumber(string.match(unformatteddate, "(%d+)")) > 31 then
			year = string.match(unformatteddate, "(%d+)")
			unformatteddate = string.gsub(unformatteddate, year, "", 1)
		else
			day = ""
		end
		
		if (tonumber(string.match(unformatteddate, "(%d+)")) or 14) < 13 then
			numericalmonth = string.match(unformatteddate, "(%d+)") or ""
			month = fullmonths[tonumber(numericalmonth)] or ""
			unformatteddate = string.gsub(unformatteddate, numericalmonth, "", 1)
		else
			month = ""
		end
		
		if year == nil then
			year = string.match(unformatteddate, "(%d+)") or ""
		elseif (day == nil) and not (tonumber(string.match(unformatteddate, "(%d+)")) == nil) then
			if tonumber(string.match(unformatteddate, "(%d+)")) < 32 then
				day = tonumber(string.match(unformatteddate, "(%d+)"))
			end
		end
		
	-- invalid
	if month == "" or month == nil then
		if year == "" then
			if day == "" then
				invalid = true
			else
				message = message .. day
				return message
			end
		else
			if day == "" or day == nil then
				message = message .. year
				return message
			else
				message = message
			end
		end
	end
		
	else
		-- miscellaneous phrases
		unformatteddate = string.gsub(unformatteddate, "on the", "", 1)
		unformatteddate = string.gsub(unformatteddate, "of", "", 1)
		unformatteddate = string.gsub(unformatteddate, "in the year of our Lord", "", 1)
		unformatteddate = string.gsub(unformatteddate, "rd", "", 1)
		unformatteddate = string.gsub(unformatteddate, "th", "", 1)
		
		if tonumber(string.match(unformatteddate, "(%d+)")) == nil then
			return "Invalid entry"
		elseif tonumber(string.match(unformatteddate, "(%d+)")) < 32 then
			day = string.match(unformatteddate, "(%d+)")
			unformatteddate = string.gsub(unformatteddate, day, "", 1)
		else
			day = ""
		end
		
		potentialmonth = string.match(unformatteddate, "(%a+)")
		if tonumber(intable(months, string.lower(potentialmonth))) > 0 then
			numericalmonth = intable(months, string.lower(potentialmonth))
			month = fullmonths[tonumber(numericalmonth)]
			
			-- add a zero to number if necessary
			if tonumber(numericalmonth) < 10 then
				numericalmonth = 0 .. numericalmonth
			end
			unformatteddate = string.gsub(unformatteddate, month, "", 1)
		else
			month = ""
		end
		
		year = string.match(unformatteddate, "(%d+)") or ""
		if not (year == nil) then
			unformatteddate = string.gsub(unformatteddate, year, "", 1)
		end
		
	-- invalid
		if month == "" or month == nil then
			if year == "" then
				if day == "" then
					invalid = true
				else
					message = message
				end
			else
				if day == "" or day == nil then
					message = message
				else
					invalid = true
				end
			end
		end
	end

-- special cases
	thirtydays = {4, 6, 9, 11}
	if not (numericalmonth == nil) then
		if tonumber(numericalmonth) == 2 then
			if tonumber(year) % 4 == 0 then
				if tonumber(day) > 29 then
					invalid = true
				end
			else
				if tonumber(day) > 28 then
					invalid = true
				end
			end
		elseif intable(thirtydays, tonumber(numericalmonth)) > 0 then
			if tonumber(day) > 30 then
				invalid = true
			end
		end
	end
	
	
-- formatting the date
	-- capitalize first letter of month
			local firstletter = string.sub(month, 1, 1)
			firstletter = string.upper(firstletter)
			local restofword = string.sub(month, 2)
			month = firstletter .. restofword
	if outputformat == nil or outputformat == "" then
		message = message .. day .. " " .. month .. " " .. year
	elseif outputformat == "mdy" then
		message = message .. month .. " " .. day .. ", " .. year
	elseif outputformat == "iso" then
		if numericalmonth == nil or month == nil then
			if year == nil then
				message = message .. day
			else
				message = message .. year
			end
		else
			message = message .. year .. "-" .. numericalmonth .. "-" .. day
		end
	elseif outputformat == "year" then
		message = message .. year
	elseif outputformat == "month and year" then
		message = message .. month .. " " .. year
	end
	
	if string.match(unformatteddate, "AD") then
		message = message .. " AD"
	elseif string.match(unformatteddate, "BCE") then
		message = message .. " BCE"
	elseif string.match(unformatteddate, "BC") then
		message = message .. " BC"
	elseif string.match(unformatteddate, "CE") then
		message = message .. " CE"
	end
		
	if invalid then message = "Invalid entry" end
	
	return message
	end
return p