Module:Time: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- 123 => 00:02:03
local Number = require('Module:Number')
function p.secondsToHMS(s)
local TimeSpan = require('Module:TimeSpan')
local ONE_DAY = 24 * 60 * 60
 
if s > ONE_DAY then
function p._secondsToHMS(totalSeconds)
return string.format('%d day%s, ', s/ONE_DAY, (math.floor(s/ONE_DAY) > 1) and "s" or "") .. os.date("!%X", s)
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
else
return os.date("!%X", s)
return os.date("!%X", totalSeconds)
end
end
end
end


-- 123 => 2 minutes, 3 seconds
function p._secondsToHuman(totalSeconds)
function p.secondsToHuman(s)
local timeSpan = TimeSpan.fromSeconds(totalSeconds)
if s < 60 then
return timeSpan:toStringLong()
if s > 0 or s < 0 then
return string.format('%.1f seconds', s)
else
return string.format('0 seconds', s)
end
end
local days = math.floor(s / (24 * 60 * 60))
local hours = math.floor(s / (60 * 60) % (24))
local minutes = math.floor(s / 60 % 60)
local seconds = math.floor(s % (60))
local output = {}
if days > 0 then
table.insert(output, string.format('%d day%s', days, (days > 1) and "s" or ""))
end
if hours > 0 then
table.insert(output, string.format('%d hour%s', hours, (hours > 1) and "s" or ""))
end
if minutes > 0 then
table.insert(output, string.format('%d minute%s', minutes, (minutes > 1) and "s" or ""))
end
if seconds > 0 then
table.insert(output, string.format('%d second%s', seconds, (seconds > 1) and "s" or ""))
end
return table.concat(output, ", ")
end
end


function p.testSecondsToHuman(s)
function p.secondsToHuman(frame)
output = {}
local args = frame.args ~= nil and frame.args or frame
for i, s in ipairs(
return p._secondsToHuman(args[1])
{-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)
return p
if frame == nil then
return '<invalid frame object>'
end
if frame.args[1] == nil then
return '<missing argument seconds>'
end
local seconds = tonumber(frame.args[1])
if frame.args["hms"] ~= "" then
return p.secondsToHMS(seconds)
else
return p.secondsToHuman(seconds)
end
end

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