summing datetime.timedelta objects in Python

Argh! The following snippet is totally broken:

import datetime 

times = [datetime.timedelta(hours=1), datetime.timedelta(minutes=30)]
print sum(times)
print min(times)
print max(times)

It raises “TypeError: unsupported operand type(s) for +: ‘int’ and ‘datetime.timedelta’”

The thing is, I want this function to be able to sum both a list of floats, integers, or timedelta objects.  Other than doing a bunch of type checking, or writing my own sum method, is there some easy way to do this that I’m missing? I guess I could write:

print datetime.timedelta(milliseconds=sum([d.milliseconds for d in times]))

Yuk.  That’s not really type safe either, since I’d have to check the type of times[0] before I started.

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>