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