Using seconds directly with relativedelta does not populate the months and years fields and there is a reason of course that we wouldn't know if it's a leap year or a month with 31 days.
So something like this:
[In]: tdelta = datetime.now() - datetime(1971, 1, 1)
[In]: relativedelta(seconds=tdelta.total_seconds())
[Out]: relativedelta(days=+16958, hours=+13, minutes=+19, seconds=+49)
gives relative delta with lots of days and no months. While this can be okay in certain cases, in some we might need the years and months. Therefore:
def timedelta_to_relativedelta(tdelta):
assert isinstance(tdelta, timedelta)
seconds_in = {
'year' : 365 * 24 * 60 * 60,
'month' : 30 * 24 * 60 * 60,
'day' : 24 * 60 * 60,
'hour' : 60 * 60,
'minute': 60
}
years, rem = divmod(tdelta.total_seconds(), seconds_in['year'])
months, rem = divmod(rem, seconds_in['month'])
days, rem = divmod(rem, seconds_in['day'])
hours, rem = divmod(rem, seconds_in['hour'])
minutes, rem = divmod(rem, seconds_in['minute'])
seconds = rem
return relativedelta(years=years, months=months, days=days, hours=hours, minutes=minutes, seconds=seconds)
This might not be a very Pythonic solution but it works.
Note: This assumes that a year has 365 days (ignores leap non leap years) and months have 30 days.