Module:Time: Difference between revisions

From Melvor Idle
(Remove unused functions)
No edit summary
Line 2: Line 2:


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


function p._secondsToHMS(totalSeconds)
function p._secondsToHMS(totalSeconds)
Line 14: Line 13:


function p._secondsToHuman(totalSeconds)
function p._secondsToHuman(totalSeconds)
ts = Number.secondsToTimeSpan(totalSeconds)
local timeSpan = TimeSpan.fromSeconds(totalSeconds)
if ts.totalSeconds < 60 then
return timeSpan:toStringLong()
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
end


Line 42: Line 21:
local args = frame:getParent().args
local args = frame:getParent().args
return p._secondsToHuman(args[0])
return p._secondsToHuman(args[0])
end
-- Creates a TimeSpan object from a total amount of seconds.
function p.secondsToTimeSpan(totalSeconds)
return p.actionTimeToTimeSpan(totalSeconds * 100)
end
--- Creates a TimeSpan object from action time (hundreds of a second).
-- @param str (number) The amount of action time to convert to a TimeSpan
-- @return (TimeSpan) A TimeSpan object containing the seconds, minutes, hours and days the input amount of action time amounts to.
function p.actionTimeToTimeSpan(totalActionTime)
    local days = math.floor(totalActionTime / 8640000)
    local remainder = totalActionTime % 8640000
    local hours = math.floor(remainder / 360000)
    remainder = remainder % 360000
    local minutes = math.floor(remainder / 6000)
    remainder = remainder % 6000
   
    local seconds = math.floor(remainder / 100)
    return {
        days = days,
        hours = hours,
        minutes = minutes,
        seconds = seconds,
       
        totalDays = totalActionTime / 8640000,
        totalHours = totalActionTime / 360000,
        totalMinutes = totalActionTime / 6000,
        totalSeconds = totalActionTime / 100
    }
end
end


return p
return p

Revision as of 20:32, 13 April 2024

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


local p = {}

local Number = require('Module:Number')

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 frame = frame or mw.getCurrentFrame()
	local args = frame:getParent().args
	return p._secondsToHuman(args[0])
end

-- Creates a TimeSpan object from a total amount of seconds.
function p.secondsToTimeSpan(totalSeconds)
	return p.actionTimeToTimeSpan(totalSeconds * 100)
end

--- Creates a TimeSpan object from action time (hundreds of a second).
-- @param str (number) The amount of action time to convert to a TimeSpan
-- @return (TimeSpan) A TimeSpan object containing the seconds, minutes, hours and days the input amount of action time amounts to.
function p.actionTimeToTimeSpan(totalActionTime)
    local days = math.floor(totalActionTime / 8640000)
    local remainder = totalActionTime % 8640000

    local hours = math.floor(remainder / 360000)
    remainder = remainder % 360000

    local minutes = math.floor(remainder / 6000)
    remainder = remainder % 6000
    
    local seconds = math.floor(remainder / 100)

    return {
        days = days,
        hours = hours,
        minutes = minutes,
        seconds = seconds,
        
        totalDays = totalActionTime / 8640000,
        totalHours = totalActionTime / 360000,
        totalMinutes = totalActionTime / 6000,
        totalSeconds = totalActionTime / 100
    }
end

return p