Last active
November 6, 2023 18:10
-
-
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
This file contains hidden or 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_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)) |
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example outputs: