-
-
Save mwoolweaver/1eb76fbe26d1cda45cd4b7d39cb762a1 to your computer and use it in GitHub Desktop.
print a pretty time delta in Python in days, hours, minutes and seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Byte-compiled / optimized / DLL files / pip / IDE | |
__pycache__/ | |
*.py[cod] | |
*$py.class | |
src/ | |
.idea/ | |
.vscode/ | |
# Config | |
config.ini | |
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
elif minutes > 0: | |
return '%s%dm%ds' % (sign_string, minutes, seconds) | |
else: | |
return '%s%ds' % (sign_string, seconds) | |
# I needed to print time deltas in a compact hours, minutes seconds format for log output. | |
# As an example try these on the Python console: | |
# >>> pretty_time_delta(123) | |
# '2m3s' | |
# >>> pretty_time_delta(12345) | |
# '3h25m45s' | |
# >>> pretty_time_delta(1234567) | |
# '14d6h56m7s' | |
# It only displays to second precision and only displays up to hours as the largest unit. | |
# To convert a standard Python [`datetime.timedelta`](http://docs.python.org/3/library/datetime.html#datetime.timedelta), use the [`total_seconds()`](http://docs.python.org/3/library/datetime.html#datetime.timedelta.total_seconds) method on your `timedelta` to convert to seconds. | |
# If all your calls come from standard Python `datetime` operations, you probably want to change line 3 to something like: | |
# seconds = abs(int(seconds.total_seconds())) | |
# The built-in string representation of a `timedelta` is also quite acceptable for most situations and also handles sub-second precision (see the last example): | |
# >>> import datetime | |
# >>> str(datetime.timedelta(seconds=123)) | |
# '0:02:03' | |
# >>> str(datetime.timedelta(seconds=12345)) | |
# '3:25:45' | |
# >>> str(datetime.timedelta(seconds=1234567)) | |
# '14 days, 6:56:07' | |
# >>> str(datetime.timedelta(seconds=123.456789)) | |
# '0:02:03.456789' | |
# The version included in `python-pretty-time-delta-positive-only.py` has the negative number handling stripped out for some slightly simpler code. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment