| 1 | use super::{ |
| 2 | masks::MASKS, |
| 3 | utils::{days_since_unix_epoch, get_year_len, pymod}, |
| 4 | }; |
| 5 | use crate::RRule; |
| 6 | use chrono::{Datelike, TimeZone, Utc}; |
| 7 | |
| 8 | #[derive(Debug)] |
| 9 | pub(crate) struct BaseMasks { |
| 10 | month_mask: &'static [u8], |
| 11 | month_day_mask: &'static [i8], |
| 12 | neg_month_day_mask: &'static [i8], |
| 13 | month_range: &'static [u16], |
| 14 | weekday_mask: &'static [u32], |
| 15 | } |
| 16 | |
| 17 | fn base_year_masks(year_weekday: u16, year_len: u16) -> BaseMasks { |
| 18 | if year_len == 365 { |
| 19 | BaseMasks { |
| 20 | month_mask: &MASKS.month_365, |
| 21 | month_day_mask: &MASKS.month_day_365, |
| 22 | neg_month_day_mask: &MASKS.neg_month_day_365, |
| 23 | month_range: &MASKS.month_365_range, |
| 24 | weekday_mask: &MASKS.weekday[usize::from(year_weekday)..], |
| 25 | } |
| 26 | } else { |
| 27 | BaseMasks { |
| 28 | month_mask: &MASKS.month_366, |
| 29 | month_day_mask: &MASKS.month_day_366, |
| 30 | neg_month_day_mask: &MASKS.neg_month_day_366, |
| 31 | month_range: &MASKS.month_366_range, |
| 32 | weekday_mask: &MASKS.weekday[usize::from(year_weekday)..], |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone)] |
| 38 | pub(crate) struct YearInfo { |
| 39 | /// The year |
| 40 | pub year: i32, |
| 41 | /// Number of days in the current year (365 or 366) |
| 42 | pub year_len: u16, |
| 43 | /// Number of days in the next year (365 or 366) |
| 44 | pub next_year_len: u16, |
| 45 | /// Number of days since Unix epoch |
| 46 | pub year_ordinal: i64, |
| 47 | pub month_mask: &'static [u8], |
| 48 | pub month_day_mask: &'static [i8], |
| 49 | pub neg_month_day_mask: &'static [i8], |
| 50 | pub month_range: &'static [u16], |
| 51 | pub weekday_mask: &'static [u32], |
| 52 | /// Week number mask |
| 53 | pub week_no_mask: Option<Vec<u8>>, |
| 54 | } |
| 55 | |
| 56 | impl YearInfo { |
| 57 | pub fn new(year: i32, rrule: &RRule) -> Self { |
| 58 | // It should never fail, since there is always a 1st of January, is there? |
| 59 | let first_year_day = Utc.with_ymd_and_hms(year, 1, 1, 0, 0, 0).unwrap(); |
| 60 | |
| 61 | let year_len = get_year_len(year); |
| 62 | let next_year_len = get_year_len(year + 1); |
| 63 | let year_ordinal = days_since_unix_epoch(&first_year_day); |
| 64 | let year_start_weekday = u16::try_from(first_year_day.weekday().num_days_from_monday()) |
| 65 | .expect("num_days_from_monday is between 0 and 6 which is covered by u16"); |
| 66 | |
| 67 | let base_masks = base_year_masks(year_start_weekday, year_len); |
| 68 | |
| 69 | let mut result = Self { |
| 70 | year, |
| 71 | year_len, |
| 72 | next_year_len, |
| 73 | year_ordinal, |
| 74 | week_no_mask: None, |
| 75 | month_mask: base_masks.month_mask, |
| 76 | month_day_mask: base_masks.month_day_mask, |
| 77 | neg_month_day_mask: base_masks.neg_month_day_mask, |
| 78 | month_range: base_masks.month_range, |
| 79 | weekday_mask: base_masks.weekday_mask, |
| 80 | }; |
| 81 | |
| 82 | if rrule.by_week_no.is_empty() { |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | let mut week_no_mask = vec![0; usize::from(year_len) + 7]; |
| 87 | |
| 88 | let rrule_week_start = u16::try_from(rrule.week_start.num_days_from_monday()) |
| 89 | .expect("num_days_from_monday is between 0 and 6 which is covered by u16"); |
| 90 | let mut no1_week_start = pymod(7 - year_start_weekday + rrule_week_start, 7); |
| 91 | let first_week_start = no1_week_start; |
| 92 | let year_len_ext = if no1_week_start >= 4 { |
| 93 | no1_week_start = 0; |
| 94 | // Number of days in the year, plus the days we got |
| 95 | // from last year. |
| 96 | let diff = i32::from(year_start_weekday) - i32::from(rrule_week_start); |
| 97 | result.year_len |
| 98 | + u16::try_from(pymod(diff, 7)).expect("to be positive because 7 is the modulus") |
| 99 | } else { |
| 100 | // Number of days in the year, minus the days we |
| 101 | // left in last year. |
| 102 | year_len - no1_week_start |
| 103 | }; |
| 104 | |
| 105 | let div = year_len_ext / 7; |
| 106 | let year_mod = pymod(year_len_ext, 7); |
| 107 | let num_weeks = div + (year_mod / 4); |
| 108 | |
| 109 | for &(mut n) in &rrule.by_week_no { |
| 110 | let num_weeks = |
| 111 | i8::try_from(num_weeks).expect("num_weeks is 52-54 which is covered by i8::MAX"); |
| 112 | if n < 0 { |
| 113 | n += num_weeks + 1; |
| 114 | } |
| 115 | if !(n > 0 && n <= num_weeks) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | let i = if n > 1 { |
| 120 | let n = |
| 121 | u16::try_from(n).expect("We know that 1 < n < i8::MAX which is covered by u16"); |
| 122 | let mut i = no1_week_start + ((n - 1) * 7); |
| 123 | if no1_week_start != first_week_start { |
| 124 | i -= 7 - first_week_start; |
| 125 | } |
| 126 | i |
| 127 | } else { |
| 128 | no1_week_start |
| 129 | }; |
| 130 | |
| 131 | for j in i..i + 7 { |
| 132 | let j = usize::from(j); |
| 133 | week_no_mask[j] = 1; |
| 134 | if result.weekday_mask[j + 1] == rrule.week_start.num_days_from_monday() { |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if rrule.by_week_no.contains(&1) { |
| 141 | // Check week number 1 of next years as well |
| 142 | let mut i = no1_week_start + num_weeks * 7; |
| 143 | if no1_week_start != first_week_start { |
| 144 | i -= 7 - first_week_start; |
| 145 | } |
| 146 | if i < year_len { |
| 147 | // If week starts in next year, we |
| 148 | // don't care about it. |
| 149 | for j in i..i + 7 { |
| 150 | let j = usize::from(j); |
| 151 | week_no_mask[j] = 1; |
| 152 | if result.weekday_mask[j + 1] == rrule.week_start.num_days_from_monday() { |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if no1_week_start > 0 { |
| 160 | // Check last week number of last year as |
| 161 | // well. If no1_week_start is 0, either the year |
| 162 | // started on week start, or week number 1 |
| 163 | // got days from last year, so there are no |
| 164 | // days from last year's last week number in |
| 165 | // this year. |
| 166 | let l_num_weeks = if rrule.by_week_no.contains(&-1) { |
| 167 | -1 |
| 168 | } else { |
| 169 | let l_year_weekday = u16::try_from( |
| 170 | Utc.with_ymd_and_hms(year - 1, 1, 1, 0, 0, 0) |
| 171 | // It should never fail, since there is always a 1st of January, is there? |
| 172 | .unwrap() |
| 173 | .weekday() |
| 174 | .num_days_from_monday(), |
| 175 | ) |
| 176 | .expect("num_days_from_monday is between 0 and 6 which is covered by u16"); |
| 177 | |
| 178 | let rrule_week_start = u16::try_from(rrule.week_start.num_days_from_monday()) |
| 179 | .expect("num_days_from_monday is between 0 and 6 which is covered by u16"); |
| 180 | let ln_no1_week_start = pymod(7 - l_year_weekday + rrule_week_start, 7); |
| 181 | |
| 182 | let l_year_len = get_year_len(year - 1); |
| 183 | let week_start = if ln_no1_week_start >= 4 { |
| 184 | l_year_len |
| 185 | + u16::try_from(pymod( |
| 186 | i32::from(l_year_weekday) - i32::from(rrule_week_start), |
| 187 | 7, |
| 188 | )) |
| 189 | .expect("7 is the modulo, so the range is 0-6, and u16 covers that range") |
| 190 | } else { |
| 191 | year_len - no1_week_start |
| 192 | }; |
| 193 | |
| 194 | 52 + i8::try_from(pymod(week_start, 7) / 4) |
| 195 | .expect("7 is the modulo, so the range is 0-6, and i8 covers that range") |
| 196 | }; |
| 197 | |
| 198 | if rrule.by_week_no.contains(&l_num_weeks) { |
| 199 | for i in 0..no1_week_start { |
| 200 | let i = usize::from(i); |
| 201 | week_no_mask[i] = 1; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | result.week_no_mask = Some(week_no_mask); |
| 207 | |
| 208 | result |
| 209 | } |
| 210 | } |