Python humanize time-interval without Arrow or Humanize libraries

tl;dr Sometimes when we need to debug functions in Python, we need a way to write some quick timer code to capture the time-delta and to compute the time it took for the function to execute. This article shows you a quick and easy way to humanize a time-interval/time-delta without bringing in additional dependencies or libraries like Arrow or Humanize.

Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds 
Yucel Moral (@yucelmoran) at Unsplash

Yucel Moral (@yucelmoran) at Unsplash

Sometimes when we need to debug functions in Python, we need a way to write some quick timer code to capture the time-delta and to compute the time it took for the function to execute. As an example:

from datetime import datetime 
start = datetime.now()
# Then call some long running code or function here
end = datetime.now()
diff = end - start
print diff

And this gives you:

print diff
123 days, 16:48:22

Now the variable “diff” holds a value of type: timedelta (elapsed-time or time-interval in seconds) as shown with the Python type() function below:

print type(diff)
<class 'datetime.timedelta'>

To get it formatted into a human-readable friendly format, you can bring in a library such as Arrow or Humanize. There is nothing wrong with these libraries. In fact, they are two great libraries that I use frequently. But sometimes, you just need to display the time-interval or time-delta in a human readable format without brining in an additional library into the mix to display “Elapsed Time” in a friendly format like this:

Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds 

The snippet below will get you the results you need:

days = diff.days # Get Day 
hours,remainder = divmod(diff.seconds,3600) # Get Hour
minutes,seconds = divmod(remainder,60) # Get Minute & Second

print(f'Elapsed Time: {days} Days, {hours} Hours, {minutes} Minutes, {seconds} Seconds.') 

Sample Output:

Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds 

Full Working Example:
from datetime import datetime 
start = datetime.now()
# Then call some long running code or function here
end = datetime.now()
diff = end - start
print type(diff)
print diff
days = diff.days # Get Day 
hours,remainder = divmod(diff.seconds,3600) # Get Hour
minutes,seconds = divmod(remainder,60) # Get Minute & Second

print(f’Elapsed Time: {days} Days, {hours} Hours, {minutes} Minutes, {seconds} Seconds.’)


The trick to this example/implementation is to use the divmod function in Python. The divmod() function in python takes two numbers and returns a pair of numbers (tuple) consisting of their quotient and remainder.

Syntax:

divmod(x, y)
x and y : x is numerator and y is denominator
x and y must be non complex
Returns tuple (quotient, remainder)

Examples:

Input: x = 9, y = 3
Output: (3, 0)
# 3 is quotient, 0 is remainder
Input: x = 16, y = 3
Output:(5, 1)
# 5 is quotient, 1 is remainder

If you find this article useful, consider bookmarking, subscribing and/or sharing it on your favorite social channels so that others can also find and read these articles. I do this out of love and passion to share my ideas, thoughts, findings and knowledge with others. So the more you help me share, the more my content can reach others.

Thank you for helping spread the word.

Find your passion and inspiration today! And help someone else find theirs!