Skip to content

Instantly share code, notes, and snippets.

@vkbo
Created October 24, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkbo/a61a2c2f22b65ec209b9b1025667faa8 to your computer and use it in GitHub Desktop.
Save vkbo/a61a2c2f22b65ec209b9b1025667faa8 to your computer and use it in GitHub Desktop.
A simple and fast Python 3.6+ function for formatting an integer representing time in seconds to HH:MM:SS or d-HH:MM:SS format.
def formatTime(tS):
"""Format a time in seconds in HH:MM:SS format or d-HH:MM:SS format
if a full day or longer.
"""
if isinstance(tS, int):
if tS >= 86400:
return f"{tS//86400:d}-{tS%86400//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
else:
return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
return "ERROR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment