ConvertFloatToSeconds Stock (Useful for Songs)
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:
Usage of the stock: PHP Code:
Code:
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Try
PHP Code:
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
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.
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Quote:
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Quote:
Quote:
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Quote:
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. |
Re: ConvertFloatToSeconds Stock (Useful for Songs)
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. |
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Quote:
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:
|
Re: ConvertFloatToSeconds Stock (Useful for Songs)
@ddhoward, going from duration to hours, minutes, and seconds can be done more elegantly without any subtraction or multiplication:
Code:
new iDays= iDuration / 86400 |
Re: ConvertFloatToSeconds Stock (Useful for Songs)
Quote:
|
All times are GMT -4. The time now is 01:57. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.