Given the python code below, please help me understand what is happening there.
start_time = time.time()
time.sleep(42)
end_time = time.time()
uptime = end_time - start_time
human_uptime = str(datetime.timedelta(seconds=int(uptime)))
So I get the difference between start time and end time, on line 5 I round up the duration by casting and what now, what’s the further explanation?
I know what delta means(average or difference), but why do I have to pass seconds = uptime to timedelta and why does the string casting works so nicely that I get HH:MM:SS ?
Because timedelta is defined like:
All arguments are optional and default to 0.
You can easily say “Three days and four milliseconds” with optional arguments that way.
And for str casting, it returns a nice formatted value instead of
__repr__to improve readability. From docs:Checkout documentation:
http://docs.python.org/library/datetime.html#timedelta-objects