convert seconds into day:hour:minute:seconds

Moderator: leecollings

Post Reply
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

convert seconds into day:hour:minute:seconds

Post 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?
Hans
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: convert seconds into day:hour:minute:seconds

Post 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
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: convert seconds into day:hour:minute:seconds

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: convert seconds into day:hour:minute:seconds

Post 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...
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest