Jump to content

Module:Module Util

From SPS Wiki

This is a module intended for utility functions in the various templates and modules.


local p = {}

-- DATE FORMATS
local dateTimeFormat ="(%d+)-(%d+)-(%d+) (%d+):(%d+)"
local dateFormat = "(%d+)-(%d+)-(%d+)"

--Simple dirty function to print out tables/arrays
function p.dumpTable(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. p.dumpTable(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

--Function to determine if string is empty value 
local function isEmpty(s)
  return s == nil or s == ''
end

--wrapper function if invoked from a template
function p.presentWrittenDate(frame)
	return p._presentWrittenDate(frame.args[1])
end

--overload function to present date in "1 January 2023" format
function p._presentWrittenDate(dateString)
	p._presentWrittenDate(dateString, "no hours", false)
end

--overload function to present date in "1 January 2023 18:00" format
function p._presentWrittenDate(dateString, hoursFlag)
	p._presentWrittenDate(dateString, hoursFlag, false)
end

--function to present date in "1 January 2023" format
function p._presentWrittenDate(dateString, hoursFlag, shortMonthFlag)
	-- CHECK IF UNKNOWN
	if dateString == "Unknown" then
		return "Unknown"
	else
		local monthFormat = (shortMonthFlag == true and "%b" or "%B")
		local y,m,d,H,M = dateString:match(dateTimeFormat)
		if (y == nil) or (m == nil) or (d == nil) or (H == nil) or (M == nil) then
			local y,m,d = dateString:match(dateFormat) --if anything is nil try reduced format
			if (y == nil) or (m == nil) or (d == nil) then
				return "Cannot parse date.[[Category:Error:Incorrect Date Format]]" --if still nil then error handling
			else
				local day = os.time({year = y, month = m, day = d, hour = 0, min = 0})
				return os.date("%d "..monthFormat.." %Y",day)
			end
		else
			local day = os.time({year = y, month = m, day = d, hour = H, min = M})
			if hoursFlag == "no hours" then
				return os.date("%d "..monthFormat.." %Y",day)
			else
				return os.date("%d "..monthFormat.." %Y %H:%M",day)
			end
		end
	end
end

--wrapper function if invoked from a template
function p.isPast(frame)
	return p._isPast(frame.args)
end

--function to determine if the current date is later than a series of submitted dates
function p._isPast(array)
	-- loop through all the submitted dates and compare to current
	for key,param in pairs(array) do
		if isEmpty(param)==false then
			local dateVar
			local y,m,d,H,M = param:match(dateTimeFormat)
			if (y == nil) or (m == nil) or (d == nil) or (H == nil) or (M == nil) then
				local y,m,d = param:match(dateFormat) --if anything is nil try reduced format
				if (y == nil) or (m == nil) or (d == nil) then
					return "Could not parse one of the dates. Please check the input: "..param
				else
					dateVar = os.time({year = y, month = m, day = d, hour = 0, min = 0})
				end
			else
				dateVar = os.time({year = y, month = m, day = d, hour = H, min = M})
			end
			if os.time() < dateVar then
				return false --if the current date is before the selected date, it is not past
			end
		end
	end
	return true --if cycled through all inputs and no date was later than current date
end

--wrapper function if invoked from a template
function p.isUpcoming(frame)
	return p._isUpcoming(frame.args)
end

--function to determine if the current date is later than a series of submitted dates
function p._isUpcoming(array)
	-- loop through all the submitted dates and compare to current
	for key,param in pairs(array) do
		if isEmpty(param)==false then
			local dateVar
			local y,m,d,H,M = param:match(dateTimeFormat)
			if (y == nil) or (m == nil) or (d == nil) or (H == nil) or (M == nil) then
				local y,m,d = param:match(dateFormat) --if anything is nil try reduced format
				if (y == nil) or (m == nil) or (d == nil) then
					return "Could not parse one of the dates. Please check the input: "..param
				else
					dateVar = os.time({year = y, month = m, day = d, hour = 0, min = 0})
				end
			else
				dateVar = os.time({year = y, month = m, day = d, hour = H, min = M})
			end
			if os.time() < dateVar then
				return true --if the current date is before the selected date, it is upcoming
			end
		end
	end
	return false --if cycled through all inputs and no date was later than current date
end

--wrapper function if invoked from a template
function p.returnLatestDate(frame)
	return p._returnLatestDate(frame.args)
end

--function to return latest date in a submitted series
function p._returnLatestDate(array)
	local result = {}
	-- loop through all the submitted dates and compare to current
	for key,param in pairs(array) do
		if isEmpty(param)==false then
			local dateVar
			local y,m,d,H,M = param:match(dateTimeFormat)
			if (y == nil) or (m == nil) or (d == nil) or (H == nil) or (M == nil) then
				local y,m,d = param:match(dateFormat) --if anything is nil try reduced format
				if (y == nil) or (m == nil) or (d == nil) then
					return "Could not parse one of the dates. Please check the input."
				else
					dateVar = os.time({year = y, month = m, day = d, hour = 0, min = 0})
				end
			else
				dateVar = os.time({year = y, month = m, day = d, hour = H, min = M})
			end
			table.insert(result, dateVar)
		end
	end
	table.sort(result) --sort the list
	return os.date("%Y-%m-%d %H:%M",result[#result]) --return last item in the list
end

--wrapper function if invoked from a template
function p.parseDateIntoUnix(frame)
	return p._parseDateIntoUnix(frame.args[1])
end

--function to return date in UNIX format
function p._parseDateIntoUnix(dateString)
	local dateVar
	-- if not empty, parse
	if isEmpty(dateString)==false then
		local y,m,d,H,M = dateString:match(dateTimeFormat)
		if (y == nil) or (m == nil) or (d == nil) or (H == nil) or (M == nil) then
			local y,m,d = dateString:match(dateFormat) --if anything is nil try reduced format
			if (y == nil) or (m == nil) or (d == nil) then
				return "Could not parse one of the dates. Please check the input."
			else
				dateVar = os.time({year = y, month = m, day = d, hour = 0, min = 0})
			end
		else
			dateVar = os.time({year = y, month = m, day = d, hour = H, min = M})
		end
	end
	return dateVar --return UNIX format date
end

function p.main(frame)
	return frame
end

return p