Python humanize time-interval without Arrow or Humanize libraries
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.
1 | Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds |
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:
1 2 3 4 5 6 | 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:
1 2 | 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:
1 2 | 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:
1 | Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds |
The snippet below will get you the results you need:
1 2 3 4 5 | 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:
1 | Elapsed Time: 2 Days, 5 Hours, 7 Minutes, 13 Seconds |
Full Working Example:
01 02 03 04 05 06 07 08 09 10 11 | 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:
1 2 3 4 | 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:
1 2 3 4 5 6 | 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!
Published on Web Code Geeks with permission by Venkatt Guhesan, partner at our WCG program. See the original article here: Python humanize time-interval without Arrow or Humanize libraries Opinions expressed by Web Code Geeks contributors are their own. |