-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
44 lines (36 loc) · 940 Bytes
/
main.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
/*
Create a function that takes two dates and returns the number of days between the first and second date. - https://edabit.com/challenge/3hdXjfJozQySRC3gE
*/
use chrono::{Duration, Utc};
use chrono::prelude::*;
use std::cmp::Ordering;
fn get_days(x: Date<chrono::Utc>, y: Date<chrono::Utc>) -> Duration {
match x.cmp(&y) {
Ordering::Less => y - x,
Ordering::Greater => y - x,
Ordering::Equal => x - y,
}
}
fn main() {
assert_eq!(
get_days(
Utc.ymd(2019, 06, 14),
Utc.ymd(2019, 06, 20),
),
chrono::Duration::days(6)
);
assert_eq!(
get_days(
Utc.ymd(2018, 12, 29),
Utc.ymd(2019, 01, 01),
),
chrono::Duration::days(3)
);
assert_eq!(
get_days(
Utc.ymd(2019, 06, 20),
Utc.ymd(2019, 06, 30),
),
chrono::Duration::days(10)
);
}