-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday-of-the-week.rs
46 lines (38 loc) · 1.12 KB
/
day-of-the-week.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn day_of_the_week(day: i32, month: i32, year: i32) -> String {
let mut start = 3i32;
for i in 1970..year {
start += 365;
if i % 4 == 0 {
if i % 100 == 0 && i % 400 != 0 {
continue;
}
start += 1;
}
}
const MONTH: [i32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
for i in 1..month {
start += MONTH[i as usize - 1];
if i == 2 && year % 4 == 0 {
if year % 100 == 0 && year % 400 != 0 {
continue;
}
start += 1;
}
}
start += day;
match start % 7 {
0 => "Sunday".into(),
1 => "Monday".into(),
2 => "Tuesday".into(),
3 => "Wednesday".into(),
4 => "Thursday".into(),
5 => "Friday".into(),
6 => "Saturday".into(),
_ => unreachable!(),
}
}
}