AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Precise calculation between 2 dates and times in UnixTimestamp (https://forums.alliedmods.net/showthread.php?t=333183)

iceeedr 06-23-2021 13:57

Precise calculation between 2 dates and times in UnixTimestamp
 
Hi, it's been a while. I've been racking my brain for a few days with a classic XYZ problem, and honestly I ran out of ideas.

I need to calculate the difference between 2 dates and times and return the value to me in unixtimestamp, for example the difference in Unix between now and Friday 14:00PM, but honestly I'm unsuccessful in this.

That's what's left after several attempts, the days return correctly, but I'm in doubt as to how to proceed with the calculation of hours to be an exact value.

PHP Code:

/* Sublime AMXX Editor v2.2 */

#include <amxmodx>
// #include <amxmisc>
// #include <cstrike>
// #include <engine>
// #include <fakemeta>
// #include <hamsandwich>
// #include <fun>
// #include <xs>
// #include <sqlx>

#define PLUGIN  "New Plug-In"
#define VERSION "1.0"
#define AUTHOR  "Author"

public plugin_init()
{
        
register_plugin(PLUGINVERSIONAUTHOR)

        
// Add your code here...
}

public 
plugin_cfg()
{
        new 
Time[15]
        
get_time("%A"Timecharsmax(Time))

        new 
EventDay CalculateDays(Time)

        
Counter EventDay 86400

        set_task_ex
(1.0"TimeToEvent"TASK_HUD_DROP, .flags SetTask_Repeat)
}

public 
TimeToEvent()
{
        
Counter --

        if(
Counter 0)
        {
                new 
UnixTimeStamp Counter

                Day 
UnixTimeStamp 86400
                UnixTimeStamp 
UnixTimeStamp 86400
                Hour 
UnixTimeStamp 3600
                UnixTimeStamp 
UnixTimeStamp 3600
                Minute 
UnixTimeStamp 60
                Second 
UnixTimeStamp 60

                
if(Day 1)
                {
                       
//
                        
return PLUGIN_HANDLED
                
}

                
//
                
return PLUGIN_HANDLED
        
}

        
remove_task(TASK_HUD_DROP)
        
set_task(0.1"AnounceEvent")
        return 
PLUGIN_HANDLED
}

CalculateDays(const szDay[])
{
        new 
Count 0
        enum _
:ItemData
        
{
                
szDays[10],
                
iCount
        
}
        new const 
g_szDays[][ItemData] =
        {
                {
"Sunday",       1},
                {
"Monday",       2},
                {
"Tuesday",      3},
                {
"Wednesday",    4},
                {
"Thursday",     5},
                {
"Friday",       6},
                {
"Saturday",     7}
        }

        for(new 
isizeof g_szDaysi++)
        {
                if(
equali(szDayg_szDays[i][szDays]))
                {
                        
Count g_szDays[i][iCount]
                        break
                }
        }

        return 
Count



HamletEagle 06-23-2021 14:06

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Use parse_time to convert both the start and end date time strings to unix timestamps. Then subtract these 2 values and you'll get the difference in seconds, which seems to be what you want.

iceeedr 06-23-2021 14:56

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Quote:

Originally Posted by HamletEagle (Post 2750891)
Use parse_time to convert both the start and end date time strings to unix timestamps. Then subtract these 2 values and you'll get the difference in seconds, which seems to be what you want.

Thanks for taking the time to respond, the "problem" is that I want this automated function to run on a specific day and time every week, for example every Saturday 12 PM, which is why parse_time doesn't seem to cover this situation.

Bugsy 06-23-2021 17:22

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Does this help?
PHP Code:

#include <amxmodx>

public plugin_init() 
{
    new const 
FutureDate[] = "14:00:00 06/25/2021";
    
    new 
iFutureDateTime parse_timeFutureDate "%H:%M:%S %m/%d/%Y" );
    new 
iCurrentDateTime get_systime();
    new 
iTimeDifference iFutureDateTime iCurrentDateTime;
    new 
iDays iHours iMinutesiSeconds;
    
    
convert_secondsiTimeDifference iSeconds iMinutes iHours iDays );
    
    
server_print"%s is %d days, %d hours, %d minutes, %d seconds away" FutureDate iDays iHours iMinutes iSeconds );
}

convert_secondsiTotalSeconds , &seconds , &minutes , &hours , &days 
{
    
days iTotalSeconds 86400;
    
hours = ( iTotalSeconds 3600 ) % 24;
    
minutes = ( iTotalSeconds 60 ) % 60;
    
seconds iTotalSeconds 60;



iceeedr 06-23-2021 17:45

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Quote:

Originally Posted by Bugsy (Post 2750921)
Does this help?
PHP Code:

#include <amxmodx>

public plugin_init() 
{
    new const 
FutureDate[] = "14:00:00 06/25/2021";
    
    new 
iFutureDateTime parse_timeFutureDate "%H:%M:%S %m/%d/%Y" );
    new 
iCurrentDateTime get_systime();
    new 
iTimeDifference iFutureDateTime iCurrentDateTime;
    new 
iDays iHours iMinutesiSeconds;
    
    
convert_secondsiTimeDifference iSeconds iMinutes iHours iDays );
    
    
server_print"%s is %d days, %d hours, %d minutes, %d seconds away" FutureDate iDays iHours iMinutes iSeconds );
}

