Module:Time

From Nookipedia, the Animal Crossing wiki

Documentation for this module may be created at Module:Time/doc

local p = {}

local function getArgs(frame)
    local args = {}
    for key, value in pairs(frame:getParent().args) do
        args[key] = value
    end
    for key, value in pairs(frame.args) do
        args[key] = value
    end
    return args
end

function p.main(frame)
    local args      = getArgs(frame)
    local seconds   = args[1] or args.seconds or ''
    return p.timeFormat(seconds)
end

-- Function from StackOverflow https://stackoverflow.com/questions/18313171/lua-rounding-numbers-and-then-truncate
function round(number)
	return number - (number % 1)
end

function p.timeFormat(seconds)
    local hours = round(seconds / 3600)
    local minutes = round((seconds % 3600) / 60)
    local secs = round((seconds % 3600) % 60)
    local print = ""
    if hours > 0 then
    	print = print .. hours .. " hour"
    	if hours > 1 then
    		print = print .. "s"
    	end
    	if minutes > 0 then
    		if secs > 0 then
				print = print .. ", " .. minutes .. " minute"
				if minutes > 1 then
					print = print .. "s"
				end
				print = print .. ", and " .. secs .. " second"
				if secs > 1 then
					print = print .. "s"
				end
			else
				print = print .. " and " .. minutes .. " minute"
				if minutes > 1 then
					print = print .. "s"
				end
			end
    	elseif secs > 0 then
    		print = print .. " and " .. secs .. " second"
    		if secs > 1 then
    			print = print .. "s"
    		end
    	end
    elseif minutes > 0 then
    	print = print .. minutes .. " minute"
		if minutes > 1 then
			print = print .. "s"
		end
		if secs > 0 then
			print = print .. " and " .. secs .. " second"
			if secs > 1 then
				print = print .. "s"
			end
		end
    elseif secs > 0 then
    	print = print .. secs .. " second"
		if secs > 1 then
			print = print .. "s"
		end
    end
	return print
end
return p