1185. Day of the Week

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

 

Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

 

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.

1185. Day of the Week
struct Solution;

impl Solution {
    fn day_of_the_week(day: i32, mut month: i32, mut year: i32) -> String {
        let days = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
        ];
        if month < 3 {
            month += 12;
            year -= 1;
        }
        let week =
            (day + ((month + 1) * 26) / 10 + year + year / 4 + 6 * (year / 100) + year / 400 + 5)
                % 7
                + 1;
        days[week as usize % 7].to_string()
    }
}

#[test]
fn test() {
    let day = 31;
    let month = 8;
    let year = 2019;
    let res = "Saturday".to_string();
    assert_eq!(Solution::day_of_the_week(day, month, year), res);
    let day = 18;
    let month = 7;
    let year = 1999;
    let res = "Sunday".to_string();
    assert_eq!(Solution::day_of_the_week(day, month, year), res);
    let day = 15;
    let month = 8;
    let year = 1993;
    let res = "Sunday".to_string();
    assert_eq!(Solution::day_of_the_week(day, month, year), res);
}