convert_secondsiTotalSeconds , &seconds , &minutes , &hours , &days 
{
    
days iTotalSeconds 86400;
    
hours = ( iTotalSeconds 3600 ) % 24;
    
minutes = ( iTotalSeconds 60 ) % 60;
    
seconds iTotalSeconds 60;



In a way yes, I think I'll have to create an array with all the "Saturdays" of the year to be able to save the parse_time automatically since the event will always recur on the same day and time.

Edit: And that made me have an idea of ​​what to do, I will use part of my stock with part of yours, so I take how many days are left until the required date, and I use parse_time, thanks.

Bugsy 06-23-2021 18:21

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
I guess I do not fully understand what you are trying to do. Can you explain a real world example?

iceeedr 06-23-2021 18:30

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Quote:

Originally Posted by Bugsy (Post 2750928)
I guess I do not fully understand what you are trying to do. Can you explain a real world example?

Of course, I've just finished my idea, it needs some refinement, but it's already serving the purpose.

PHP Code:

public CalculateStrings()
{
        new 
Time[15], ToDay[5],  Month[5]

        
get_time("%A"Timecharsmax(Time))
        new 
EventDay CalculateDays(Time)

        
get_time("%d"ToDaycharsmax(ToDay))
        new 
iFutureDate str_to_num(ToDay) + EventDay

        get_time
("%m"Monthcharsmax(Month))

        new 
FormatedFutureDate[30]
        
formatex(FormatedFutureDatecharsmax(FormatedFutureDate), "19:00:00 %i/%i/2021"str_to_num(Month), iFutureDate)

        new 
iFutureDateTime parse_timeFormatedFutureDate "%H:%M:%S %m/%d/%Y" );
        new 
iCurrentDateTime get_systime();
        
Counter iFutureDateTime iCurrentDateTime;

        
set_task_ex(1.0"TimeToEvent"TASK_HUD_DROP, .flags SetTask_Repeat)
}

convert_secondsiTotalSeconds , &seconds , &minutes , &hours , &days )
{
        
days iTotalSeconds 86400
        hours 
= ( iTotalSeconds 3600 ) % 24
        minutes 
= ( iTotalSeconds 60 ) % 60
        seconds 
iTotalSeconds 60
}

CalculateDays(const szDay[])
{
        new 
Count 0
        enum _
:ItemData
        
{
                
szDays[10],
                
iCount
        
}
        new const 
g_szDays[][ItemData] =
        {
                {
"Sunday",       1},
                {
"Monday",       2},
                {
"Tuesday",      3},
                {
"Wednesday",    4},
                {
"Thursday",     5},
                {
"Friday",       6},
                {
"Saturday",     7}
        }

        for(new 
isizeof g_szDaysi++)
        {
                if(
equali(szDayg_szDays[i][szDays]))
                {
                        
Count g_szDays[i][iCount]
                        break
                }
        }

        return 
Count



Bugsy 06-23-2021 19:04

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Explaining in code isn't ideal, just explain in words.

Example: "I want my plugin to tell players how much time away it currently is from 7/4/21, and then again 1 week later"

iceeedr 06-23-2021 19:10

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Quote:

Originally Posted by Bugsy (Post 2750935)
Explaining in code isn't ideal, just explain in words.

Example: "I want my plugin to tell players how much time away it currently is from 7/4/21, and then again 1 week later"

Forgive me, I thought I was clear when I answered in post #3, but that's exactly what you got.

Bugsy 06-23-2021 21:28

Re: Precise calculation between 2 dates and times in UnixTimestamp
 
Ok, it's a lot simpler then if I understand correctly. format_time() can do most of the work for you, see http://cplusplus.com/reference/ctime/strftime/

PHP Code:


#include <amxmodx>

enum _:WeekDays
{
    
wdSunday,
    
wdMonday,
    
wdTuesday,
    
wdWednesday,
    
wdThursday,
    
wdFriday,
    
wdSaturday
}

enum _:TimeStringPos
{
    
tspDayOfWeek,
    
tspSpace,
    
tspHour
}

public 
plugin_init() 
{
    new 
szOutput] , iDayOfWeek iHour;

    
//1624465533 = Wed Jun 23 2021 12:25:33 GMT-0400 (Eastern Daylight Time)
    
format_timeszOutput charsmaxszOutput ) , "%w %H" 1624465533 );
    
    
szOutputtspSpace ] = EOS;
    
iDayOfWeek str_to_numszOutputtspDayOfWeek ] );
    
iHour str_to_numszOutputtspHour ] );
    
    if ( ( 
iDayOfWeek == wdWednesday ) && ( iHour == 12 ) )
    {
        
server_print"It is Wednesday at 12pm" );
    }




All times are GMT -4. The time now is 02:33.

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