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
RA2lover (talk | contribs)
mNo edit summary
RA2lover (talk | contribs)
m add handling for strings coerceable into numbers
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:


function duration.fromSeconds(frame)
function duration.fromSeconds(frame)
local seconds = tonumber(frame.args[1]) or frame.args.Seconds or 0
frame = frame:getParent()
if seconds and type(seconds) ~= "number" then
local seconds = frame.args.Seconds or tonumber(frame.args[1]) or frame.args.seconds or 0
if seconds and (type(seconds) ~= "number" and not tonumber(seconds)) then
return "<strong>Error in Module:Duration: argument Seconds ('" .. seconds .. "') is not a number</strong>"
return "<strong>Error in Module:Duration: argument Seconds ('" .. seconds .. "') is not a number</strong>"
end
end
      
      
local minutes = frame.args.Minutes or 0
local minutes = frame.args.Minutes or frame.args.minutes or 0
if minutes and type(minutes) ~= "number" then
if minutes and (type(minutes) ~= "number" and not tonumber(minutes)) then
return "<strong>Error in Module:Duration: argument Minutes ('" .. minutes .. "') is not a number</strong>"
return "<strong>Error in Module:Duration: argument Minutes ('" .. minutes .. "') is not a number</strong>"
end
end


local hours = frame.args.Hours or 0
local hours = frame.args.Hours or frame.args.hours or 0
if hours and type(hours) ~= "number" then
if hours and (type(hours) ~= "number" and not tonumber(hours)) then
return "<strong>Error in Module:Duration: argument Hours ('" .. hours .. "') is not a number</strong>"
return "<strong>Error in Module:Duration: argument Hours ('" .. hours .. "') is not a number</strong>"
end
end


local days = frame.args.Days or 0
local days = frame.args.Days or frame.args.days or 0
if days and type(days) ~= "number" then
if days and (type(days) ~= "number" and not tonumber(days)) then
return "<strong>Error in Module:Duration: argument Days ('" .. days .. "') is not a number</strong>"
return "<strong>Error in Module:Duration: argument Days ('" .. days .. "') is not a number</strong>"
end
end

Latest revision as of 05:09, 19 March 2026

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

local duration = {}

function duration.fromSeconds(frame)
	frame = frame:getParent()
	local seconds = frame.args.Seconds or tonumber(frame.args[1]) or frame.args.seconds or 0
	if seconds and (type(seconds) ~= "number" and not tonumber(seconds)) then
		return "<strong>Error in Module:Duration: argument Seconds ('" .. seconds .. "') is not a number</strong>"
	end
    
	local minutes = frame.args.Minutes or frame.args.minutes or 0
	if minutes and (type(minutes) ~= "number" and not tonumber(minutes)) then
		return "<strong>Error in Module:Duration: argument Minutes ('" .. minutes .. "') is not a number</strong>"
	end

	local hours = frame.args.Hours or frame.args.hours or 0
	if hours and (type(hours) ~= "number" and not tonumber(hours)) then
		return "<strong>Error in Module:Duration: argument Hours ('" .. hours .. "') is not a number</strong>"
	end

	local days = frame.args.Days or frame.args.days or 0
	if days and (type(days) ~= "number" and not tonumber(days)) then
		return "<strong>Error in Module:Duration: argument Days ('" .. days .. "') is not a number</strong>"
	end

	seconds = seconds + (minutes*60) + (hours*3600) + (days*86400)

	local result = ""
	if seconds >= 86400 then
		local days = math.floor(seconds / 86400)
		seconds = seconds - days*86400
		result = result .. days .. "&nbsp;d"
	end
	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