View Single Post
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 11-22-2017 , 21: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 17:48. Reason: better code thanks to fysiks!
ddhoward is offline