8.11. Builtin Divmod

8.11.1. Recap

>>> 10 / 8
1.25

8.11.2. Problem

>>> 10 // 8
1
>>> 10 % 8
2

8.11.3. Solution

>>> divmod(10, 8)
(1, 2)

8.11.4. Case Study

birthdate = date(2000, 1, 2)
today = date.today()

SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
YEAR = 365.25 * DAY
MONTH = YEAR / 12

td = today - birthdate

td.total_seconds()
778118400.0

seconds = td.total_seconds()
years, seconds = divmod(seconds, YEAR)
months, seconds = divmod(seconds, MONTH)
days, seconds = divmod(seconds, DAY)
hours, seconds = divmod(seconds, HOUR)
minutes, seconds = divmod(seconds, MINUTE)

result = {
 'year': int(years),
 'months': int(months),
 'days': int(days),
 'hours': int(hours),
 'minutes': int(minutes),
 'seconds': int(seconds),
}

result
# {'year': 24, 'months': 7, 'days': 26, 'hours': 22, 'minutes': 30, 'seconds': 0}