Aftab Naveed
Aftab Naveed

Reputation: 3879

Measuring Elapsed time using Python 2.7 time module

I am trying to find the difference between two times using time module and then convert it to datetime format using strftime but I get a strange output for elapsed time starting from 1970, what is the best way to find elapsed time using python?

start_time = time()
info('My Start Time ' + datetime.fromtimestamp(start_time).strftime('%d-%m-%Y %H:%M:%S'))
sleep(5)
elapsed_time = time() - start_time
info('My Elapsed Time' + datetime.fromtimestamp(elapsed_time).strftime('%d-%m-%Y %H:%M:%S'))

Output is

 [2016-03-17 00:45:16.828277] INFO: Generic: My Start Time 17-03-2016 11:45:16 
 [2016-03-17 00:45:21.832503] INFO: Generic: My Elapsed Time 01-01-1970 10:00:05

Upvotes: 4

Views: 8571

Answers (3)

PM 2Ring
PM 2Ring

Reputation: 55499

Time intervals are different to times. It doesn't really make sense to convert a time interval like elapsed_time to a datetime.

Your script can be simplified considerably by using the datetime module's datetime and timedelta objects. When you subtract one datetime from another the result is a timedelta.

from time import sleep
from datetime import datetime

start_time = datetime.now()
print('My Start Time', start_time)
sleep(5)

stop_time = datetime.now()
print('My Stop Time', stop_time)

elapsed_time = stop_time - start_time
print('My Elapsed Time', elapsed_time)    

output

My Start Time 2016-03-17 12:28:01.262090
My Stop Time 2016-03-17 12:28:06.265964
My Elapsed Time 0:00:05.003874

Please see the docs for timedelta to see more examples of what you can do with these objects.

Upvotes: 9

Brandon
Brandon

Reputation: 50

Instead of making your final timestamp from "elapsed time", you might want to do "start time" + "elapsed time" :)

start_time = time()
info('My Start Time ' + datetime.fromtimestamp(start_time).strftime('%d-%m-%Y %H:%M:%S'))
sleep(5)
elapsed_time = time() - start_time
info('My Elapsed Time' + datetime.fromtimestamp(elapsed_time + start_time).strftime('%d-%m-%Y %H:%M:%S'))

This is because Python's Time module's epoch is January 1, 1970. Calling time() measures the seconds since the epoch (.strftime('%d-%m-%Y %H:%M:%S') just reformats the seconds to a date we can understand)

So when you find elapsed_time, you end up with seconds between when you first started and the time you assigned the variable. Thus, it makes sense for datetime.fromtimestamp(elapsed_time) to output Jan 1, 1970 10:00:05. Where elapsed_time is 5, start_time is 1458177108.6...You want the result to be start_time PLUS elapsed_time (or, simply, why not just the current time?)

I hope this explanation made sense. Here's more explanation from the Python docs https://docs.python.org/2/library/time.html

Upvotes: -1

bunji
bunji

Reputation: 5223

This is one of my favourite things! Your problem is that, as far as the time module is concerned... time began on January 1st 1970!! See the docs here.
Time starts counting from this date so what you are basically saying when you convert your elapsed time to a date, is give me 01/01/1970 + my elapsed time.

Besides I'm not sure if taking the elapsed time as a date is really what you want. Don't you want to measure this in hours, minutes, etc. ? If you really do want the date, you should just call new_time = time.time() and then convert new_time to the format you want (without bothering to calculate elapsed time as a date)

Upvotes: 0

Related Questions