View Single Post
Author Message
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 08-13-2017 , 19:33   [INC] Unix Time by Bugsy - Ported to SourceMod
Reply With Quote #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 iTimeiTimeAdjustediYeariMonthiDayiHouriMinuteiSecond;
    
    
iTime GetTime( );
    
    
// Display GetTime( ) value which is UTC time (no +/- adjustment for timezone)
    
PrintToServer"GetTime( ) = %d" iTime );
    
UnixToTimeiTime 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 TimeToUnixiYear iMonth iDay iHour iMinute iSecond UT_TIMEZONE_SERVER );
    
UnixToTimeiTimeAdjusted 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( )
    
UnixToTimeiTime 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 iYeariMonthiDayiHouriMinuteiSecond;
    
UnixToTimeiTimeStampiYeariMonthiDayiHouriMinuteiSecondUT_TIMEZONE_SERVER );
    
    
PrintToServer"Timestamp -> Date: %02d/%02d/%d %02d:%02d:%02d"iMonthiDayiYeariHouriMinuteiSecond );
}

// For date and time - Format: mm/dd/yyyy HH:MM:SS
stock int DateTimeToTimestamp( const char[ ] szDate ) {
    
char szBuffer64 ];
    
strcopyszBuffersizeofszBuffer ), szDate );
    
    
ReplaceStringszBuffersizeofszBuffer ), "/"" " );
    
ReplaceStringszBuffersizeofszBuffer ), "."" " );
    
ReplaceStringszBuffersizeofszBuffer ), ":"" " );
    
    
char szTime][ ];
    
ExplodeStringszBuffer" "szTimesizeofszTime ), sizeofszTime[ ] ) );
    
    
int iYear StringToIntszTime] );
    
int iMonth  StringToIntszTime] );
    
int iDay StringToIntszTime] );
    
    
int iHour StringToIntszTime] );
    
int iMinute  StringToIntszTime] );
    
int iSecond StringToIntszTime] );
    
    return 
TimeToUnixiYeariMonthiDayiHouriMinuteiSecondUT_TIMEZONE_SERVER );
}

// Just for date - Format: mm/dd/yyyy
stock int DateToTimestamp( const char[ ] szDate ) {
    
char szBuffer64 ];
    
strcopyszBuffersizeofszBuffer ), szDate );
    
    
ReplaceStringszBuffersizeofszBuffer ), "/"" " );
    
ReplaceStringszBuffersizeofszBuffer ), "."" " );
    
    
char szTime][ ];
    
ExplodeStringszBuffer" "szTimesizeofszTime ), sizeofszTime[ ] ) );
    
    
int iYear StringToIntszTime] );
    
int iMonth  StringToIntszTime] );
    
int iDay StringToIntszTime] );
    
int iHour iMinute  iSecond 0;
    
    return 
TimeToUnixiYeariMonthiDayiHouriMinuteiSecondUT_TIMEZONE_SERVER );

Download:
Attached Files
File Type: inc unixtime_sourcemod.inc (6.1 KB, 922 views)

Last edited by milutinke; 08-13-2017 at 20:21.
milutinke is offline
Send a message via Skype™ to milutinke