[INC] Unix Time

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1090
Gender:
United States of America

[INC] Unix Time

Post by Archive-Poster »

Unix Time
.: Description

This include contains two functions that allow you to convert unix time to normal time and vice versa.

.: Commands
UnixToTime - This will convert unix time to normal time.
TimeToUnix - This will convert normal time to unix time.

.: Usage
UnixToTime( iUnixTime , &iYear , &iMonth , &iDay , &iHour , &iMinute , &iSecond , [ TimeZone ] )
iUnixTime - Unix time value to be converted to normal time.
iYear .. iSecond - Normal time values passed by-reference.
[ TimeZone ] - Optional parameter to obtain time values adjusted for a particular timezone. By default, UTC time is returned which has no timezone adjustment (same as what get_systime() returns). You can pass a timezone of your choice or use UT_TIMEZONE_SERVER to use the timezone of the server machine.
TimeToUnix( iYear , iMonth , iDay , iHour , iMinute , iSecond , [ TimeZone ] )
iYear .. iSecond - Normal time values to be converted to unix time.
[ TimeZone ] - Optional parameter to obtain time values adjusted for a particular timezone. By default, UTC time is returned which has no timezone adjustment (same as what get_systime() returns). You can pass a timezone of your choice or use UT_TIMEZONE_SERVER to use the timezone of the server machine.

Comments: This include can be useful for working with timestamps or any task involving time\date manipulation. When using a timezone offset, only use it on a UTC (0 offset) time value. Specifying a timezone on a time value that is already offsetted will provide inaccurate results.

.: Example Code

Code: Select all

public UnixTime()
{
    new iTime , iTimeAdjusted , iYear , iMonth , iDay , iHour , iMinute , iSecond;
    
    iTime = get_systime();
    
    //Display get_systime() value which is UTC time (no +/- adjustment for timezone)
    server_print( "get_systime() = %d" , iTime );
    UnixToTime( iTime , iYear , iMonth , iDay , iHour , iMinute , iSecond );
    server_print( "get_systime() Time = %02d/%02d/%d %02d:%02d:%02d" , iMonth , iDay , iYear , iHour , iMinute , iSecond );
    
    //Display time value adjusted with TimeToUnix()
    iTimeAdjusted = TimeToUnix( iYear , iMonth , iDay , iHour , iMinute , iSecond , UT_TIMEZONE_SERVER );
    UnixToTime( iTimeAdjusted , iYear , iMonth , iDay , iHour , iMinute , iSecond );
    server_print( "TimeToUnix Adjusted Time = %02d/%02d/%d %02d:%02d:%02d" , iMonth , iDay , iYear , iHour , iMinute , iSecond );
    
    //Display time value adjusted with UnixToTime()
    UnixToTime( iTime , iYear , iMonth , iDay , iHour , iMinute , iSecond , UT_TIMEZONE_SERVER );
    server_print( "UnixToTime Adjusted Time = %02d/%02d/%d %02d:%02d:%02d" , iMonth , iDay , iYear , iHour , iMinute , iSecond );
} 
Attachments
unixtime.inc.zip
(1.49 KiB) Downloaded 5 times

Who is online

Users browsing this forum: No registered users and 0 guests