GetTimeSeconds: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Info|Function|Server & Client|1.0}}
{{Info|Function|Server & Client|1.0}}


{{FuncDescription|__EDIT_ME__}}
{{FuncDescription|Returns the amount of time since the game or server started up.}}


{{FuncSyntax|GetTimeSeconds()}}
{{FuncSyntax|GetTimeSeconds()}}
Line 8: Line 8:
{{FuncNoParam}}
{{FuncNoParam}}


{{FuncReturnValue|__EDIT_ME__}}
{{FuncReturnValue|Returns a float value of the amount of time passed since the server started.}}


== Example ==
== Example ==
__EDIT_ME__
<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}}
__EDIT_ME__
*[[GetTimeSeconds]]
*[[GetDeltaSeconds]]
*[[GetTickCount]]

Latest revision as of 09:25, 14 April 2021

GetTimeSeconds

Type: Function
Context: Server & Client
Introduced: v1.0

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

See also