| 1 | use super::{utils::pymod, yearinfo::YearInfo}; |
| 2 | use crate::{Frequency, NWeekday, RRule}; |
| 3 | |
| 4 | #[derive(Debug, Clone)] |
| 5 | pub(crate) struct MonthInfo { |
| 6 | pub last_year: i32, |
| 7 | pub last_month: u8, |
| 8 | pub neg_weekday_mask: Vec<u8>, |
| 9 | } |
| 10 | |
| 11 | impl MonthInfo { |
| 12 | pub fn new(year_info: &YearInfo, month: u8, rrule: &RRule) -> Self { |
| 13 | let neg_weekday_mask = Self::get_neg_weekday_mask(year_info, month, rrule); |
| 14 | Self { |
| 15 | last_year: year_info.year, |
| 16 | last_month: month, |
| 17 | neg_weekday_mask, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn get_neg_weekday_mask(year_info: &YearInfo, month: u8, rrule: &RRule) -> Vec<u8> { |
| 22 | let YearInfo { |
| 23 | year_len, |
| 24 | month_range, |
| 25 | weekday_mask, |
| 26 | .. |
| 27 | } = year_info; |
| 28 | |
| 29 | // Build up `ranges` |
| 30 | let mut ranges = vec![]; |
| 31 | if rrule.freq == Frequency::Yearly { |
| 32 | if rrule.by_month.is_empty() { |
| 33 | ranges.push((0, u32::from(*year_len) - 1)); |
| 34 | } else { |
| 35 | for month in &rrule.by_month { |
| 36 | let month = usize::from(*month); |
| 37 | let first = u32::from(month_range[month - 1]); |
| 38 | let last = u32::from(month_range[month]); |
| 39 | ranges.push((first, last - 1)); |
| 40 | } |
| 41 | } |
| 42 | } else if rrule.freq == Frequency::Monthly { |
| 43 | let month = usize::from(month); |
| 44 | let first = u32::from(month_range[month - 1]); |
| 45 | let last = u32::from(month_range[month]); |
| 46 | ranges.push((first, last - 1)); |
| 47 | } |
| 48 | |
| 49 | if ranges.is_empty() { |
| 50 | return vec![]; |
| 51 | } |
| 52 | |
| 53 | // Weekly frequency won't get here, so we may not |
| 54 | // care about cross-year weekly periods. |
| 55 | let mut neg_weekday_mask = vec![0; usize::from(*year_len)]; |
| 56 | |
| 57 | // Loop over `ranges` |
| 58 | for (first, last) in ranges { |
| 59 | for by_weekday in &rrule.by_weekday { |
| 60 | // Only check Nth occurrences here |
| 61 | if let NWeekday::Nth(number, weekday) = by_weekday { |
| 62 | let weekday = i16::try_from(weekday.num_days_from_monday()) |
| 63 | .expect("num_days_from_monday is between 0 and 6 which is covered by i16"); |
| 64 | let nth_weekday = if *number < 0 { |
| 65 | let number = match u32::try_from(-number) { |
| 66 | Ok(num) => num, |
| 67 | _ => continue, |
| 68 | }; |
| 69 | let nth_last_week = match last.checked_sub((number - 1) * 7) { |
| 70 | Some(val) => val, |
| 71 | None => continue, |
| 72 | }; |
| 73 | let index = match usize::try_from(nth_last_week) { |
| 74 | Ok(i) => i, |
| 75 | _ => continue, |
| 76 | }; |
| 77 | let nth_last_weekday = match weekday_mask.get(index) { |
| 78 | Some(val) => i16::try_from(*val).expect("values in weekday mask are all between 0-6 which is covered by i32"), |
| 79 | None => continue, |
| 80 | }; |
| 81 | |
| 82 | // Adjust to get the correct weekday |
| 83 | let modulo = u32::try_from(pymod(nth_last_weekday - weekday, 7)) |
| 84 | .expect("pymod is positive because 7 is the modulus"); |
| 85 | match nth_last_week.checked_sub(modulo) { |
| 86 | Some(val) => val, |
| 87 | None => continue, |
| 88 | } |
| 89 | } else { |
| 90 | let number = match u32::try_from(number - 1) { |
| 91 | Ok(num) => num, |
| 92 | _ => continue, |
| 93 | }; |
| 94 | let nth_first_day = first + number * 7; |
| 95 | let index = match usize::try_from(nth_first_day) { |
| 96 | Ok(i) => i, |
| 97 | _ => continue, |
| 98 | }; |
| 99 | let nth_first_day_weekday = match weekday_mask.get(index) { |
| 100 | Some(val) => i16::try_from(*val).expect("values in weekday mask are all between 0-6 which is covered by i32"), |
| 101 | None => continue, |
| 102 | }; |
| 103 | |
| 104 | // Adjust to get the correct weekday |
| 105 | let a = u32::try_from(7 - nth_first_day_weekday + weekday) |
| 106 | .expect("to be positive because nth_first_day_weekday is at most 6"); |
| 107 | nth_first_day + pymod(a, 7) |
| 108 | }; |
| 109 | if first <= nth_weekday && nth_weekday <= last { |
| 110 | if let Ok(nth_weekday) = usize::try_from(nth_weekday) { |
| 111 | neg_weekday_mask[nth_weekday] = 1; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | neg_weekday_mask |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[cfg(test)] |
| 123 | mod tests { |
| 124 | use chrono::{TimeZone, Weekday}; |
| 125 | |
| 126 | use crate::core::Tz; |
| 127 | |
| 128 | use super::*; |
| 129 | |
| 130 | const UTC: Tz = Tz::UTC; |
| 131 | |
| 132 | #[test] |
| 133 | fn get_neg_weekday_mask_with_daily_freq() { |
| 134 | let rrule = RRule { |
| 135 | freq: Frequency::Daily, |
| 136 | ..Default::default() |
| 137 | } |
| 138 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 0, 0, 0).unwrap()) |
| 139 | .unwrap(); |
| 140 | |
| 141 | let year_info = YearInfo::new(1997, &rrule); |
| 142 | |
| 143 | let neg_weekday_mask = MonthInfo::get_neg_weekday_mask(&year_info, 1, &rrule); |
| 144 | assert!(neg_weekday_mask.is_empty()); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn get_neg_weekday_mask_with_yearly_freq() { |
| 149 | let rrule = RRule { |
| 150 | freq: Frequency::Yearly, |
| 151 | ..Default::default() |
| 152 | } |
| 153 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 0, 0, 0).unwrap()) |
| 154 | .unwrap(); |
| 155 | |
| 156 | let year_info = YearInfo::new(1997, &rrule); |
| 157 | |
| 158 | let neg_weekday_mask = MonthInfo::get_neg_weekday_mask(&year_info, 1, &rrule); |
| 159 | assert_eq!(neg_weekday_mask.len(), year_info.year_len as usize); |
| 160 | assert!(neg_weekday_mask.into_iter().all(|val| val == 0)); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn get_neg_weekday_mask_with_yearly_freq_and_byweekday() { |
| 165 | let rrule = RRule { |
| 166 | freq: Frequency::Yearly, |
| 167 | by_weekday: vec![ |
| 168 | NWeekday::new(None, Weekday::Mon), |
| 169 | NWeekday::new(Some(-2), Weekday::Thu), |
| 170 | NWeekday::new(Some(1), Weekday::Thu), |
| 171 | ], |
| 172 | ..Default::default() |
| 173 | } |
| 174 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 0, 0, 0).unwrap()) |
| 175 | .unwrap(); |
| 176 | |
| 177 | let year_info = YearInfo::new(1997, &rrule); |
| 178 | |
| 179 | let neg_weekday_mask = MonthInfo::get_neg_weekday_mask(&year_info, 1, &rrule); |
| 180 | assert_eq!(neg_weekday_mask.len(), year_info.year_len as usize); |
| 181 | assert!(neg_weekday_mask |
| 182 | .into_iter() |
| 183 | .enumerate() |
| 184 | .all(|(idx, val)| match idx { |
| 185 | 1 | 351 => val == 1, |
| 186 | _ => val == 0, |
| 187 | })); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn get_neg_weekday_mask_with_monthly_freq_and_byweekday() { |
| 192 | let rrule = RRule { |
| 193 | freq: Frequency::Monthly, |
| 194 | by_weekday: vec![ |
| 195 | NWeekday::new(None, Weekday::Mon), |
| 196 | NWeekday::new(Some(-2), Weekday::Thu), |
| 197 | NWeekday::new(Some(1), Weekday::Thu), |
| 198 | ], |
| 199 | ..Default::default() |
| 200 | } |
| 201 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 0, 0, 0).unwrap()) |
| 202 | .unwrap(); |
| 203 | |
| 204 | let year_info = YearInfo::new(1997, &rrule); |
| 205 | |
| 206 | let neg_weekday_mask = MonthInfo::get_neg_weekday_mask(&year_info, 1, &rrule); |
| 207 | assert_eq!(neg_weekday_mask.len(), year_info.year_len as usize); |
| 208 | assert!(neg_weekday_mask |
| 209 | .into_iter() |
| 210 | .enumerate() |
| 211 | .all(|(idx, val)| match idx { |
| 212 | 1 | 22 => val == 1, |
| 213 | _ => val == 0, |
| 214 | })); |
| 215 | } |
| 216 | } |