| 1 | use chrono::{Datelike, Duration, NaiveDate, Weekday}; |
| 2 | |
| 3 | trait NaiveDateExt { |
| 4 | fn days_in_month(&self) -> i32; |
| 5 | fn is_leap_year(&self) -> bool; |
| 6 | } |
| 7 | |
| 8 | impl NaiveDateExt for chrono::NaiveDate { |
| 9 | fn days_in_month(&self) -> i32 { |
| 10 | let month = self.month(); |
| 11 | match month { |
| 12 | 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, |
| 13 | 4 | 6 | 9 | 11 => 30, |
| 14 | 2 => { |
| 15 | if self.is_leap_year() { |
| 16 | 29 |
| 17 | } else { |
| 18 | 28 |
| 19 | } |
| 20 | } |
| 21 | _ => panic!("Invalid month: {}", month), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn is_leap_year(&self) -> bool { |
| 26 | let year = self.year(); |
| 27 | return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub(crate) fn days_in_month(year: i32, month: u32, first_day: Weekday) -> Vec<Vec<NaiveDate>> { |
| 32 | let mut year = year; |
| 33 | let mut month = month; |
| 34 | if month > 12 { |
| 35 | year += 1; |
| 36 | month = 1; |
| 37 | } |
| 38 | if month < 1 { |
| 39 | year -= 1; |
| 40 | month = 12; |
| 41 | } |
| 42 | |
| 43 | let date = NaiveDate::from_ymd_opt(year, month, 1).unwrap(); |
| 44 | let num_days = date.days_in_month(); |
| 45 | let start_weekday = |
| 46 | (date.weekday().num_days_from_sunday() + 7 - first_day.num_days_from_sunday()) % 7; |
| 47 | |
| 48 | // Get the days in the month, 2023-02 will returns |
| 49 | // "29|30|31| 1| 2| 3| 4", |
| 50 | // " 5| 6| 7| 8| 9|10|11", |
| 51 | // "12|13|14|15|16|17|18", |
| 52 | // "19|20|21|22|23|24|25", |
| 53 | // "26|27|28| 1| 2| 3| 4", |
| 54 | let mut days = vec![]; |
| 55 | for n in 0..5 { |
| 56 | let mut week_days = vec![]; |
| 57 | for weekday in 0..7 { |
| 58 | let (mut y, mut m) = (year, month); |
| 59 | |
| 60 | // If the day is less than the start weekday, we need to go back to the previous month. |
| 61 | if n == 0 && weekday < start_weekday { |
| 62 | m = if m == 1 { 12 } else { m - 1 }; |
| 63 | y = if m == 1 { year - 1 } else { y }; |
| 64 | } |
| 65 | |
| 66 | // If start_weekday is 3, and n is 0 and weekday is 3, then day is 1. |
| 67 | // If start_weekday is 3, and n is 1 and weekday is 4, then day is 9. |
| 68 | let day = n * 7 + weekday as i32 - start_weekday as i32; |
| 69 | |
| 70 | // If the day is greater than the number of days in the month, we need to go to the next month. |
| 71 | if day > num_days { |
| 72 | m = if m == 12 { 1 } else { m + 1 }; |
| 73 | y = if m == 1 { year + 1 } else { y }; |
| 74 | } |
| 75 | |
| 76 | #[allow(clippy::expect_fun_call)] |
| 77 | let date = date |
| 78 | .checked_add_signed(Duration::days(day as i64)) |
| 79 | .expect(&format!("invalid date {}-{} days {}", y, m, day)); |
| 80 | week_days.push(date); |
| 81 | } |
| 82 | |
| 83 | days.push(week_days); |
| 84 | } |
| 85 | |
| 86 | days |
| 87 | } |
| 88 | |
| 89 | #[cfg(test)] |
| 90 | mod tests { |
| 91 | use chrono::{Datelike, NaiveDate, Weekday}; |
| 92 | |
| 93 | use super::{NaiveDateExt, days_in_month}; |
| 94 | |
| 95 | #[test] |
| 96 | fn test_days_in_month() { |
| 97 | assert_eq!( |
| 98 | NaiveDate::from_ymd_opt(2024, 2, 1).unwrap().days_in_month(), |
| 99 | 29 |
| 100 | ); |
| 101 | assert_eq!( |
| 102 | NaiveDate::from_ymd_opt(2023, 2, 1).unwrap().days_in_month(), |
| 103 | 28 |
| 104 | ); |
| 105 | assert_eq!( |
| 106 | NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().days_in_month(), |
| 107 | 31 |
| 108 | ); |
| 109 | assert_eq!( |
| 110 | NaiveDate::from_ymd_opt(2023, 4, 1).unwrap().days_in_month(), |
| 111 | 30 |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn test_days() { |
| 117 | #[track_caller] |
| 118 | fn assert_case(date: NaiveDate, first_day: Weekday, expected: Vec<&str>) { |
| 119 | let out = days_in_month(date.year(), date.month(), first_day) |
| 120 | .iter() |
| 121 | .map(|week| { |
| 122 | week.iter() |
| 123 | .map(|d| { |
| 124 | if d.year() == date.year() && d.month() == date.month() { |
| 125 | format!("{:2}", d.day()) |
| 126 | } else if d.year() == date.year() { |
| 127 | format!("{}-{}", d.month(), d.day()) |
| 128 | } else { |
| 129 | format!("{}-{}-{}", d.year(), d.month(), d.day()) |
| 130 | } |
| 131 | }) |
| 132 | .collect::<Vec<_>>() |
| 133 | .join("|") |
| 134 | }) |
| 135 | .collect::<Vec<_>>(); |
| 136 | |
| 137 | assert_eq!(out, expected); |
| 138 | } |
| 139 | |
| 140 | assert_case( |
| 141 | NaiveDate::from_ymd_opt(2024, 8, 1).unwrap(), |
| 142 | Weekday::Sun, |
| 143 | vec![ |
| 144 | "7-28|7-29|7-30|7-31| 1| 2| 3", |
| 145 | " 4| 5| 6| 7| 8| 9|10", |
| 146 | "11|12|13|14|15|16|17", |
| 147 | "18|19|20|21|22|23|24", |
| 148 | "25|26|27|28|29|30|31", |
| 149 | ], |
| 150 | ); |
| 151 | assert_case( |
| 152 | NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(), |
| 153 | Weekday::Sun, |
| 154 | vec![ |
| 155 | "2024-12-29|2024-12-30|2024-12-31| 1| 2| 3| 4", |
| 156 | " 5| 6| 7| 8| 9|10|11", |
| 157 | "12|13|14|15|16|17|18", |
| 158 | "19|20|21|22|23|24|25", |
| 159 | "26|27|28|29|30|31|2-1", |
| 160 | ], |
| 161 | ); |
| 162 | |
| 163 | assert_case( |
| 164 | NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(), |
| 165 | Weekday::Sun, |
| 166 | vec![ |
| 167 | "1-28|1-29|1-30|1-31| 1| 2| 3", |
| 168 | " 4| 5| 6| 7| 8| 9|10", |
| 169 | "11|12|13|14|15|16|17", |
| 170 | "18|19|20|21|22|23|24", |
| 171 | "25|26|27|28|29|3-1|3-2", |
| 172 | ], |
| 173 | ); |
| 174 | assert_case( |
| 175 | NaiveDate::from_ymd_opt(2023, 2, 20).unwrap(), |
| 176 | Weekday::Sun, |
| 177 | vec![ |
| 178 | "1-29|1-30|1-31| 1| 2| 3| 4", |
| 179 | " 5| 6| 7| 8| 9|10|11", |
| 180 | "12|13|14|15|16|17|18", |
| 181 | "19|20|21|22|23|24|25", |
| 182 | "26|27|28|3-1|3-2|3-3|3-4", |
| 183 | ], |
| 184 | ); |
| 185 | |
| 186 | // Monday-first week: Feb 2023 starts on a Wednesday, so the grid |
| 187 | // shifts to start on Monday (column 0 = Mon 30 Jan). |
| 188 | assert_case( |
| 189 | NaiveDate::from_ymd_opt(2023, 2, 1).unwrap(), |
| 190 | Weekday::Mon, |
| 191 | vec![ |
| 192 | "1-30|1-31| 1| 2| 3| 4| 5", |
| 193 | " 6| 7| 8| 9|10|11|12", |
| 194 | "13|14|15|16|17|18|19", |
| 195 | "20|21|22|23|24|25|26", |
| 196 | "27|28|3-1|3-2|3-3|3-4|3-5", |
| 197 | ], |
| 198 | ); |
| 199 | } |
| 200 | } |