daysbetweendates/date.rs
1//! This module is making a Date type struct and its allows to change to next day
2/// This function is return the year is leak or not
3fn is_leak(year: usize) -> bool {
4 if year % 400 == 0 {
5 true
6 } else if year % 100 == 0 {
7 false
8 } else {
9 year % 4 == 0
10 }
11}
12/// Date type struct takes 4 argumant year, month, day and leak
13/// ```rust
14/// use daysbetweendates::date::Date;
15/// ```
16pub struct Date {
17 pub year: usize,
18 pub month: usize,
19 pub day: usize,
20 pub leak: bool,
21}
22/// Date have 5 function Date::now, Date::from, Date.next_day, Date.next_month, Date.next_year
23
24impl Date {
25 ///Date::from takes a array dd,mm,yyyy and returns a Date
26 /// # Exemple
27 /// ```rust
28 /// use daysbetweendates::date::Date;
29 /// let date = Date::from([1,1,1999]);
30 /// println!("{}/{}/{}",date.day, date.month, date.year);
31 /// ```
32 pub fn from(date: [usize; 3]) -> Self {
33 Self {
34 year: date[2],
35 month: date[1],
36 day: date[0],
37 leak: is_leak(date[2]),
38 }
39 }
40 /// This function takes Date and add 1 of the day and retruns Date
41 /// # Exemple
42 /// ```rust
43 /// use daysbetweendates::date::Date;
44 /// let mut date = Date::from([1,1,1999]);
45 /// println!("{}/{}/{}",date.day, date.month, date.year);
46 /// date = date.next_day();
47 /// println!("{}/{}/{}",date.day, date.month, date.year);
48 /// ```
49 pub fn next_day(self) -> Self {
50 Self {
51 year: self.year,
52 month: self.month,
53 day: self.day + 1,
54 leak: is_leak(self.year),
55 }
56 }
57 /// This function takes Date, makes day 1, add 1 to month and return Date
58 ///
59 /// # Exemple
60 /// ```rust
61 /// use daysbetweendates::date::Date;
62 /// let mut date = Date::from([1,1,1999]);
63 /// println!("{}/{}/{}",date.day, date.month, date.year);
64 /// date = date.next_month();
65 /// println!("{}/{}/{}",date.day, date.month, date.year);
66 /// ```
67 pub fn next_month(self) -> Self {
68 Self {
69 year: self.year,
70 month: self.month + 1,
71 day: 1,
72 leak: is_leak(self.year),
73 }
74 }
75 /// This function takes Date, makes day, month 1, add 1 to year and return Date
76 ///
77 /// # Exemple
78 /// ```rust
79 /// use daysbetweendates::date::Date;
80 /// let mut date = Date::from([1,1,1999]);
81 /// println!("{}/{}/{}",date.day, date.month, date.year);
82 /// date = date.next_year();
83 /// println!("{}/{}/{}",date.day, date.month, date.year);
84 /// ```
85 pub fn next_year(self) -> Self {
86 Self {
87 year: self.year + 1,
88 month: 1,
89 day: 1,
90 leak: is_leak(self.year),
91 }
92 }
93}
94
95/// Next function is for the decides next day, next month, next year Takes a Date returns a date
96///
97/// /// # Exemple
98/// ```rust
99/// use daysbetweendates::date::{Date, next};
100/// let mut date = Date::from([31,12,1999]);
101/// let mut counter = 0;
102/// println!("{}/{}/{}",date.day, date.month, date.year);
103/// (date, counter) = next(date, counter);
104/// println!("{}/{}/{} -> {}",date.day, date.month, date.year, counter);
105/// ```
106pub fn next(date: Date, counter: i64) -> (Date, i64) {
107 let mut _month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
108 if !date.leak {
109 _month_tup = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
110 } else {
111 _month_tup = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
112 }
113 if date.day < _month_tup[date.month - 1] {
114 (date.next_day(), counter + 1)
115 } else if date.month < 12 {
116 (date.next_month(), counter + 1)
117 } else {
118 (date.next_year(), counter + 1)
119 }
120}