GetTimestamp: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Useful Function: GetTimestamp)
 
Line 17: Line 17:
Returns an UNIX timestamp.
Returns an UNIX timestamp.


<section name="Serverside Script" class="server" show="true">
==Code==
==Code==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 60: Line 61:
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>


===Required Functions===
===Required Functions===

Revision as of 15:22, 13 May 2009

With this function you can get the UNIX timestamp.

Syntax

int GetTimestamp( [ int year, int month, int day, int hour, int minute, int second ] )

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • year: The year of the date you want to retrieve the UNIX timestamp of. Default is your local year.
  • month: The month of the date you want to retrieve the UNIX timestamp of. Default is your local month.
  • day: The day of the date you want to retrieve the UNIX timestamp of. Default is your local day.
  • hour: The hour of the date you want to retrieve the UNIX timestamp of. Default is your local hour.
  • minute: The minute of the date you want to retrieve the UNIX timestamp of. Default is your local minute.
  • second: The second of the date you want to retrieve the UNIX timestamp of. Default is your local second.

Returns

Returns an UNIX timestamp.

Click to collapse [-]
Serverside Script

Code

function GetTimestamp(year, month, day, hour, minute, second)
    local i
    local timestamp = 0
    local time = getRealTime()
    local monthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    
    if (not year or year < 1970) then
        year = time.year + 1900
        month = time.month + 1
        day = time.monthday
        hour = time.hour
        minute = time.minute
        second = time.second
    else
        month = month or 1
        day = day or 1
        hour = hour or 0
        minute = minute or 0
        second = second or 0
    end
    
    for i=1970, year-1, 1 do
        timestamp = timestamp + 60*60*24*365
        if (IsYearALeapYear(i)) then
            timestamp = timestamp + 60*60*24
        end
    end
    
    if (IsYearALeapYear(year)) then
        monthDays[2] = monthDays[2] + 1
    end
    
    for i=1, month-1, 1 do
        timestamp = timestamp + 60*60*24*monthDays[i]
    end
    
    timestamp = timestamp + 60*60*24 * (day - 1) + 60*60 * hour + 60 * minute + second
    
    return timestamp
end

Required Functions

NOTE: This function requires function [[{{{1}}}]] in order to work correctly.

IsYearALeapYear

Example

Click to collapse [-]
Server

This example saves the time of the player's joining.

-- get the root element
local _root = getRootElement()
-- define the onPlayerJoin handler function
function OnPlayerJoin()
    -- get the actual UNIX timestamp
    local datetime = getTimestamp()
    -- attach the time to the player's element
    setElementData(source, "joinTime", datetime)
end
-- add the event handler
addEventHandler("onPlayerJoin", _root, OnPlayerJoin)

Author: NeonBlack