AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] Unix Time by Bugsy - Ported to SourceMod (https://forums.alliedmods.net/showthread.php?t=300350)

milutinke 08-13-2017 19:33

[INC] Unix Time by Bugsy - Ported to SourceMod
 
1 Attachment(s)

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:
http://i.imgur.com/qwa6h3n.png

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:

root88 09-26-2017 11:31

Re: [INC] Unix Time by Bugsy - Ported to SourceMod
 
Thanks!

BoBiTza 03-27-2021 08:53

Re: [INC] Unix Time by Bugsy - Ported to SourceMod
 
L 03/27/2021 - 14:53:05: [SM] Exception reported: Array index out-of-bounds (index 12, limit 12)
L 03/27/2021 - 14:53:05: [SM] Blaming: caseopening.smx
L 03/27/2021 - 14:53:05: [SM] Call stack trace:
L 03/27/2021 - 14:53:05: [SM] [1] Line 249, D:\Steam-server\steamapps\common\Counter-Strike Global Offensive Beta - Dedicated Server\csgo\addons\sourcemod\scripting\includ e\unixtime_sourcemod.inc::SecondsInMonth
L 03/27/2021 - 14:53:05: [SM] [2] Line 204, D:\Steam-server\steamapps\common\Counter-Strike Global Offensive Beta - Dedicated Server\csgo\addons\sourcemod\scripting\includ e\unixtime_sourcemod.inc::TimeToUnix

glhf3000 02-08-2024 09:30

Re: [INC] Unix Time by Bugsy - Ported to SourceMod
 
1 Attachment(s)
Since this inc has ~1000 downloads, here are my 2 cents:

1. TZ offset calc in GetTimeZone() is wrong:

PHP Code:

// FormatTime( szTime, sizeof( szTime ), "%m~%d~%y~%H~%M~%S", GetTime( ) ); // 24 -> 2024
FormatTimeszTimesizeofszTime ), "%m~%d~%Y~%H~%M~%S"GetTime( ) );
    
// char szTimeParts[ 6 ][ 4 ]; // https://github.com/alliedmodders/sourcepawn/blob/96ce3259003c8fe77cf9f1a0f84bbb3167b81b5d/vm/plugin-context.cpp#L346
char szTimeParts][ ]; 

&&

2. imagine local_unix->local_datetime->(change hour?)->local_unix, in TimeToUnix( using UT_TIMEZONE_SERVER ) there should be -, not + (double check my logic, timezones = minefield)
PHP Code:

// return ( iTimeStamp + g_iTimeZoneOffset[ iTimeZone ] );
return ( iTimeStamp g_iTimeZoneOffsetiTimeZone ] ); 


Fixed inc:

Bacardi 02-10-2024 14:50

Re: [INC] Unix Time by Bugsy - Ported to SourceMod
 
If some one want fancy time formats and calculations,
you can connect into SM extension sqlite and perhaps do fastcall query.

https://www.sqlite.org/lang_datefunc.html


All times are GMT -4. The time now is 16:06.

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