GetTimeSeconds: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
 
Line 1: Line 1:
{{Info|Function|Server & Client|1.0}}
{{Info|Function|Server & Client|1.0}}


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


{{FuncSyntax|GetTimeSeconds()}}
{{FuncSyntax|GetTimeSeconds()}}

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