Raised This Month: $101 Target: $400
 25% 

ConvertFloatToSeconds Stock (Useful for Songs)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Traphouse
Old 11-22-2017 , 12:06   ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #1

Today, I was making a Song Player and my array of Songs had a dimension just for the music length, most of people don't know the exact length of a song in seconds so they write it in a 'float' form (Example 3:15), the way YouTube displays it. So I was in need of a stock that would Convert that 'float' length to seconds but couldn't find any, that's why I'm posting this stock here so other's can find what they need.

3:15

3 = Number of minutes.
15 = Number of seconds.

Stock:

PHP Code:
ConvertFloatToSeconds( const Float:fTime )
{
    new 
iSecondsiMinutesszExtraSeconds], iExtraSecondsszString];

    
float_to_strfTimeszStringcharsmaxszString ) );

    
szExtraSeconds] = szString];
    
addszExtraSecondscharsmaxszExtraSeconds ), szString] );

    
iExtraSeconds str_to_numszString] );

    
iMinutes floatroundfTimefloatround_floor );

    
iSeconds iMinutes 60;

    return 
iSeconds += iExtraSeconds;

The reason I needed this stock was that to make the song more relaxable by lowering the volume of the game sounds when the music started and letting the player feel the music. So I needed this stock to create a task to be executed when music would end and increase the game volume of the player again.

Usage of the stock:

PHP Code:
#define MUSIC_LENGTH 3.15

set_taskfloatConvertFloatToSecondsMUSIC_LENGTH ), "OnTaskEndSong"id_) ); 
Test Logs:
Code:
L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.14 | New Converted Value: 194 L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 0.26 | New Converted Value: 26 L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.06 | New Converted Value: 186
__________________
Knife Bounty
CS Battle Royale (PUBG)
Contact/Ticket System

Santas vs Monsters New Private Mod

Juice Wrld

Last edited by edon1337; 12-26-2017 at 07:30.
edon1337 is offline
Send a message via Skype™ to edon1337
TheWhitesmith
Senior Member
Join Date: Oct 2017
Location: Morocco :c
Old 11-22-2017 , 12:44   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #2

Try
PHP Code:
iMinutes floatroundfTimefloatround_floor 
If I'm not wrong the error does not come from float_to_str, but from floatround because if the 2nd option is left blank, it will use "floatround_round" as default, and HERE it says "floatround_round : rounds to the nearest integer"
TheWhitesmith is offline
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-22-2017 , 13:55   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #3

I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.

Last edited by KliPPy; 11-22-2017 at 13:55.
KliPPy is offline
TheWhitesmith
Senior Member
Join Date: Oct 2017
Location: Morocco :c
Old 11-22-2017 , 14:42   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.
Lack of mathematics? doesn't know that 1 minute is 60 second? Lazy to convert minutes to seconds? lol
TheWhitesmith is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Traphouse
Old 11-22-2017 , 15:07   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #5

Quote:
Originally Posted by KliPPy View Post
I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.
In YouTube, songs length are displayed in minute:seconds format. Furthermore, when you want to print the length of a video in chat, if you use 'Video Length: 195.0 seconds' they'll get confused lol. It's better for the readability. I personally needed this stock so I posted it here for other people. I don't get what you're trying to say with '3:15 as 3.15', how would you convert 3:15 to seconds? It's not even a number.

Quote:
Originally Posted by TheWhitesmith View Post
Try
PHP Code:
iMinutes floatroundfTimefloatround_floor 
If I'm not wrong the error does not come from float_to_str, but from floatround because if the 2nd option is left blank, it will use "floatround_round" as default, and HERE it says "floatround_round : rounds to the nearest integer"
I'll try your method, thanks .
__________________
Knife Bounty
CS Battle Royale (PUBG)
Contact/Ticket System

Santas vs Monsters New Private Mod

Juice Wrld

Last edited by edon1337; 11-22-2017 at 15:11.
edon1337 is offline
Send a message via Skype™ to edon1337
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-22-2017 , 16:32   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #6

