Skip to content

Commit

Permalink
feat: Display next birthday
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 24, 2023
1 parent 83dbd39 commit db1613e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ impl Birthday {
pub fn age(&self, today: NaiveDate) -> Option<u32> {
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 {
Expand Down Expand Up @@ -69,3 +79,9 @@ pub fn get_birthdays_for_date(date: NaiveDate) -> Result<Vec<Birthday>> {
.collect();
Ok(birthdays)
}

pub fn get_next_birthday(today: NaiveDate) -> Result<Option<Birthday>> {
let mut birthdays = get_all_birthdays()?;
birthdays.sort_by_key(|birthday| birthday.next(today));
Ok(birthdays.into_iter().next())
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit db1613e

Please sign in to comment.