Page 1 of 1
convert seconds into day:hour:minute:seconds
Posted: Friday 12 October 2018 9:55
by manjh
Before I start doing this in separate calculations, is there a simple function?
I have a numeric value (uptime in seconds), which I would like to convert into a presentation that is more human-friendly.... such as for example "2 days, 5 hours, 10 minutes, 5 seconds".
Is there a clever function for this?
Re: convert seconds into day:hour:minute:seconds
Posted: Friday 12 October 2018 10:32
by ben53252642
You would have found many answers just by searching the forum...
Code: Select all
-- Device Last Updates
t1 = os.time()
devices = {
"Living Room Left Blind",
"Main Bedroom Entrance Motion",
"Hours in Bed",
"Main Hallway Motion",
"Living Room Motion",
"Office Entrance Motion",
"Main Bedroom",
"Bedroom ZRC90 Curtain Button",
"Bedroom ZRC90 Blind Button",
"Bedroom ZRC90 Cat Button",
"Bedroom ZRC90 Radio Button",
"Bedroom ZRC90 Combo Button",
"Main Bedroom Blinds",
"Main Bedroom Curtains"
}
for i = 1, #devices do
s = otherdevices_lastupdate[devices[i]]
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
str = (devices[i] .. "LastUpdate")
str = str:gsub("%s+", "")
str = string.gsub(str, "%s+", "")
_G[str] = (os.difftime (t1, t2))
end
commandArray = {}
If / control statements go here
return commandArray
Edit as needed
Re: convert seconds into day:hour:minute:seconds
Posted: Friday 12 October 2018 10:44
by waaren
And another example (also one of the first hits on Google)
Code: Select all
local function SecondsToClock(seconds)
local days = math.floor(seconds / 86400)
seconds = seconds - days * 86400
local hours = math.floor(seconds / 3600 )
seconds = seconds - hours * 3600
local minutes = math.floor(seconds / 60)
seconds = seconds - minutes * 60
return string.format("%d days, %d hours, %d minutes, %d seconds.",days,hours,minutes,seconds)
end
Re: convert seconds into day:hour:minute:seconds
Posted: Friday 12 October 2018 11:21
by manjh
Ah, I'll try that. Thanks.
As for searches: the results are depending on the search arguments. I guess I just used the wrong words...