Skip to content

Instantly share code, notes, and snippets.

@homebysix
Created March 16, 2021 19:12
Show Gist options
  • Save homebysix/c146214011e9ce64337b8140c5da65ed to your computer and use it in GitHub Desktop.
Save homebysix/c146214011e9ce64337b8140c5da65ed to your computer and use it in GitHub Desktop.
def readable_time(seconds):
"""Converts a number of seconds to a human-readable time in seconds, minutes, and hours."""
parts = []
if seconds >= 86400: # 1 day
days = seconds // 86400
if days == 1:
parts.append("{} day".format(int(days)))
else:
parts.append("{} days".format(int(days)))
if seconds >= 3600: # 1 hour
hours = seconds // 3600 % 24
if hours == 1:
parts.append("{} hour".format(int(hours)))
else:
parts.append("{} hours".format(int(hours)))
if seconds >= 60: # 1 hour
minutes = seconds // 60 % 60
if minutes == 1:
parts.append("{} minute".format(int(minutes)))
else:
parts.append("{} minutes".format(int(minutes)))
seconds = round(seconds % 60, 2)
if seconds == 1:
parts.append("{} second".format(seconds))
else:
parts.append("{} seconds".format(seconds))
return ", ".join(parts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment