Raised This Month: $ Target: $400
 0% 

[INC] Unix Time


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-07-2009 , 11:21   [INC] Unix Time
Reply With Quote #1

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
Attached Files
File Type: inc unixtime.inc (4.6 KB, 5686 views)
__________________

Last edited by Bugsy; 09-24-2011 at 16:34.
Bugsy is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 05-09-2009 , 13:23   Re: [INC] Unix Time Conversion
Reply With Quote #2

I wrote up a header file for AlliedModders time. I hope you like it.
Attached Files
File Type: inc amtime.inc (432 Bytes, 1033 views)
__________________

Last edited by Hawk552; 05-09-2009 at 13:27.
Hawk552 is offline
Send a message via AIM to Hawk552
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-09-2009 , 13:24   Re: [INC] Unix Time Conversion
Reply With Quote #3

Quote:
Originally Posted by Hawk552 View Post
I wrote up a header file for AlliedModders time. I hope you like it.
Good job!
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-09-2009 , 16:42   Re: [INC] Unix Time Conversion
Reply With Quote #4

HAHA Valve like always is right
__________________
xPaw is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-09-2009 , 21:02   Re: [INC] Unix Time Conversion
Reply With Quote #5

haha nice job chief
__________________

Last edited by Bugsy; 05-09-2009 at 21:12.
Bugsy is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 05-12-2009 , 21:56   Re: [INC] Unix Time Conversion
Reply With Quote #6

tyvm for this , 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;
//...
__________________

Last edited by stupok; 05-13-2009 at 00:38.
stupok is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-13-2009 , 18:44   Re: [INC] Unix Time Conversion
Reply With Quote #7

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
__________________

Last edited by Bugsy; 05-13-2009 at 18:59.
Bugsy is offline
TheRadiance
Senior Member
Join Date: Nov 2007
Location: Kazakhstan
Old 06-01-2009 , 09:19   Re: [INC] Unix Time Conversion
Reply With Quote #8

Bugsy, awesome!!! I like it, Thanks!
TheRadiance is offline
Send a message via ICQ to TheRadiance
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-24-2011 , 16:34   Re: [INC] Unix Time Conversion
Reply With Quote #9

New revision posted, read description for details.
__________________
Bugsy is offline
BrUn3S
Member
Join Date: Jan 2012
Location: Slovakia
Old 07-05-2012 , 13:31   Re: [INC] Unix Time
Reply With Quote #10

best!!!
thx
BrUn3S is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:54.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode