Module:Time: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:


local Number = require('Module:Number')
local Number = require('Module:Number')
local TimeSpan = require('Module:TimeSpan')


-- 123 => 00:02:03
function p._secondsToHMS(totalSeconds)
function p.secondsToHMS(totalSeconds)
local days = math.floor(totalSeconds / (24 * 60 * 60))
local days = math.floor(totalSeconds / (24 * 60 * 60))
if days >= 1 then
if days >= 1 then
Line 13: Line 13:
end
end


-- 123 => 2 minutes, 3 seconds
function p._secondsToHuman(totalSeconds)
function p.secondsToHuman(totalSeconds)
local timeSpan = TimeSpan.fromSeconds(totalSeconds)
ts = Number.secondsToTimeSpan(totalSeconds)
return timeSpan:toStringLong()
if ts.totalSeconds < 60 then
if math.floor(ts.totalSeconds) == 0 then
return string.format('0 seconds')
else
return string.format('%.1f seconds', ts.totalSeconds)
end
end
local output = {}
if ts.days > 0 then
table.insert(output, string.format('%d day%s', ts.days, (ts.days > 1) and "s" or ""))
end
if ts.hours > 0 then
table.insert(output, string.format('%d hour%s', ts.hours, (ts.hours > 1) and "s" or ""))
end
if ts.minutes > 0 then
table.insert(output, string.format('%d minute%s', ts.minutes, (ts.minutes > 1) and "s" or ""))
end
if ts.seconds > 0 then
table.insert(output, string.format('%d second%s', ts.seconds, (ts.seconds > 1) and "s" or ""))
end
return table.concat(output, ", ")
end
 
function p.testSecondsToHuman(s)
output = {}
for i, s in ipairs(
{-1, 0, 0.5, 5 * 60, 5 * 60 + 20,
6 * 60 * 60, 6 * 60 * 60 + 20, 6 * 60 * 60 + 3 * 60 + 20,
1 * 24 * 60 * 60, 2 * 24 * 60 * 60 + 3 * 60 * 60 + 2 * 60 + 1}) do
table.insert(output, string.format('%s = %s', s, p.secondsToHuman(s)))
end
return table.concat(output, ' || ')
end
end


function p.prettyPrintTime(frame)
function p.secondsToHuman(frame)
if frame == nil then
local args = frame.args ~= nil and frame.args or frame
return '<invalid frame object>'
return p._secondsToHuman(args[1])
end
if frame.args[1] == nil then
return '<missing argument seconds>'
end
local seconds = tonumber(frame.args[1])
if frame.args["hms"] == "true" then
return p.secondsToHMS(seconds)
else
return p.secondsToHuman(seconds)
end
end
end


return p
return p

Latest revision as of 20:52, 13 April 2024

Provides time helper functions that can be used elsewhere in the wiki


local p = {}

local Number = require('Module:Number')
local TimeSpan = require('Module:TimeSpan')

function p._secondsToHMS(totalSeconds)
	local days = math.floor(totalSeconds / (24 * 60 * 60))
	if days >= 1 then
		return string.format('%d day%s, ', days, (days > 1) and "s" or "") .. os.date("!%X", totalSeconds)
	else
		return os.date("!%X", totalSeconds)
	end
end

function p._secondsToHuman(totalSeconds)
	local timeSpan = TimeSpan.fromSeconds(totalSeconds)
	return timeSpan:toStringLong()
end

function p.secondsToHuman(frame)
	local args = frame.args ~= nil and frame.args or frame
	return p._secondsToHuman(args[1])
end

return p