Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Duration: Difference between revisions

From Stationeers Community Wiki
Tyraeis (talk | contribs)
Create duration module for converting seconds to a human-readable duration
 
Tyraeis (talk | contribs)
Receive arguments correctly
Line 1: Line 1:
local duration = {}
local duration = {}


function duration.fromSeconds(seconds)
function duration.fromSeconds(frame)
local seconds = tonumber(seconds)
local seconds = tonumber(frame.args[1])
if not seconds then
return "<strong>Error in Module:Duration: argument 1 ('" .. frame.args[1] .. "') is not a number</strong>"
end
 
local result = ""
local result = ""
if seconds >= 3600 then
if seconds >= 3600 then

Revision as of 03:22, 19 March 2026

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

local duration = {}

function duration.fromSeconds(frame)
	local seconds = tonumber(frame.args[1])
	if not seconds then
		return "<strong>Error in Module:Duration: argument 1 ('" .. frame.args[1] .. "') is not a number</strong>"
	end

	local result = ""
	if seconds >= 3600 then
		local hours = math.floor(seconds / 3600)
		seconds = seconds - hours*3600
		result = result .. hours .. "&nbsp;h"
	end
	if seconds >= 60 then
		if #result > 0 then
			result = result .. "&nbsp;"
		end
		local minutes = math.floor(seconds / 60)
		seconds = seconds - minutes * 60
		result = result .. minutes .. "&nbsp;min"
	end
	if seconds > 0 or #result == 0 then
		if #result > 0 then
			result = result .. "&nbsp;"
		end
		result = result .. seconds .. "&nbsp;s"
	end
	return result
end

return duration