mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Convert seconds to days, hours, minutes and seconds
This is a simple Python program to convert a given string into days, hours, minutes and the remaining seconds.
This commit is contained in:
parent
b9a797f3d4
commit
a078c2a9c0
33
maths/seconds_time_conversion.py
Normal file
33
maths/seconds_time_conversion.py
Normal file
@ -0,0 +1,33 @@
|
||||
def time_conversion(sec:int) -> int:
|
||||
"""
|
||||
This simple python program converts seconds to days, hours, minutes and seconds
|
||||
|
||||
Usage examples:
|
||||
>>> time_conversion(458964)
|
||||
(5, 7, 29, 24)
|
||||
>>> time_conversion(185683)
|
||||
(2, 3, 34, 43)
|
||||
>>> time_conversion(76896)
|
||||
(0, 21, 21, 36)
|
||||
>>> time_conversion(7896543)
|
||||
(91, 9, 29, 3)
|
||||
>>> time_conversion(1000000)
|
||||
(11, 13, 46, 40)
|
||||
>>> time_conversion(256)
|
||||
(0, 0, 4, 16)
|
||||
"""
|
||||
days = sec // 86400
|
||||
sec = sec % 86400
|
||||
hours = sec // 3600
|
||||
sec = sec % 3600
|
||||
minutes = sec // 60
|
||||
seconds = sec % 60
|
||||
|
||||
return days, hours, minutes, seconds
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
seconds = 256 #assign the input here
|
||||
print(f"{time_conversion(seconds)[0]} days {time_conversion(seconds)[1]} hours\
|
||||
{time_conversion(seconds)[2]} minutes {time_conversion(seconds)[3]} seconds")
|
Loading…
Reference in New Issue
Block a user