Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active November 6, 2023 18:10
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 mikbuch/6678247656cd8eee6b70af9486db3f20 to your computer and use it in GitHub Desktop.
Save mikbuch/6678247656cd8eee6b70af9486db3f20 to your computer and use it in GitHub Desktop.
Pretty print a `datatime` time delta object in Python in days, hours, minutes, seconds, and milliseconds
def pretty_timedelta(delta):
"""
Pretty printing a `timedelta` object form `datetime` Python module
Acknowledgements:
@thatalextaylor for his earlier version:
https://gist.github.com/thatalextaylor/7408395
That I used to modify the script.
Args:
delta -- `datatime` Python time delta object
Returns:
None -- just a printing function -- works better with Jupyter Notebook
"""
timedelta_seconds = delta.total_seconds()
# Seconds will be int-s, timedelta_seconds stores also decimal places
seconds = timedelta_seconds
# Can be negative
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)
# Skipping seconds, as the next divmod is 1, so the
# seconds stay the same.
_, seconds_decimal = divmod(timedelta_seconds, 1)
milliseconds = '%.2f' % float(seconds_decimal*1000)
print('__________________________________')
print('Calculating the results has taken:')
if days > 0:
print(' %s %dd %dh %dm %ds' % (sign_string, days, hours, minutes, seconds))
elif hours > 0:
print(' %s %dh %dm %ds' % (sign_string, hours, minutes, seconds))
elif minutes > 0:
print(' %s %dm %ds' % (sign_string, minutes, seconds))
elif seconds > 0:
print(' %s %ds %sms' % (sign_string, seconds, milliseconds))
elif milliseconds > 0:
print(' %s %ds' % (sign_string, milliseconds))
import time
from datetime import datetime
# We need to know when the cell/calculation started execution
start_time = datetime.now()
datetime_display_format = "%Y-%m-%d %H:%M:%S"
'''
Your code goes here
'''
# Here we use a small pause to showcase the results
time.sleep(2.5)
# When the cell finished executing
end_time = datetime.now()
print('Calculation started at: ', start_time.strftime(datetime_display_format))
print('Calculation ended at: ', end_time.strftime(datetime_display_format))
# Difference between two timestamps
# in hours:minutes:seconds format
delta = end_time - start_time
# Output: pretty print the result
pretty_timedelta(delta)
@mikbuch
Copy link
Author

mikbuch commented Nov 6, 2023

Example outputs:

  • for 2.5 seconds:
Calculation started at:   2023-11-06 09:32:33
Calculation ended at:     2023-11-06 09:32:35
__________________________________
Calculating the results has taken:
  2s 502.75ms
  • for 95 seconds:
Calculation started at:   2023-11-06 10:05:33
Calculation ended at:     2023-11-06 10:07:08
__________________________________
Calculating the results has taken:
  1m 35s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment