Rules FAQ Members List Search Register Login

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

Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Traphouse
 11-22-2017 , 12:06   ConvertFloatToSeconds Stock (Useful for Songs) #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 iSeconds, iMinutes, szExtraSeconds[ 1 ], iExtraSeconds, szString[ 5 ];    float_to_str( fTime, szString, charsmax( szString ) );    szExtraSeconds[ 0 ] = szString[ 2 ];    add( szExtraSeconds, charsmax( szExtraSeconds ), szString[ 3 ] );    iExtraSeconds = str_to_num( szString[ 2 ] );    iMinutes = floatround( fTime, floatround_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.15set_task( float( ConvertFloatToSeconds( MUSIC_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.
TheWhitesmith
Senior Member
Join Date: Oct 2017
Location: Morocco :c
 11-22-2017 , 12:44   Re: ConvertFloatToSeconds Stock (Useful for Songs) #2 Try PHP Code: ``` iMinutes = floatround( fTime, floatround_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"
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
 11-22-2017 , 13:55   Re: ConvertFloatToSeconds Stock (Useful for Songs) #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.
TheWhitesmith
Senior Member
Join Date: Oct 2017
Location: Morocco :c
11-22-2017 , 14:42   Re: ConvertFloatToSeconds Stock (Useful for Songs)
#4

Quote:
 Originally Posted by KliPPy 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
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Traphouse
11-22-2017 , 15:07   Re: ConvertFloatToSeconds Stock (Useful for Songs)
#5

Quote:
 Originally Posted by KliPPy 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 Try PHP Code: ``` iMinutes = floatround( fTime, floatround_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.
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
11-22-2017 , 16:32   Re: ConvertFloatToSeconds Stock (Useful for Songs)
#6

Quote:
 Originally Posted by edon1337 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.
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
 11-22-2017 , 19:52   Re: ConvertFloatToSeconds Stock (Useful for Songs) #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.
 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
11-22-2017 , 22:08   Re: ConvertFloatToSeconds Stock (Useful for Songs)
#8

Quote:
 Originally Posted by edon1337 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, ":", timeunits, sizeof(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 totalSeconds, char[] timeString) {    int hours = totalSeconds / (60*60);    int minutes = totalSeconds % (60 * 60) / 60;    int seconds = totalSeconds % 60;        if (hours) Format(timeString, 10, "%i:%02i:%02i",  hours, minutes, seconds);     else Format(timeString, 10, "%i:%02i", minutes, seconds); }  ```
__________________

Last edited by ddhoward; 11-23-2017 at 18:48. Reason: better code thanks to fysiks!
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
 11-23-2017 , 03:19   Re: ConvertFloatToSeconds Stock (Useful for Songs) #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``` __________________
ddhoward
Veteran Member
Join Date: May 2012
Location: California
11-23-2017 , 16:47   Re: ConvertFloatToSeconds Stock (Useful for Songs)
#10

Quote:
 Originally Posted by fysiks @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.
__________________

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Server Discussion     Source Servers (SRCDS)     HL1 Servers (HLDS) AMX Mod X     News     Bug Reports     General     Off-Topic     Plugins         Suggestions / Requests         Approved Plugins         New Plugin Submissions         Unapproved/Old Plugins         Translation Request         High-Traffic Plugins             GunGame             UAIO (Ultimate All-In-One Plugin)             xREDIRECT             CSDM             AMX Super             RuneMod             Zombie Plague Mod             SuperHero Mod                 News                 Tech Support                 Scripting Help                 Off-Topic / General Chat                 Heroes                     Suggestions / Requests                     Approved Heroes                     New Submissions                     Unapproved/Old Heroes                         Module Heroes                     SuperHero Mod Stats - By 123                 (OLD) Bug Reports     Scripting         Scripting Help         Code Snippets/Tutorials         Module Coding     Donor Access SourceMod     News     General     Plugins         Plugins         Unapproved Plugins         Plugin/Gameplay Ideas and Requests         High-Traffic Plugins             SourceMod Anti-Cheat             Zombie:Reloaded             SourceBans             VSH / Freak Fortress             Store             SM_Hosties             HLstatsX:CE     Scripting     Extensions     Snippets and Tutorials     Donor Access     Metamod: Source         Metamod:Source Plugins         Metamod:Source Questions         Coding MM:S Plugins & SM Extensions Hosted Stuff     Asherkin's Plugins         TF2Items         TFDodgeball         SteamTools     Bail's Plugins         CSDM         CS:S DM Off-Topic & Trash     Off-Topic     Trash

All times are GMT -4. The time now is 06:34.

 DMCA - Archive - Top