Raised This Month: $ Target: $400
 0% 

[INC] Unix Time by Bugsy - Ported to SourceMod


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 08-13-2017 , 20: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, 844 views)

Last edited by milutinke; 08-13-2017 at 21:21.
milutinke is offline
Send a message via Skype™ to milutinke
root88
Senior Member
Join Date: May 2016
Old 09-26-2017 , 12:31   Re: [INC] Unix Time by Bugsy - Ported to SourceMod
Reply With Quote #2

Thanks!
__________________
root88 is offline
BoBiTza
Junior Member
Join Date: Feb 2021
Old 03-27-2021 , 09:53   Re: [INC] Unix Time by Bugsy - Ported to SourceMod
Reply With Quote #3

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
BoBiTza is offline
glhf3000
Member
Previously known as:
[ru]In1ernal Error
Join Date: Aug 2010
Location: middle-universe
Old 02-08-2024 , 10:30   Re: [INC] Unix Time by Bugsy - Ported to SourceMod
Reply With Quote #4

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:
Attached Files
File Type: inc unixtime_sourcemod.inc (6.3 KB, 1 views)

Last edited by glhf3000; 02-08-2024 at 12:19.
glhf3000 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 02-10-2024 , 15:50   Re: [INC] Unix Time by Bugsy - Ported to SourceMod
Reply With Quote #5

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
__________________
Do not Private Message @me

Last edited by Bacardi; 02-10-2024 at 15:51.
Bacardi 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 07:26.


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