Converting the dict
values to datetime.timedelta
values is trivial, after which you can easily add them.
>>> from datetime import timedelta
>>> timedelta(**a) + timedelta(**b)
datetime.timedelta(days=1, seconds=47100)
Converting the sum back to a dict
is not so trivial, but still easy.
>>> x = timedelta(**a) + timedelta(**b)
>>> dict(zip(("hours", "minutes"), divmod(int(x.total_seconds())//60, 60)))
{'hours': 37, 'minutes': 5}
- Converting the total number of seconds into minutes
- Convert the minutes into hours and minutes
- Zip with the strings
"hours"
and "minutes"
to create
a sequence of tuples suitable for passing to dict
.