AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
|
08-13-2017
, 19:33
[INC] Unix Time by Bugsy - Ported to SourceMod
|
#1
|
I have used this in my Amx ModX plugins.
Today I needed same functionality in my Source Mod plugin, so I decided to port this include to new Source Mod syntax to be able to use it.
I hope that you will find it useful as it is useful for my purposes, and I want to thank Bugsy for creating this awesome include
Originally made by Bugsy for Amx ModX.
Original theme with more informations: https://forums.alliedmods.net/showthread.php?t=91915
Example usage:
PHP Code:
public void OnPluginStart( ) { int iTime, iTimeAdjusted, iYear, iMonth, iDay, iHour, iMinute, iSecond; iTime = GetTime( ); // Display GetTime( ) value which is UTC time (no +/- adjustment for timezone) PrintToServer( "GetTime( ) = %d" , iTime ); UnixToTime( iTime , iYear , iMonth , iDay , iHour , iMinute , iSecond ); PrintToServer( "GetTime( ) 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 ); PrintToServer( "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 ); PrintToServer( "UnixToTime Adjusted Time = %02d/%02d/%d %02d:%02d:%02d" , iMonth , iDay , iYear , iHour , iMinute , iSecond ); }
Result:
Here are 2 useful functions written by me, which can be improved
TO DO: Add regex checks
PHP Code:
#include <sourcemod> #include <unixtime_sourcemod>
#pragma semicolon 1 #pragma newdecls required
public Plugin myinfo = { name = "", author = "", description = "", version = "0.0.0", url = "" };
public void OnPluginStart( ) { int iTimeStamp = DateToTimestamp( "3/7/2017 15:30:2" ); PrintToServer( "Date -> Timestamp: %d", iTimeStamp ); int iYear, iMonth, iDay, iHour, iMinute, iSecond; UnixToTime( iTimeStamp, iYear, iMonth, iDay, iHour, iMinute, iSecond, UT_TIMEZONE_SERVER ); PrintToServer( "Timestamp -> Date: %02d/%02d/%d %02d:%02d:%02d", iMonth, iDay, iYear, iHour, iMinute, iSecond ); }
// For date and time - Format: mm/dd/yyyy HH:MM:SS stock int DateTimeToTimestamp( const char[ ] szDate ) { char szBuffer[ 64 ]; strcopy( szBuffer, sizeof( szBuffer ), szDate ); ReplaceString( szBuffer, sizeof( szBuffer ), "/", " " ); ReplaceString( szBuffer, sizeof( szBuffer ), ".", " " ); ReplaceString( szBuffer, sizeof( szBuffer ), ":", " " ); char szTime[ 6 ][ 6 ]; ExplodeString( szBuffer, " ", szTime, sizeof( szTime ), sizeof( szTime[ ] ) ); int iYear = StringToInt( szTime[ 2 ] ); int iMonth = StringToInt( szTime[ 0 ] ); int iDay = StringToInt( szTime[ 1 ] ); int iHour = StringToInt( szTime[ 3 ] ); int iMinute = StringToInt( szTime[ 4 ] ); int iSecond = StringToInt( szTime[ 5 ] ); return TimeToUnix( iYear, iMonth, iDay, iHour, iMinute, iSecond, UT_TIMEZONE_SERVER ); }
// Just for date - Format: mm/dd/yyyy stock int DateToTimestamp( const char[ ] szDate ) { char szBuffer[ 64 ]; strcopy( szBuffer, sizeof( szBuffer ), szDate ); ReplaceString( szBuffer, sizeof( szBuffer ), "/", " " ); ReplaceString( szBuffer, sizeof( szBuffer ), ".", " " ); char szTime[ 3 ][ 6 ]; ExplodeString( szBuffer, " ", szTime, sizeof( szTime ), sizeof( szTime[ ] ) ); int iYear = StringToInt( szTime[ 2 ] ); int iMonth = StringToInt( szTime[ 0 ] ); int iDay = StringToInt( szTime[ 1 ] ); int iHour = iMinute = iSecond = 0; return TimeToUnix( iYear, iMonth, iDay, iHour, iMinute, iSecond, UT_TIMEZONE_SERVER ); }
Download:
Last edited by milutinke; 08-13-2017 at 20:21.
|
|