Moortiii icon

Human Readable Time

Moortiii | PRO | 11/28/16 05:39:33 PM UTC | 0 ⭐ | 365 👁️ | Never ⏰ | []
Python |

1.52 KB

|

None

|

0 👍

/

0 👎

# This would probably be a lot easier to do using the timedate library and timedelta
# or something along those lines, but I actually quite like this approach to the challenge.
 
# I originally had a variable called remainder that stored the number of seconds after each
# subtraction, but quickly realized it was redundant, once "seconds" is smaller than 60, we
# can simply return the seconds variable itself, as it will always fit the criteria.
 
def make_readable(seconds):
    hours = 0  # A counter that tracks the number of hours
    minutes = 0  # A counter that tracks the number of minutes
    
    while seconds >= 3600:  # Check that there is at least 1 hour worth of seconds left
        seconds -= 3600  # Subtract exactly 1 hour from the amount of seconds
        hours += 1  # Add 1 to the counter that tracks hours
        
    while seconds >= 60:  # Check that there is at least 1 minute worth of seconds left
        seconds -= 60  # Subtract exactly 1 minute from the amount of seconds.
        minutes += 1  # Add 1 to the counter that tracks minutes
    
    if hours < 10:
        hours = "0" + str(hours)  # Some simple formatting to get the HH:MM:SS output we want
    if minutes < 10:
        minutes = "0" + str(minutes)  # Some simple formatting to get the HH:MM:SS output we want
    if seconds < 10:
        seconds = "0" + str(seconds)  # Some simple formatting to get the HH:MM:SS output we want
    
    return "{}:{}:{}".format(hours, minutes, seconds)  # Finally we return the formatted string

Comments