Quote:
Originally Posted by edon1337 View Post
In YouTube, songs length are displayed in minute:seconds format. Furthermore, when you want to print the length of a video in chat, if you use 'Video Length: 195.0 seconds' they'll get confused lol. It's better for the readability. I personally needed this stock so I posted it here for other people. I don't get what you're trying to say with '3:15 as 3.15', how would you convert 3:15 to seconds? It's not even a number.



I'll try your method, thanks .
That's the thing - the minute:seconds format is a string. In your example you defined MUSIC_LENGTH to 3.15.
It's more logical to store a timespan as the number of seconds. 3.15 is not the number of seconds, 195.0 is. It's much easier to convert 195.0 to that string representation than 3.15. You'll find that in any programming language / library that can work with timespans. It's also really easy to convert "3:15" back to 195.

Last edited by KliPPy; 11-22-2017 at 16:33.
KliPPy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-22-2017 , 19:52   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #7

This is completely wrong and shouldn't be used by anyone.

The time 3:15 (3 minutes, 15 seconds) is 3.25 minutes. As others mentioned, time should be stored in a plugin as seconds. When you need to use time as an input or
output, you can convert it to/from the colon notation (as a string).

I recommend that you either convert this thread to functions for doing the conversion to and from the colon notation or remove the thread entirely so that people new to plugin scripting aren't confused.
__________________

Last edited by fysiks; 11-22-2017 at 19:55.
fysiks is offline
Old 11-22-2017, 21:44
ddhoward
This message has been deleted by ddhoward. Reason: new post with more information
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 11-22-2017 , 22:08   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #8

Quote:
Originally Posted by edon1337 View Post
how would you convert 3:15 to seconds? It's not even a number.
Grab the value before the : and multiply it by 60. Then add the value that was after the :.

If you wanted to go the opposite direction, take the total seconds value and divide it by 60. That's your minutes. Then take the total seconds value and modulo it by 60. There's the rest of the seconds.

Here's the equivalent code that you'd find in a Sourcemod plugin, with some tweaks to make it work if they included hours.

PHP Code:

stock int HMSDurationToSeconds
(char[] duration) {
    
char timeunits[3][8]
    
int numStrings ExplodeString(duration":"timeunitssizeof(timeunits), sizeof(timeunits[]));
    if (!
numStrings) return 0;
    
    
int numSeconds;
    
numSeconds += StringToInt(timeunits[0]);
    if (
numStrings 1) {
        
numSeconds *= 60;
        
numSeconds += StringToInt(timeunits[1]);
    }
    if (
numStrings 2) {
        
numSeconds *= 60;
        
numSeconds += StringToInt(timeunits[2]);
    }
    return 
numSeconds;
}

stock void SecondsToHMSDuration(int totalSecondschar[] timeString) {
    
int hours totalSeconds / (60*60);
    
int minutes totalSeconds % (60 60) / 60;
    
int seconds totalSeconds 60;
    
    if (
hoursFormat(timeString10"%i:%02i:%02i",  hoursminutesseconds); 
    else 
Format(timeString10"%i:%02i"minutesseconds); 

__________________

Last edited by ddhoward; 11-23-2017 at 18:48. Reason: better code thanks to fysiks!
ddhoward is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-23-2017 , 03:19   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #9

@ddhoward, going from duration to hours, minutes, and seconds can be done more elegantly without any subtraction or multiplication:

Code:
new iDays= iDuration / 86400
new iHours = iDuration % 86400 / 3600
new iMinutes = iDuration % 3600 / 60
new iSeconds = iDuration % 60
__________________
fysiks is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 11-23-2017 , 16:47   Re: ConvertFloatToSeconds Stock (Useful for Songs)
Reply With Quote #10

Quote:
Originally Posted by fysiks View Post
@ddhoward, going from duration to hours, minutes, and seconds can be done more elegantly without any subtraction or multiplication:

Code:
new iDays= iDuration / 86400
new iHours = iDuration % 86400 / 3600
new iMinutes = iDuration % 3600 / 60
new iSeconds = iDuration % 60
Haha, excellent. I knew there was an easier way to do it, I just couldn't think of it in my sleep-deprived state. I'll update my post now.
__________________
ddhoward 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 06:34.


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