GetRealTime: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Revert)
Line 64: Line 64:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function showtime ()
function showtime ()
    local time = getRealTime()
local time = getRealTime()
    local hours = time.hour
local hours = time.hour
    local minutes = time.minute
local minutes = time.minute
    local seconds = time.second
local seconds = time.second
    -- Make sure to add a 0 to the front of single digits.
-- Make sure to add a 0 to the front of single digits.
    if (hours < 10) then
if (hours < 10) then
        hours = "0"..hours
hours = "0"..hours
    end
end
    if (minutes < 10) then
if (minutes < 10) then
        minutes = "0"..minutes
minutes = "0"..minutes
    end
end
    if (seconds < 10) then
if (seconds < 10) then
        seconds = "0"..seconds
seconds = "0"..seconds
    end
end
    outputChatBox ( "Local Time: "..hours..":"..minutes..":"..seconds )
outputChatBox ( "Local Time: "..hours..":"..minutes..":"..seconds )
end
end
addCommandHandler("showtime", showtime)
addCommandHandler("showtime", showtime)
Line 86: Line 86:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function showtime ()
function showtime ()
    local time = getRealTime()
local time = getRealTime()
    local hours = time.hour
local hours = time.hour
    local minutes = time.minute
local minutes = time.minute
    local seconds = time.second
local seconds = time.second


    local monthday = time.monthday
        local monthday = time.monthday
    local month = time.month
local month = time.month
    local year = time.year
local year = time.year


    local formattedTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", year + 1900, month + 1, monthday, hours, minutes, seconds)
        local formattedTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", year + 1900, month + 1, monthday, hours, minutes, seconds)
    outputChatBox ( "Local Time: ".. formattedTime )
outputChatBox ( "Local Time: ".. formattedTime )
end
end
addCommandHandler("showtime", showtime)
addCommandHandler("showtime", showtime)
</syntaxhighlight>
</syntaxhighlight>
==Example==
Time report from 2020
<syntaxhighlight lang="lua">
function func_get_timestamp_server()
    local second = getRealTime().timestamp
    local day = math.ceil(tonumber((second / 86400) - 18250))
    local hour = math.ceil(tonumber(day * 24))
    return day, hour
end
</syntaxhighlight>
By RIGZI


==Changelog==
==Changelog==

Revision as of 04:19, 18 April 2021

This function gets the server or client (if used client sided it returns time as set on client's computer) real time and returns it in a table. If you want to get the in-game time (shown on GTA's clock) use getTime.

Syntax

table getRealTime( [ int seconds = current, bool localTime = true ] )

Optional Arguments

  • seconds: A count in seconds from the year 1970. Useful for storing points in time, or for retrieving time information for getBanTime. The valid range of this argument is 0 to 32,000,000,000
  • localTime: Set to true to adjust for the locally set timezone.

Returns

Returns a table of substrings with different time format or false if the seconds argument is out of range.

Member Meaning Range
second seconds after the minute 0-61*
minute minutes after the hour 0-59
hour hours since midnight 0-23
monthday day of the month 1-31
month months since January 0-11
year years since 1900
weekday days since Sunday 0-6
yearday days since January 1 0-365
isdst Daylight Saving Time flag
timestamp seconds since 1970 (Ignoring set timezone)

* second is generally 0-59. Extra range to accommodate for leap seconds in certain systems.

Example

This example adds 'showtime' like the default MTA 'time' command:

function showtime ()
	local time = getRealTime()
	local hours = time.hour
	local minutes = time.minute
	local seconds = time.second
	-- Make sure to add a 0 to the front of single digits.
	if (hours < 10) then
		hours = "0"..hours
	end
	if (minutes < 10) then
		minutes = "0"..minutes
	end
	if (seconds < 10) then
		seconds = "0"..seconds
	end
	outputChatBox ( "Local Time: "..hours..":"..minutes..":"..seconds )
end
addCommandHandler("showtime", showtime)

Example with year, month, monthday using string.format:

function showtime ()
	local time = getRealTime()
	local hours = time.hour
	local minutes = time.minute
	local seconds = time.second

        local monthday = time.monthday
	local month = time.month
	local year = time.year

        local formattedTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", year + 1900, month + 1, monthday, hours, minutes, seconds)
	outputChatBox ( "Local Time: ".. formattedTime )
end
addCommandHandler("showtime", showtime)

Changelog

Version Description
1.4.0-9.06976 Added localTime argument

See Also