GetTimeSeconds: Difference between revisions

From Onset Developer Wiki
m (GetTimeSeconds edits)
mNo 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 game started up}}
{{FuncDescription|Returns the amount of time since the 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 game strarted}}
{{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__
__EDIT_ME__

Revision as of 00:29, 25 January 2020

GetTimeSeconds

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

Description

Returns the amount of time since the 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

__EDIT_ME__