Handle birthdays where the year isn't known
This commit is contained in:
parent
68b34f7973
commit
8cc8e3f9d3
|
@ -6,15 +6,20 @@ import yaml
|
||||||
|
|
||||||
from .types import Event
|
from .types import Event
|
||||||
|
|
||||||
|
YEAR_NOT_KNOWN = 1900
|
||||||
|
|
||||||
def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]:
|
|
||||||
|
def next_birthday(from_date: date, birth_date: date) -> tuple[date, int | None]:
|
||||||
"""Calculate the date of the next birthday based on a given birth date."""
|
"""Calculate the date of the next birthday based on a given birth date."""
|
||||||
next_birthday_date = birth_date.replace(year=from_date.year)
|
next_birthday_date = birth_date.replace(year=from_date.year)
|
||||||
|
|
||||||
if from_date > next_birthday_date:
|
if from_date > next_birthday_date:
|
||||||
next_birthday_date = birth_date.replace(year=from_date.year + 1)
|
next_birthday_date = birth_date.replace(year=from_date.year + 1)
|
||||||
|
|
||||||
age_at_next_birthday = next_birthday_date.year - birth_date.year
|
if birth_date.year != YEAR_NOT_KNOWN:
|
||||||
|
age_at_next_birthday = next_birthday_date.year - birth_date.year
|
||||||
|
else:
|
||||||
|
age_at_next_birthday = None
|
||||||
|
|
||||||
return next_birthday_date, age_at_next_birthday
|
return next_birthday_date, age_at_next_birthday
|
||||||
|
|
||||||
|
@ -26,14 +31,18 @@ def get_birthdays(from_date: date, filepath: str) -> list[Event]:
|
||||||
entities = yaml.safe_load(f)
|
entities = yaml.safe_load(f)
|
||||||
|
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
birthday = date(**entity["birthday"])
|
if "year" in entity["birthday"]:
|
||||||
|
birthday = date(**entity["birthday"])
|
||||||
|
else:
|
||||||
|
birthday = date(year=YEAR_NOT_KNOWN, **entity["birthday"])
|
||||||
bday, age = next_birthday(from_date, birthday)
|
bday, age = next_birthday(from_date, birthday)
|
||||||
for offset in range(2):
|
for offset in range(2):
|
||||||
|
display_age = f"aged {age + offset}" if age is not None else "age unknown"
|
||||||
events.append(
|
events.append(
|
||||||
Event(
|
Event(
|
||||||
date=bday.replace(year=bday.year + offset),
|
date=bday.replace(year=bday.year + offset),
|
||||||
name="birthday",
|
name="birthday",
|
||||||
title=f'🎈 {entity["label"]} (aged {age + offset})',
|
title=f'🎈 {entity["label"]} ({display_age})',
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue