formatted 'time' data in calculations

Chris Rebert clp2 at rebertia.com
Wed Jan 7 12:50:32 EST 2009


On Wed, Jan 7, 2009 at 9:35 AM, Ross <nospam at forme.thks> 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)

Use the `datetime` class in the `datetime` module
(http://docs.python.org/library/datetime.html). It has a class method
.strptime() to parse a string into a `datetime` object. You can then
subtract one `datetime` from another to produce a `timedelta` object
representing the difference between them.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list