Raised This Month: $32 Target: $400
 8% 

Solved Precise calculation between 2 dates and times in UnixTimestamp


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 06-23-2021 , 13:57   Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #1

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

__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 06-24-2021 at 16:52.
iceeedr is offline
Send a message via Skype™ to iceeedr
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-23-2021 , 14:06   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #2

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.
__________________
HamletEagle is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 06-23-2021 , 14:56   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
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.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-23-2021 , 17:22   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #4

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;

__________________
Bugsy is online now
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 06-23-2021 , 17:45   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
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.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 06-23-2021 at 17:47.
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-23-2021 , 18:21   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #6

I guess I do not fully understand what you are trying to do. Can you explain a real world example?
__________________
Bugsy is online now
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 06-23-2021 , 18:30   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
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

__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-23-2021 , 19:04   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #8

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"
__________________
Bugsy is online now
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 06-23-2021 , 19:10   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
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.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-23-2021 , 21:28   Re: Precise calculation between 2 dates and times in UnixTimestamp
Reply With Quote #10

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" );
    }

__________________

Last edited by Bugsy; 06-23-2021 at 21:29.
Bugsy is online now
Reply



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 10:11.


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