From db1613eca5f7471dd5dc2e657d7d567e0aaccc10 Mon Sep 17 00:00:00 2001 From: ducdetronquito Date: Sun, 24 Dec 2023 12:11:13 +0100 Subject: [PATCH] feat: Display next birthday --- src/lib.rs | 16 ++++++++++++++++ src/main.rs | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 408b44b..1e25342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,16 @@ impl Birthday { pub fn age(&self, today: NaiveDate) -> Option { today.years_since(self.date) } + + pub fn next(&self, today: NaiveDate) -> NaiveDate { + let birthday_this_year = self.date.with_year(today.year()).unwrap(); + let birthday_next_year = self.date.with_year(today.year() + 1).unwrap(); + if birthday_this_year > today { + birthday_this_year + } else { + birthday_next_year + } + } } fn to_timestamp(date: NaiveDate) -> i64 { @@ -69,3 +79,9 @@ pub fn get_birthdays_for_date(date: NaiveDate) -> Result> { .collect(); Ok(birthdays) } + +pub fn get_next_birthday(today: NaiveDate) -> Result> { + let mut birthdays = get_all_birthdays()?; + birthdays.sort_by_key(|birthday| birthday.next(today)); + Ok(birthdays.into_iter().next()) +} diff --git a/src/main.rs b/src/main.rs index 927c4ce..06f2494 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,14 @@ fn main() -> Result<()> { print_birthdays(birthdays); Ok(()) } - Command::Next {} => todo!(), + Command::Next {} => { + let today = Utc::now().date_naive(); + let maybe_birthday = birthday::get_next_birthday(today)?; + if let Some(birthday) = maybe_birthday { + print_birthdays(vec![birthday]); + } + Ok(()) + } Command::Search { .. } => todo!(), Command::Today {} => { let today = Utc::now().date_naive();