Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Last active January 10, 2022 01:07
Show Gist options
  • Save juancarlospaco/d736a4fa5084a0a302e9 to your computer and use it in GitHub Desktop.
Save juancarlospaco/d736a4fa5084a0a302e9 to your computer and use it in GitHub Desktop.
Seconds positive integer to friendly human string with precision from seconds to days
def seconds_time_to_human_string(self, time_on_seconds=0):
"""Calculate time, with precision from seconds to days."""
minutes, seconds = divmod(int(time_on_seconds), 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
human_time_string = ""
if days:
human_time_string += "%02d Days " % days
if hours:
human_time_string += "%02d Hours " % hours
if minutes:
human_time_string += "%02d Minutes " % minutes
human_time_string += "%02d Seconds" % seconds
return human_time_string
@juancarlospaco
Copy link
Author

>>> def seconds_time_to_human_string(time_on_seconds=0):
...     """Calculate time, with precision from seconds to days."""
...     minutes, seconds = divmod(int(time_on_seconds), 60)
...     hours, minutes = divmod(minutes, 60)
...     days, hours = divmod(hours, 24)
...     human_time_string = ""
...     if days:
...         human_time_string += "%02d Days " % days
...     if hours:
...         human_time_string += "%02d Hours " % hours
...     if minutes:
...         human_time_string += "%02d Minutes " % minutes
...     human_time_string += "%02d Seconds" % seconds
...     return human_time_string
...
>>>

>>> seconds_time_to_human_string(666)
'11 Minutes 06 Seconds'

>>> seconds_time_to_human_string(2314125666)
'26783 Days 20 Hours 41 Minutes 06 Seconds'

>>> seconds_time_to_human_string(0)
'00 Seconds'

>>> seconds_time_to_human_string(999999999)
'11574 Days 01 Hours 46 Minutes 39 Seconds'

>>> seconds_time_to_human_string(str("42"))
'42 Seconds'

>>> seconds_time_to_human_string(42.75)
'42 Seconds'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment