Calculating Elapsed Time

Bengt Richter bokr at oz.net
Tue Dec 6 17:25:24 EST 2005


On Tue, 6 Dec 2005 12:36:55 -0800 (PST), Jean Johnson <jeanjohnson51 at yahoo.com> wrote:

>Hello - 
>
>I have a start and end time that is written using the
>following:
>
>time.strftime("%b %d %Y  %H:%M:%S")
>
>How do I calculate the elapsed time?
>
 >>> tf1 = time.strftime("%b %d %Y  %H:%M:%S")
 >>> tf1
 'Dec 06 2005  14:07:11'
 >>> tt1 = time.strptime(tf1, "%b %d %Y  %H:%M:%S" )
 >>> tt1
 (2005, 12, 6, 14, 7, 11, 1, 340, -1)
 >>> t1 = time.mktime(tt1)
 >>> t1
 1133906831.0
 >>> tf2 = time.strftime("%b %d %Y  %H:%M:%S")
 >>> tf2
 'Dec 06 2005  14:08:34'
 >>> tt2 = time.strptime(tf2, "%b %d %Y  %H:%M:%S" )
 >>> tt2
 (2005, 12, 6, 14, 8, 34, 1, 340, -1)
 >>> t2 = time.mktime(tt2)
 >>> t2
 1133906914.0
 >>> t2-t1
 83.0
(seconds elapsed)

Perhaps time was available as a number before stftime was used? E.g., t2 can round trip:
 >>> time.ctime(t2)
 'Tue Dec 06 14:08:34 2005'
 >>> time.strftime("%b %d %Y  %H:%M:%S", time.localtime(t2))
 'Dec 06 2005  14:08:34'
 >>> time.strftime("%b %d %Y  %H:%M:%S", time.gmtime(t2))
 'Dec 06 2005  22:08:34'


for more info, see time module docs at

    http://www.python.org/doc/current/lib/module-time.html

and in general, learn how to find info starting at

    http://www.python.org/doc/

also, interactively,
    import time
    help(time)

Regards,
Bengt Richter



More information about the Python-list mailing list