Skip to content

Instantly share code, notes, and snippets.

@yassineAlouini
Created July 14, 2017 06:42
Show Gist options
  • Save yassineAlouini/3dd9f60fc37d1bbcef0e6a61677288fd to your computer and use it in GitHub Desktop.
Save yassineAlouini/3dd9f60fc37d1bbcef0e6a61677288fd to your computer and use it in GitHub Desktop.
Format seconds to human-readable time.
# This function is extracted from this file: https://github.com/dask/dask/blob/master/dask/diagnostics/progress.py
def format_time(t):
"""Format seconds into a human readable form.
>>> format_time(10.4)
'10.4s'
>>> format_time(1000.4)
'16min 40.4s'
"""
m, s = divmod(t, 60)
h, m = divmod(m, 60)
if h:
return '{0:2.0f}hr {1:2.0f}min {2:4.1f}s'.format(h, m, s)
elif m:
return '{0:2.0f}min {1:4.1f}s'.format(m, s)
else:
return '{0:4.1f}s'.format(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment