Module:TimeSpan: Difference between revisions

m
Small fix to toStringLong
(Add two toString functions)
m (Small fix to toStringLong)
 
(3 intermediate revisions by the same user not shown)
Line 40: Line 40:
end
end


function TimeSpan.FromMilliseconds(milliseconds)
function TimeSpan.fromMilliseconds(milliseconds)
     return TimeSpan.new(0, 0, 0, 0, (milliseconds or 0))
     return TimeSpan.new(0, 0, 0, 0, (milliseconds or 0))
end
end


function TimeSpan.FromSeconds(seconds)
function TimeSpan.fromSeconds(seconds)
     local totalMilliseconds = (seconds or 0) * MillisecondsPerSecond
     local totalMilliseconds = (seconds or 0) * MillisecondsPerSecond
     return TimeSpan.new(0, 0, 0, 0, totalMilliseconds)
     return TimeSpan.new(0, 0, 0, 0, totalMilliseconds)
Line 110: Line 110:


function TimeSpan:toStringLong()
function TimeSpan:toStringLong()
local sb = StringBuilder.new()
-- 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
 
-- Long path that outputs the entire TimeSpan
local output = {}
local days = self:getDays()
local days = self:getDays()
local hours = self:getHours()
local hours = self:getHours()
Line 116: Line 127:
local seconds = self:getSeconds()
local seconds = self:getSeconds()
local milliseconds = self:getMilliseconds()
local milliseconds = self:getMilliseconds()
 
local output = {}
if days > 0 then
if days > 0 then
table.insert(output, string.format('%d day%s', days, (days > 1) and "s" or ""))
table.insert(output, string.format('%d day%s', days, (days > 1) and "s" or ""))
Line 128: Line 138:
end
end
if seconds > 0 then
if seconds > 0 then
table.insert(output, string.format('%d second%s', seconds, (seconds > 1) and "s" or ""))
table.insert(output, string.format('%.2f second%s', seconds, (seconds > 1) and "s" or ""))
end
end
if milliseconds > 0 then
 
table.insert(output, string.format('%d second%s', milliseconds, (milliseconds > 1) and "s" or ""))
end
return table.concat(output, ", ")
return table.concat(output, ", ")
end
end
1,047

edits