aboutsummaryrefslogtreecommitdiff
path: root/uptime.lua
blob: 8254f4cf121575926eadb059da21b9c86af537fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
----------------------------------------------------------
-- Licensed under the GNU General Public License version 2
--  * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
----------------------------------------------------------

-- {{{ Grab environment
local tonumber = tonumber
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
-- }}}


-- Uptime: provides system uptime information
module("vicious.uptime")


-- {{{ Uptime widget type
function worker(format)
    -- Get /proc/uptime
    local f = io.open("/proc/uptime")
    local line = f:read("*line")
    f:close()

    -- Format data
    local total_uptime   = math.floor(tonumber(line:match("[%d%.]+")))

    local uptime_days    = math.floor(total_uptime   / (3600 * 24))
    local uptime_hours   = math.floor((total_uptime  % (3600 * 24)) / 3600)
    local uptime_minutes = math.floor(((total_uptime % (3600 * 24)) % 3600) / 60)
    local uptime_seconds = math.floor(((total_uptime % (3600 * 24)) % 3600) % 60)

    return {total_uptime, uptime_days, uptime_hours, uptime_minutes, uptime_seconds}
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })