daysbetweendates/
date.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
//! This module is making a Date type struct and its allows to change to next day
/// This function is return the year is leak or not
fn is_leak(year: usize) -> bool {
    if year % 400 == 0 {
        true
    } else if year % 100 == 0 {
        false
    } else {
        year % 4 == 0
    } 
}
/// Date type struct takes 4 argumant year, month, day and leak
/// ```rust
/// use daysbetweendates::date::Date;
/// ```
pub struct Date {
    pub year: usize,
    pub month: usize,
    pub day: usize,
    pub leak: bool,
}
/// Date have 5 function Date::now, Date::from, Date.next_day, Date.next_month, Date.next_year

impl Date {
    ///Date::from takes a array dd,mm,yyyy and returns a Date
    /// # Exemple
    /// ```rust
    /// use daysbetweendates::date::Date;
    /// let date = Date::from([1,1,1999]);
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// ```
    pub fn from(date: [usize; 3]) -> Self {
        Self {
            year: date[2],
            month: date[1],
            day: date[0],
            leak: is_leak(date[2]),
        }
    }
    /// This function takes Date and add 1 of the day and retruns Date
    /// # Exemple
    /// ```rust
    /// use daysbetweendates::date::Date;
    /// let mut date = Date::from([1,1,1999]);
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// date = date.next_day();
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// ```
    pub fn next_day(self) -> Self {
        Self {
            year: self.year,
            month: self.month,
            day: self.day + 1,
            leak: is_leak(self.year),
        }
    }
    /// This function takes Date, makes day 1, add 1 to month and return Date
    ///
    /// # Exemple
    /// ```rust
    /// use daysbetweendates::date::Date;
    /// let mut date = Date::from([1,1,1999]);
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// date = date.next_month();
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// ```
    pub fn next_month(self) -> Self {
        Self {
            year: self.year,
            month: self.month + 1,
            day: 1,
            leak: is_leak(self.year),
        }
    }
    /// This function takes Date, makes day, month 1, add 1 to year and return Date
    ///
    /// # Exemple
    /// ```rust
    /// use daysbetweendates::date::Date;
    /// let mut date = Date::from([1,1,1999]);
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// date = date.next_year();
    /// println!("{}/{}/{}",date.day, date.month, date.year);
    /// ```
    pub fn next_year(self) -> Self {
        Self {
            year: self.year + 1,
            month: 1,
            day: 1,
            leak: is_leak(self.year),
        }
    }
}

/// Next function is for the decides next day, next month, next year Takes a Date returns a date
///
/// /// # Exemple
/// ```rust
/// use daysbetweendates::date::{Date, next};
/// let mut date = Date::from([31,12,1999]);
/// let mut counter = 0;
/// println!("{}/{}/{}",date.day, date.month, date.year);
/// (date, counter) = next(date, counter);
/// println!("{}/{}/{} -> {}",date.day, date.month, date.year, counter);
/// ```
pub fn next(date: Date, counter: i64) -> (Date, i64) {
    let mut _month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if !date.leak {
        _month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    } else {
        _month_tup = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    }
    if date.day < _month_tup[date.month - 1] {
        (date.next_day(), counter + 1)
    } else if date.month < 12 {
        (date.next_month(), counter + 1)
    } else {
        (date.next_year(), counter + 1)
    }
}