General > Developers Corner

Server Uptime

(1/3) > >>

JamminR:
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: --- 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

--- End code ---

JamminR:
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.

JamminR:
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

monkeynutts:
So where to I place this code on my server to impliment it?

JamminR:

--- Quote from: monkeynutts on July 06, 2007, 05:12:40 PM ---So where to I place this code on my server to impliment it?

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version