Module:Duration: Difference between revisions
From Stationeers Community Wiki
More actions
Receive arguments correctly |
add handling for minutes/hours/days arguments |
||
| Line 2: | Line 2: | ||
function duration.fromSeconds(frame) | function duration.fromSeconds(frame) | ||
local seconds = tonumber(frame.args[1]) | local seconds = tonumber(frame.args[1]) or frame.args.Seconds | ||
if | if seconds and type(seconds) ~= "number" then | ||
return "<strong>Error in Module:Duration: argument | return "<strong>Error in Module:Duration: argument Seconds ('" .. seconds .. "') is not a number</strong>" | ||
end | end | ||
local minutes = frame.args.Minutes or 0 | |||
if minutes and type(minutes) ~= "number" then | |||
return "<strong>Error in Module:Duration: argument Minutes ('" .. minutes .. "') is not a number</strong>" | |||
end | |||
local hours = frame.args.Hours or 0 | |||
if hours and type(hours) ~= "number" then | |||
return "<strong>Error in Module:Duration: argument Hours ('" .. hours .. "') is not a number</strong>" | |||
end | |||
local days = frame.args.Days or 0 | |||
if days and type(days) ~= "number" 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 = "" | local result = "" | ||
if seconds >= 86400 then | |||
local days = math.floor(seconds / 8640000) | |||
seconds = seconds - days*3600 | |||
result = result .. days .. " d" | |||
end | |||
if seconds >= 3600 then | if seconds >= 3600 then | ||
local hours = math.floor(seconds / 3600) | local hours = math.floor(seconds / 3600) | ||
Revision as of 04:54, 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]) or frame.args.Seconds
if seconds and type(seconds) ~= "number" then
return "<strong>Error in Module:Duration: argument Seconds ('" .. seconds .. "') is not a number</strong>"
end
local minutes = frame.args.Minutes or 0
if minutes and type(minutes) ~= "number" then
return "<strong>Error in Module:Duration: argument Minutes ('" .. minutes .. "') is not a number</strong>"
end
local hours = frame.args.Hours or 0
if hours and type(hours) ~= "number" then
return "<strong>Error in Module:Duration: argument Hours ('" .. hours .. "') is not a number</strong>"
end
local days = frame.args.Days or 0
if days and type(days) ~= "number" 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 / 8640000)
seconds = seconds - days*3600
result = result .. days .. " d"
end
if seconds >= 3600 then
local hours = math.floor(seconds / 3600)
seconds = seconds - hours*3600
result = result .. hours .. " h"
end
if seconds >= 60 then
if #result > 0 then
result = result .. " "
end
local minutes = math.floor(seconds / 60)
seconds = seconds - minutes * 60
result = result .. minutes .. " min"
end
if seconds > 0 or #result == 0 then
if #result > 0 then
result = result .. " "
end
result = result .. seconds .. " s"
end
return result
end
return duration