GetRealTime: Difference between revisions
(New page: __NOTOC__ {{Server client function}} This function gets the real server time and returns it in a table ==Syntax== <syntaxhighlight lang="lua">table getCTime()</syntaxhighlight> ===Returns=== Returns a ''table'' o...) |
(format the string to two digits with a proper programming standard) |
||
(29 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | {{Server client function}} | ||
This function gets the real | 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== | ==Syntax== | ||
<syntaxhighlight lang="lua">table | <syntaxhighlight lang="lua">table getRealTime( [ int seconds = current, bool localTime = true ] )</syntaxhighlight> | ||
===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 | |||
{{New feature/item|3.0141|1.4.1|6976| | |||
*'''localTime:''' Set to ''true'' to adjust for the locally set timezone. | |||
}} | |||
===Returns=== | ===Returns=== | ||
Returns a ''table'' of substrings with different time format | Returns a ''table'' of substrings with different time format or ''false'' if the '''seconds''' argument is out of range. | ||
{| border="2" cellpadding="2" cellspacing="0" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;" | {| border="2" cellpadding="2" cellspacing="0" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;" | ||
Line 14: | Line 20: | ||
|'''Range''' | |'''Range''' | ||
|- | |- | ||
| | |second | ||
|seconds after the minute | |seconds after the minute | ||
|0-61* | |0-61* | ||
|- | |- | ||
| | |minute | ||
|minutes after the hour | |minutes after the hour | ||
|0-59 | |0-59 | ||
|- | |- | ||
| | |hour | ||
|hours since midnight | |hours since midnight | ||
|0-23 | |0-23 | ||
|- | |- | ||
| | |monthday | ||
|day of the month | |day of the month | ||
|1-31 | |1-31 | ||
|- | |- | ||
| | |month | ||
|months since January | |months since January | ||
|0-11 | |0-11 | ||
|- | |- | ||
| | |year | ||
|years since 1900 | |years since 1900 | ||
|- | |- | ||
| | |weekday | ||
|days since Sunday | |days since Sunday | ||
|0-6 | |0-6 | ||
|- | |- | ||
| | |yearday | ||
|days since January 1 | |days since January 1 | ||
|0-365 | |0-365 | ||
|- | |- | ||
| | |isdst | ||
|Daylight Saving Time flag | |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. | ||
==Remarks== | |||
The '''seconds''' parameter can be left out entirely while still using the '''localTime''' parameter. To achieve that simply pass the boolean localTime parameter as first argument where you would otherwise pass the '''seconds''' parameter. This way you can retrieve a current timepoint that is not denoted in local time. | |||
==Example== | ==Example== | ||
This example adds 'showtime' like the default MTA 'time' command: | |||
This example | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function showtime () | function showtime () | ||
local time = getRealTime() | |||
local hours = time.hour | |||
local minutes = time.minute | |||
local seconds = time.second | |||
-- use string.format to keep it 2 digits. eg 1 will be converted to 01 | |||
outputChatBox ( string.format("Local Time: %02d:%02d:%02d", hours, minutes, seconds) ) | |||
end | end | ||
addCommandHandler("showtime", showtime) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</ | |||
Example with year, month, monthday using string.format: | |||
<syntaxhighlight lang="lua"> | |||
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) | |||
</syntaxhighlight> | |||
==Changelog== | |||
{{ChangelogHeader}} | |||
{{ChangelogItem|1.4.0-9.06976|Added localTime argument}} | |||
==See Also== | ==See Also== | ||
{{Utility functions}} | {{Utility functions}} | ||
[[ru:getRealTime]] |
Latest revision as of 22:36, 26 August 2022
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.
Remarks
The seconds parameter can be left out entirely while still using the localTime parameter. To achieve that simply pass the boolean localTime parameter as first argument where you would otherwise pass the seconds parameter. This way you can retrieve a current timepoint that is not denoted in local time.
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 -- use string.format to keep it 2 digits. eg 1 will be converted to 01 outputChatBox ( string.format("Local Time: %02d:%02d:%02d", 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
- addDebugHook
- base64Decode
- base64Encode
- debugSleep
- decodeString
- encodeString
- fromJSON
- generateKeyPair
- getColorFromString
- getDevelopmentMode
- getDistanceBetweenPoints2D
- getDistanceBetweenPoints3D
- getEasingValue
- getNetworkStats
- getNetworkUsageData
- getPerformanceStats
- getRealTime
- getTickCount
- getTimerDetails
- getTimers
- getFPSLimit
- getUserdataType
- getVersion
- gettok
- isTransferBoxVisible
- setTransferBoxVisible
- hash
- inspect
- interpolateBetween
- iprint
- isOOPEnabled
- isTimer
- killTimer
- md5
- passwordHash
- passwordVerify
- pregFind
- pregMatch
- pregReplace
- removeDebugHook
- resetTimer
- setDevelopmentMode
- setFPSLimit
- setTimer
- ref
- deref
- sha256
- split
- teaDecode
- teaEncode
- toJSON
- tocolor
- getProcessMemoryStats
- utfChar
- utfCode
- utfLen
- utfSeek
- utfSub