AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INC] Unix Time (https://forums.alliedmods.net/showthread.php?t=91915)

Bugsy 05-07-2009 11:21

[INC] Unix Time
 
1 Attachment(s)
Unix Time
.: Description

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

Unix time on wiki: http://en.wikipedia.org/wiki/Unix_time
Timezone info: http://www.epochconverter.com/epoch/timezones.php

.: 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
PHP Code:

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 );
    
UnixToTimeiTime 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 TimeToUnixiYear iMonth iDay iHour iMinute iSecond UT_TIMEZONE_SERVER );
    
UnixToTimeiTimeAdjusted 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()
    
UnixToTimeiTime 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 );


.: Changelog
  • v0.3
    • Added optional parameter for specifying a timezone.
  • v0.2
    • Added reset to 0 of hour variable in UnixToTime function. This was only causing an issue if the hour variable was holding a value when passed into UnixToTime (since it is passed by reference). [thx stupok]
  • v0.1
    • Initial release

Hawk552 05-09-2009 13:23

Re: [INC] Unix Time Conversion
 
1 Attachment(s)
I wrote up a header file for AlliedModders time. I hope you like it.

Exolent[jNr] 05-09-2009 13:24

Re: [INC] Unix Time Conversion
 
Quote:

Originally Posted by Hawk552 (Post 824232)
I wrote up a header file for AlliedModders time. I hope you like it.

Good job!

xPaw 05-09-2009 16:42

Re: [INC] Unix Time Conversion
 
HAHA Valve like always is right :mrgreen:

Bugsy 05-09-2009 21:02

Re: [INC] Unix Time Conversion
 
haha nice job chief :wink:

stupok 05-12-2009 21:56

Re: [INC] Unix Time Conversion
 
tyvm for this :mrgreen:, you saved me some work

EDIT:
ARGH >_< I found a bug

I'm going to try to squash it, here are the details:

Code:

        new iYear = 0
        new iMonth = 0
        new iDay = 0
        new iHour = 0
        new iMinute = 0
        new iSecond = 0
        new iUnixTime = 123456789
       
        UnixToTime( iUnixTime, iYear, iMonth, iDay, iHour, iMinute, iSecond )
       
        server_print("UnixToTime:(%i)->(%i.%i.%i %i:%i:%i)", iUnixTime, iYear, iMonth, iDay, iHour, iMinute, iSecond )
       
        iUnixTime = TimeToUnix( iYear, iMonth, iDay, iHour, iMinute, iSecond )
       
        server_print("TimeToUnix:(%i.%i.%i %i:%i:%i)->(%i)", iYear, iMonth, iDay, iHour, iMinute, iSecond, iUnixTime )
       
        iYear++; iMonth++; iDay++; iHour++; iMinute++; iSecond++
       
        iUnixTime = TimeToUnix( iYear, iMonth, iDay, iHour, iMinute, iSecond )
       
        server_print("TimeToUnix:(%i.%i.%i %i:%i:%i)->(%i)", iYear, iMonth, iDay, iHour, iMinute, iSecond, iUnixTime )
       
        UnixToTime( iUnixTime, iYear, iMonth, iDay, iHour, iMinute, iSecond )
       
        server_print("UnixToTime:(%i)->(%i.%i.%i %i:%i:%i)", iUnixTime, iYear, iMonth, iDay, iHour, iMinute, iSecond )

Output:

Code:

UnixToTime:(123456789)->(1973.11.29 21:33:9)
TimeToUnix:(1973.11.29 21:33:9)->(123456789)
TimeToUnix:(1974.12.30 22:34:10)->(157674850)
UnixToTime:(157674850)->(1974.12.30 44:34:10)

Basically, the hours are being incremented instead of being replaced.

The fix:

Add the highlighted line.

Code:
stock UnixToTime( iTimeStamp , &iYear , &iMonth , &iDay , &iHour , &iMinute , &iSecond ) {     new iTemp;         iYear = 1970;     iMonth = 1;     iDay = 1;     iHour = 0; //...

Bugsy 05-13-2009 18:44

Re: [INC] Unix Time Conversion
 
When I was coding it I was always using a fresh variable passed only once so I never ran into that bug.

Thanks, chief boss buddy champ

I would +k you but I need to spread some out first

TheRadiance 06-01-2009 09:19

Re: [INC] Unix Time Conversion
 
Bugsy, awesome!!! I like it, Thanks!

Bugsy 09-24-2011 16:34

Re: [INC] Unix Time Conversion
 
New revision posted, read description for details.

BrUn3S 07-05-2012 13:31

Re: [INC] Unix Time
 
best!!! :)
thx


All times are GMT -4. The time now is 03:08.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.