GetTimeSeconds: Difference between revisions
From Onset Developer Wiki
m GetTimeSeconds edits |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Info|Function|Server & Client|1.0}} | {{Info|Function|Server & Client|1.0}} | ||
{{FuncDescription|Returns the amount of time since the game started up}} | {{FuncDescription|Returns the amount of time since the game or server started up.}} | ||
{{FuncSyntax|GetTimeSeconds()}} | {{FuncSyntax|GetTimeSeconds()}} | ||
Line 8: | Line 8: | ||
{{FuncNoParam}} | {{FuncNoParam}} | ||
{{FuncReturnValue|Returns a float value of the amount of time passed since the | {{FuncReturnValue|Returns a float value of the amount of time passed since the server started.}} | ||
== Example == | == Example == | ||
<syntaxhighlight lang="Lua> | |||
AddCommand("uptime", function(player) | |||
if GetTimeSeconds() > 3600 then | |||
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." hours.") | |||
elseif GetTimeSeconds() > 60 then | |||
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." minutes.") | |||
else | |||
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." seconds.") | |||
end | |||
end) | |||
function FormatTime(seconds) | |||
local seconds = tonumber(seconds) | |||
if seconds <= 0 then | |||
return "00:00:00" | |||
else | |||
hours = string.format("%02.f", math.floor(seconds / 3600)); | |||
mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60))); | |||
secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60)); | |||
return hours..":"..mins..":"..secs | |||
end | |||
end | |||
</syntaxhighlight> | |||
{{RelatedFunctions}} | {{RelatedFunctions}} | ||
*[[GetTimeSeconds]] | |||
*[[GetDeltaSeconds]] | |||
*[[GetTickCount]] |
Latest revision as of 09:25, 14 April 2021
Description
Returns the amount of time since the game or server started up.
Syntax
GetTimeSeconds()
Parameters
- This function has no parameters.
Return Value
- Returns a float value of the amount of time passed since the server started.
Example
AddCommand("uptime", function(player)
if GetTimeSeconds() > 3600 then
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." hours.")
elseif GetTimeSeconds() > 60 then
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." minutes.")
else
AddPlayerChat(player, "The server has been up for "..FormatTime(GetTimeSeconds()).." seconds.")
end
end)
function FormatTime(seconds)
local seconds = tonumber(seconds)
if seconds <= 0 then
return "00:00:00"
else
hours = string.format("%02.f", math.floor(seconds / 3600));
mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60)));
secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60));
return hours..":"..mins..":"..secs
end
end