File size: 1,182 Bytes
5e08f26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | from datetime import date
from dateutil.relativedelta import relativedelta
from utils import format_result
def compare_ages(name1, dob1, name2, dob2):
"""
Compare the ages of two people.
Parameters
----------
name1 : str
First person's name.
dob1 : date
First person's date of birth.
name2 : str
Second person's name.
dob2 : date
Second person's date of birth.
Returns
-------
dict
Dictionary containing the comparison result.
"""
# Same birthday
if dob1 == dob2:
return {
"name": name1,
"other_name": name2,
"same_age": True
}
# Determine who is older
if dob1 < dob2:
relation = "older"
other_name = name2
difference = relativedelta(dob2, dob1)
else:
relation = "younger"
other_name = name2
difference = relativedelta(dob1, dob2)
return {
"name": name1,
"other_name": other_name,
"same_age": False,
"relation": relation,
"years": difference.years,
"months": difference.months,
"days": difference.days
}
|