Ulysses

General => Developers Corner => Topic started by: JamminR on January 20, 2007, 04:56:32 PM

Title: Server Uptime
Post by: JamminR on January 20, 2007, 04:56:32 PM
At first, I thought an 'uptime' for my server would be easy. Using Garry's Mod v10 lua command string.FormattedTime (found in his lua\extensions\string.lua)
However, I found it had two things wrong.
1) First and foremost, it displays the wrong time after more than 24 hours.
2) It didn't display days. I'd prefer days instead of milliseconds.

So, I wrote my own. Feel free to use this or any derivative of this in any code you wish.
Rewrite it if you wish to be able to pass your own time to it. It wouldn't be difficult to make it exactly like what string.FormattedTime was supposed to be.
As it is, it will use the time the server has been running.

Passing the function "full" will result in "# weeks, # days, # hours, # minutes, # seconds". "short" (or any other word) will result in "0#:0#:0#:0#:0# (w:d:h:m:s)"

Code: [Select]
function Umotd_GetUptime(fmt) -- Credit to Garry. Idea from lua\extensions\string.lua "FormattedTime" function. Though, his math ends up wrong at times.
          local U_time = math.floor(os.clock{})
          local w, d, h, m, s = 0,0,0,0,0
                w = math.floor(U_time/604800)
                U_time = U_time - (w*604800)
                d = math.floor(U_time/86400)
                U_time = U_time - (d*86400)
                h = math.floor( U_time/3600 )
                U_time = U_time - (h*3600)
                m = math.floor( U_time/60 )
                U_time = U_time - (m*60)
                s = U_time
          if fmt ~= "full" then return string.format("%02i:%02i:%02i:%02i:%02i",w,d,h,m,s) .. " (wk:d:hr:min:sec)"
          else return w.. " weeks, " ..d .. " days, " .. h .. " hours, " .. m .. " minutes, " .. s .. " seconds."
          end
 end
Title: Re: Server Uptime
Post by: JamminR on January 20, 2007, 07:59:05 PM
Seems even though I tested various times, including more than 24 hours, when I actually run it on my server, after more than one day, even my math goes bad.
Title: Re: Server Uptime
Post by: JamminR on January 21, 2007, 06:20:26 AM
There. Fixed it. Much simpler too. Why I didn't think of this originally, I have no idea.
So simple, i threw in weeks. A weeks is 7 days. No leap year calculations. Just 7 days.
If you want months, you'll have to figure out the code yourself and add it. I didn't want to figure out leap years, which month, etc. :D
Title: Re: Server Uptime
Post by: monkeynutts on July 06, 2007, 05:12:40 PM
So where to I place this code on my server to impliment it?
Title: Re: Server Uptime
Post by: JamminR on July 06, 2007, 08:17:50 PM
So where to I place this code on my server to impliment it?

You could place it in lua/autorun as any_filename_you_wanted.lua along with the extra code needed to output text to somewhere.
In its current state, it is only a function that could be used in other code, prints no output, and is not a full release.
Title: Re: Server Uptime
Post by: Bryantdl7 on August 24, 2014, 11:04:42 AM
I feel like an idiot bumping a 7 yr old post... Does this still work? I am trying to find some sort of addon to monitor my server's uptime better than the cvar "stats"

I have a feeling it doesn't but it can't hurt to ask.
Title: Re: Server Uptime
Post by: Stickly Man! on August 24, 2014, 02:12:31 PM
Oddly enough, I don't see why it wouldn't work- most of the code used here is basic lua functions- nothing strictly Garrysmod related. Try it and see! :P

Edit: Holy crap, has it been 7 years?!?
Title: Re: Server Uptime
Post by: MrPresident on August 24, 2014, 03:22:58 PM
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO
Title: Re: Server Uptime
Post by: Cobalt on August 24, 2014, 06:20:35 PM
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO
I would use SysTime instead of CurTime because it isn't affected by game events such as the timescale changing.
Title: Re: Server Uptime
Post by: MrPresident on August 24, 2014, 07:05:39 PM
Good call, never affected me because we don't mess with the timescale, but some people might.
Title: Re: Server Uptime
Post by: JamminR on August 25, 2014, 01:43:36 PM
For once, this is one zombie thread I find impressive. Like a good wine, better with age, and improved by the community.
Title: Re: Server Uptime
Post by: Bryantdl7 on October 28, 2015, 12:31:23 PM
Code: [Select]
concommand.Add('uptime', function(ply)
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)

This only works from the server console, but it should be easy enough to modify it to work for players in game. The check is already there. Just add code where it says -- TODO


One year, 2.8 months later; does this look right? 

Code: [Select]
local uptime = ulx.command( "Utility", "ulx uptime", ulx.uptime, "!uptime" )
    local str = string.format(
        '%d:%02d:%02d',
        math.floor(CurTime() / 3600),
        math.floor(CurTime() % 3600 / 60),
        math.floor(CurTime() % 3600 % 60)
    )

    if IsValid( ply ) then
ulx.fancyLogAdmin( calling_ply, "The Server uptime is"..str)
-- TODO
    else
        print('Uptime: '..str)
    end
end)
    if IsValid( ply ) then
        -- TODO
    else
        print('Uptime: '..str)
    end
end)