formatted 'time' data in calculations

Diez B. Roggisch deets at nospam.web.de
Wed Jan 7 12:49:10 EST 2009


Ross wrote:

> There seems to be no shortage of information around on how to use the
> time module, for example to use time.ctime() and push it into strftime
> and get something nice out the other side, but I haven't found anything
> helpful in going the other way.
> 
> That is, given some formatted text describing times - is there something
> that makes it easy to calculate time differences, or do I have to index
> my way through the string pulling out characters, converting to integers
> etc...
> 
> Data is formatted:
> 
>     t1 = 09:12:10
>     t2 = 11:22:14
> 
> I want to calculate tdiff = t2-t1
> 
> Any suggestions? (Thanks for anything you can offer)

The datetime module contains everything you need. Look at the
strptime-function that will allow you to parse the above string to an
actual datetime.time-object, and the you can subtract these to yield a
datetime.timedelta-object.

Diez



More information about the Python-list mailing list