Raised This Month: $51 Target: $400
 12% 

Solved Better method time format to timestamp


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-05-2019 , 05:20   Better method time format to timestamp
Reply With Quote #1

i have a function will transfer numbers and words of time format to timestamp, i was wondering if there is a better way i can achieve.

PHP Code:
new const szTimeLength[][] = {
    
"Second",
    
"Minute",
    
"Hour",
    
"Day",
    
"Week",
    
"Month",
    
"Year",
    
"Permanent"
}

new const 
iLengthMultiplier[] = {
    
1,
    
60,
    
3600,
    
86400,
    
604800,
    
2592000,
    
86400*365,
    
0
}

stock time_to_timestamps(const expiredate[])
{
    new 
iExpireLength;
    for(new 
szTimeLength_Size sizeof szTimeLengthicxiLen strlen(expiredate), szNum[20], gszTime[11]; iLeni++)
    {
        if(
isdigit(expiredate[i]))
        {
            if(
00;
            if(!( 
sizeof szNum >= 0)) continue;
            
szNum[c] = expiredate[i];
            
c++;
        }
        else if( 
sizeof szTime >= && isalpha(expiredate[i]) )
        {
            
szTime[g] = expiredate[i];
            
g++;
            
            for(
0szTimeLength_Sizex++)
            {
                if(
equali(szTimeLength[x], szTime))
                {
                    
str_to_num(szNum);
                    
iExpireLength += iLengthMultiplier[x] * !1:c;
                    
0;
                    
                    
arrayset(szTime0sizeof szTime);
                    
arrayset(szNum0sizeof szNum);
                }
            }
        }
    }
    return 
iExpireLength 


Example of stock:-

PHP Code:
time_to_timestamps("1 week 1 day"// will return  ( 86400 * 7 + 86400 ) = 691200 
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-09-2019 at 04:54.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-05-2019 , 22:15   Re: Better method time format to timestamp
Reply With Quote #2

Depends on if you enforce a pattern. If you enforce spaces between everything you can use strtok() or strbreak() to get each section in a loop instead of parsing each character. You'd also need to require that there always be a number followed by a string.

Here's a quick test function I wrote. It doesn't check the whole unit string but surely that's not all that important.

PHP Code:
public cmdTest()
{
    new 
szArg[100]
    
read_args(szArgcharsmax(szArg))
    
remove_quotes(szArg)
    
    new 
szValue[5], iValueszUnit[32], iMultiplier
    
new iTotal 0
    
    
while( szArg[0] )
    {
        
strbreak(szArgszValuecharsmax(szValue), szArgcharsmax(szArg))
        
strbreak(szArgszUnitcharsmax(szUnit), szArgcharsmax(szArg))
        
        
iValue str_to_num(szValue)
        
        switch(
szUnit[0])
        {
            case 
's''S':
            {
                
iMultiplier 1
            
}
            case 
'm''M':
            {
                switch(
szUnit[1])
                {
                    case 
'i'iMultiplier 60
                    
case 'o'iMultiplier 2592000
                
}
            }
            case 
'h''H':
            {
                
iMultiplier 3600
            
}
            case 
'd''D':
            {
                
iMultiplier 86400
            
}
            case 
'w''W':
            {
                
iMultiplier 604800
            
}
            case 
'y''Y':
            {
                
iMultiplier 31536000
            
}
            default:
            {
                
iMultiplier 0
            
}
        }
        
        
iTotal += iValue iMultiplier
    
}
        
    
server_print("Total = %d"iTotal)

You could obviously check for the odd one out (i.e. "Permanent") before doing this loop.


Why are you defining all your variables in the for loop statement? It makes your code harder to read.
__________________

Last edited by fysiks; 03-05-2019 at 23:26.
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-06-2019 , 11:36   Re: Better method time format to timestamp
Reply With Quote #3

There is no specific pattern and numbers aren't always followed by a string.

Why I'm defining the variables at the loop init because I'm only using them in the body of the loop.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-06-2019 , 22:05   Re: Better method time format to timestamp
Reply With Quote #4

Quote:
Originally Posted by Natsheh View Post
There is no specific pattern and numbers aren't always followed by a string.
Um, how does that work? What is a scenario that a number isn't followed by a string? What patterns that you expect won't work my the function that I provided?
__________________
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-07-2019 , 03:32   Re: Better method time format to timestamp
Reply With Quote #5

time_to_timestamps("year")

time_to_timestamps("1week")
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 03-08-2019 , 14:07   Re: Better method time format to timestamp
Reply With Quote #6

Input param need to be a string if i understand correctly?
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-08-2019 , 15:21   Re: Better method time format to timestamp
Reply With Quote #7

Quote:
Originally Posted by ^SmileY View Post
Input param need to be a string if i understand correctly?
Then what is it currently?

I am asking if there is a better method to interpret string rather than reading the string char by char based on the multiple various inputs, to transfer time from words t0 timestamp.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-09-2019 at 04:52.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-08-2019 , 21:10   Re: Better method time format to timestamp
Reply With Quote #8

Quote:
Originally Posted by Natsheh View Post
I am asking if there is a better method to format interpret the string than reading the string char by char based on the multiple various inputs, to transfer time from words t0 timestamp.
Fixed.

If there is no required pattern, you're going to need to traverse the string by individual character anyways (unless you use RegEx though that might be less efficient).
__________________
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-09-2019 , 04:53   Re: Better method time format to timestamp
Reply With Quote #9

Thanks for your time regex seems the best solution for this solved.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 16:05.


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