From f18c725221ae20887c3d2dcf9f39a7294beac7c0 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 7 Oct 2023 08:18:52 +0100 Subject: [PATCH] Add upcoming birthdays Closes: #1 --- agenda/__init__.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/agenda/__init__.py b/agenda/__init__.py index 0db5237..b63e609 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -265,6 +265,37 @@ def get_us_holiday(input_date: date) -> dict[str, date | str]: return {"date": next_hol[0], "title": next_hol[1]} +def next_birthday(from_date: date, birth_date: date) -> dict[str, int | date]: + """Calculate the date of the next birthday based on a given birth date.""" + next_birthday_date = birth_date.replace(year=from_date.year) + + if from_date > next_birthday_date: + next_birthday_date = birth_date.replace(year=from_date.year + 1) + + age_at_next_birthday = next_birthday_date.year - birth_date.year + + return {"next_birthday": next_birthday_date, "age": age_at_next_birthday} + + +def get_birthdays( + from_date: date, config: configparser.ConfigParser +) -> list[dict[str, date | str]]: + if "birthdays" not in config: + return [] + events = [] + for name, date_str in config["birthdays"].items(): + bday = next_birthday(from_date, datetime.strptime(date_str, "%Y-%m-%d").date()) + events.append( + { + "date": bday["next_birthday"], + "name": "birthday", + "title": f"{name} (aged {bday['age']})", + } + ) + + return events + + def get_data(now: datetime) -> dict[str, str | object]: """Get data to display on agenda dashboard.""" rocket_dir = os.path.join(data_dir, "thespacedevs") @@ -305,6 +336,8 @@ def get_data(now: datetime) -> dict[str, str | object]: for key, value in xmas_last_posting_dates.items(): events.append({"name": f"xmas_last_{key}", "date": value}) + events += get_birthdays(today, config) + next_up_series = { "date": date(2026, 6, 1), "title": "70 Up",