convert seconds into day:hour:minute:seconds

Post Reply
manjh
Posts: 345
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

convert seconds into day:hour:minute:seconds

Post by manjh » Friday 12 October 2018 9:55

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: 494
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 » Friday 12 October 2018 10:32

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

User avatar
waaren
Posts: 1064
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Friday 12 October 2018 10:44

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

manjh
Posts: 345
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

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

Post by manjh » Friday 12 October 2018 11:21

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 2 guests