[PYTHON] Make a given number of seconds into hours, minutes and seconds

def convert_seconds(ms):
    float_num = False
    if type(ms) == float:
        float_nums = ms - int(ms)
        ms = int(ms)
        float_num = True
    hour = ms / 3600
    minute = (ms - hour * 3600) / 60
    if float_num:
        second = ms - (hour * 3600 + minute * 60) + float_nums
    else:
        second = ms - (hour * 3600 + minute * 60)
    format_hour = "hour"
    format_minute = "minute"
    format_second = "second"
    if hour > 1 or hour == 0:
        format_hour = "hour" + "s"
    if minute > 1 or minute == 0:
        format_minute = "minute" + "s"
    if second > 1 or second == 0:
        format_second = "second" + "s"
    result = (str(hour) + " " + format_hour, str(minute) + " " + format_minute, str(second) + " " + format_second)
    return result[0] + ", " + result[1] + ", " + result[2]

print convert_seconds(3600)
#>>> 1 hour, 1 minute, 1 second

print convert_seconds(7325)
#>>> 2 hours, 2 minutes, 5 seconds

print convert_seconds(7261.7)
#>>> 2 hours, 1 minute, 1.7 seconds

If there is something that contains a decimal, conditional branching is performed and the decimal value is retained. Implemented by adding in the last calculation There are many if statements ...

Recommended Posts

Make a given number of seconds into hours, minutes and seconds
Organize a large number of files into folders
Paste a large number of image files into PowerPoint [python-pptx]
A1 notation and 26-ary number
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Make a DNN-CRF with Chainer and recognize the chord progression of music
A rough understanding of python-fire and a memo
Try to make a kernel of Jupyter
Connect a large number of videos together!
Make a relation diagram of Python module
A little scrutiny of pandas 1.0 and dask
Connect a lot of Python or and and
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~
Generate an SSID and password, make it a QR code and throw it into Slack.
How to count the number of elements in Django and output to a template
[Python] A program that calculates the number of updates of the highest and lowest records
A story of using rembg to make a white background + person video into a black background video
Get the stock price of a Japanese company with Python and make a graph