Thursday, February 17, 2011


Time delta in Python

Obviously, Python is a very nice and intuitive language (some subtle problems like described here notwithstanding), but one thing which always confused me immensely is time management in Python, for example relations between datetime module, time module and date object.

By way of example, if you want to measure time delta between two events, there are two ways to do it. Run an interactive Python session and try it yourself:

Unix% python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time, datetime
>>> x = time.time(); y =
>>> time.time() - x;  ( - y).seconds + ( - y).microseconds/1000000.0

(In Python 2.7 they at least added method timedelta.total_seconds(), making it slightly more consistent).

Labels: ,

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?