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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use jiff::civil;

use super::compute;
use crate::error::Error;

pub fn year(today: civil::Date) -> Result<f64, Error> {
    year_ratio(today)
}

fn year_ratio(today: civil::Date) -> Result<f64, Error> {
    let start = civil::date(today.year(), 1, 1);
    let end = civil::date(today.year() + 1, 1, 1);
    compute(today, start, end)
}

#[cfg(test)]
mod tests {
    use jiff::civil;

    use super::*;
    use anyhow::Result;

    #[test]
    fn year_should_be_0() -> Result<()> {
        // first day of the year
        let current = civil::date(2021, 1, 1);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.0);
        assert_eq!(ratio_int, 0);

        // first day of the year
        let current = civil::date(2022, 1, 1);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.0);
        assert_eq!(ratio_int, 0);

        // first day of the year
        let current = civil::date(2023, 1, 1);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.0);
        assert_eq!(ratio_int, 0);

        Ok(())
    }
    #[test]
    fn year_should_be_50() -> Result<()> {
        // middle day of the year
        let current = civil::date(2021, 7, 4);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.5041095890410959);
        assert_eq!(ratio_int, 50);

        // middle day of the year
        let current = civil::date(2022, 7, 3);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.5013698630136987);
        assert_eq!(ratio_int, 50);

        // middle day of the year
        let current = civil::date(2023, 7, 3);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.5013698630136987);
        assert_eq!(ratio_int, 50);

        Ok(())
    }
    #[test]
    fn year_should_be_100() -> Result<()> {
        // last day of the year
        let current = civil::date(2021, 12, 31);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.9972602739726028);
        assert_eq!(ratio_int, 99);

        // last day of the year
        let current = civil::date(2023, 12, 31);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.9972602739726028);
        assert_eq!(ratio_int, 99);

        // last day of the year
        let current = civil::date(2023, 12, 31);
        let ratio = year_ratio(current)?;
        let ratio_int = (ratio * 100.0) as i32;

        assert_eq!(ratio, 0.9972602739726028);
        assert_eq!(ratio_int, 99);

        Ok(())
    }
}