zbizu
Legendary OT User
I needed a function which could do proper time formatting, but I couldn't even find any examples so I wrote this one myself.
It simply converts seconds to text which can be used as remaining time.
Do not post that script on other forums without my permission
examples:
showTimeLeft(868381, true) -> 10 days, 1 hour, 13 minutes and 1 second left.
showTimeLeft(868381, false) -> 10:01:13:01
function:
If you are using other distribution than TFS 1.0 replace :split to :explode
It simply converts seconds to text which can be used as remaining time.
Do not post that script on other forums without my permission
examples:
showTimeLeft(868381, true) -> 10 days, 1 hour, 13 minutes and 1 second left.
showTimeLeft(868381, false) -> 10:01:13:01
function:
If you are using other distribution than TFS 1.0 replace :split to :explode
Code:
function showTimeLeft(number, usewords)
local number = tonumber(number)
if not number then
return "error"
end
if number < 0 then
return "expired"
end
local clocknum = os.date("!%X",number):split(":") -- h:m:s
local day = math.modf(number / 86400)
local hour = clocknum[1]
local minute = clocknum[2]
local second = clocknum[3]
if not usewords then
return table.concat({day, hour, minute, second}, ":")
end
local text = {}
if day > 0 then
table.insert(text, tonumber(day) .. " day" .. (day > 1 and "s" or ""))
end
if hour ~= "00" then
table.insert(text, tonumber(hour) .. " hour" .. (tonumber(hour) > 1 and "s" or ""))
end
if minute ~= "00" then
table.insert(text, tonumber(minute) .. " minute" .. (tonumber(minute) > 1 and "s" or ""))
end
if second ~= "00" then
table.insert(text, tonumber(second) .. " second" .. (tonumber(second) > 1 and "s" or ""))
end
countdown_text = ""
if #text > 0 then
countdown_text = text[1]
for i = 2, #text - 1 do
countdown_text = countdown_text .. ", " .. text[i]
end
if #text > 1 then
countdown_text = countdown_text .. " and " .. text[#text]
end
countdown_text = countdown_text .. " left."
else
countdown_text = "expired"
end
return countdown_text
end
Last edited: