Once you have a datetime
and a timedelta
, adding them is as simple as using the +
operator:
>>> dt = datetime.datetime(1945, 1, 3, 11, 45, 0, 44000)
>>> td = datetime.timedelta(hours=0, minutes=7, seconds=38.701)
>>> dt + td
datetime.datetime(1945, 1, 3, 11, 52, 38, 745000)
There is no built-in parser for timedelta
objects akin to strptime
for datetime
objects.
You can sometimes fake it by actually parsing the string as a datetime
, then extracting the fields to build a timedelta
, but this is both hacky and complicated—and, for your simple format, you can write one trivially:
def strpdelta(s):
hr, min, sec = map(float, s.split(':'))
return datetime.timedelta(hours=hr, minutes=min, seconds=sec)