Module:TimeSpan: Difference between revisions

m
Small fix to toStringLong
(First version)
 
m (Small fix to toStringLong)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local TimeSpan = {}
local TimeSpan = {}
local StringBuilder = require('Module:StringBuilder')


-- Metatable to make instances of TimeSpan readonly
-- Metatable to make instances of TimeSpan readonly
Line 36: Line 38:
     setmetatable(self, TimeSpan_mt)
     setmetatable(self, TimeSpan_mt)
     return self
     return self
end
function TimeSpan.fromMilliseconds(milliseconds)
    return TimeSpan.new(0, 0, 0, 0, (milliseconds or 0))
end
function TimeSpan.fromSeconds(seconds)
    local totalMilliseconds = (seconds or 0) * MillisecondsPerSecond
    return TimeSpan.new(0, 0, 0, 0, totalMilliseconds)
end
end


Line 79: Line 90:


function TimeSpan:toString()
function TimeSpan:toString()
local sb = StringBuilder.new()
local days = self:getDays()
local hours = self:getHours()
local minutes = self:getMinutes()
local seconds = self:getSeconds()


     local milliseconds = totalMilliseconds % 1000
     if days > 0 then
     totalMilliseconds = (totalMilliseconds - milliseconds) / 1000
        sb:append(string.format("%d.", days))
    end
   
    sb:append(string.format("%02d:%02d:%02d", hours, minutes, seconds))
   
     if milliseconds > 0 then
        sb:append(string.format(".%03d", milliseconds))
    end


     local seconds = totalMilliseconds % 60
     return sb:toString()
    totalMilliseconds = (totalMilliseconds - seconds) / 60
end


    local minutes = totalMilliseconds % 60
function TimeSpan:toStringLong()
    totalMilliseconds = (totalMilliseconds - minutes) / 60
-- Fast path if there's less than a minute's worth of time.
local totalSeconds = self:getTotalSeconds()
if totalSeconds < 60 then
if math.floor(totalSeconds) == 0 then
return string.format('0 seconds')
end
-- Special case to show actiontime related numbers.
return string.format('%.2f seconds', totalSeconds)
end


    local hours = totalMilliseconds % 24
-- Long path that outputs the entire TimeSpan
    local days = (totalMilliseconds - hours) / 24
local output = {}
local days = self:getDays()
local hours = self:getHours()
local minutes = self:getMinutes()
local seconds = self:getSeconds()
local milliseconds = self:getMilliseconds()


    local parts = {}
if days > 0 then
    if days > 0 then
table.insert(output, string.format('%d day%s', days, (days > 1) and "s" or ""))
        table.insert(parts, formatTime(days, "."))
end
    end
if hours > 0 then
    table.insert(parts, formatTime(hours, ":"))
table.insert(output, string.format('%d hour%s', hours, (hours > 1) and "s" or ""))
    table.insert(parts, formatTime(minutes, ":"))
end
    table.insert(parts, formatTime(seconds, ""))
if minutes > 0 then
    if milliseconds > 0 then
table.insert(output, string.format('%d minute%s', minutes, (minutes > 1) and "s" or ""))
        table.insert(parts, "." .. string.format("%03d", milliseconds))
end
    end
if seconds > 0 then
table.insert(output, string.format('%.2f second%s', seconds, (seconds > 1) and "s" or ""))
end


    return table.concat(parts)
return table.concat(output, ", ")
end
end


1,047

edits