Skip to content

Instantly share code, notes, and snippets.

View isakrs's full-sized avatar

Isak Rathe Stoere isakrs

  • Yara International ASA
View GitHub Profile
@thatalextaylor
thatalextaylor / 1-python-pretty-time-delta.py
Last active November 5, 2023 22:48
Pretty print a time delta in Python in days, hours, minutes and seconds
def pretty_time_delta(seconds):
sign_string = '-' if seconds < 0 else ''
seconds = abs(int(seconds))
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
if days > 0:
return '%s%dd%dh%dm%ds' % (sign_string, days, hours, minutes, seconds)
elif hours > 0:
return '%s%dh%dm%ds' % (sign_string, hours, minutes, seconds)