Date addition with time crate

Leap
Leap in Rust
use time::{Date, Duration, Month};

pub fn is_leap_year(year: u64) -> bool {
    (Date::from_calendar_date(year as i32, Month::February, 28).unwrap() + Duration::DAY).day()
        == 29
}
Caution

This approach may be considered a "cheat" for this exercise.

By adding a day to February 28th for the year, you can see if the new day is the 29th or the 1st. If it is the 29th, then the year is a leap year. This is done by adding a Duration::DAY to a time::Date struct and comparing it to 29 with the day method.

8th May 2024 · Found it useful?