| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use chrono::{Datelike, TimeZone, Timelike, Utc, Weekday}; |
| 4 | |
| 5 | use crate::{Frequency, RRule, RRuleError, Tz}; |
| 6 | |
| 7 | use super::{ |
| 8 | checks, |
| 9 | masks::MASKS, |
| 10 | operation_errors::{checked_add_u32, checked_mul_u32}, |
| 11 | utils::is_leap_year, |
| 12 | }; |
| 13 | |
| 14 | const MINUTES_IN_A_DAY: u32 = 60 * 24; |
| 15 | const SECONDS_IN_A_DAY: u32 = 60 * 60 * 24; |
| 16 | |
| 17 | /// A simple date time type used during iteration. |
| 18 | #[derive(Debug, Clone, PartialEq)] |
| 19 | pub(crate) struct DateTimeIter { |
| 20 | pub year: i32, |
| 21 | pub month: u32, |
| 22 | pub day: u32, |
| 23 | pub hour: u32, |
| 24 | pub minute: u32, |
| 25 | pub second: u32, |
| 26 | } |
| 27 | |
| 28 | impl DateTimeIter { |
| 29 | /// Increments the datetime based on the [`RRule`] values. |
| 30 | /// |
| 31 | /// If `increment_day` is set to `true`, then the method will also make sure that |
| 32 | /// the day is incremented. This is useful is the case where `rrule.freq` |
| 33 | /// is higher than daily (e.g. hourly) where this function might return a date with the |
| 34 | /// same day, but the iterator already knows that the current day can't |
| 35 | /// be part of the result. |
| 36 | pub fn increment(&mut self, rrule: &RRule, increment_day: bool) -> Result<(), RRuleError> { |
| 37 | let RRule { |
| 38 | interval, |
| 39 | week_start, |
| 40 | by_hour, |
| 41 | by_minute, |
| 42 | by_second, |
| 43 | .. |
| 44 | } = rrule; |
| 45 | match rrule.freq { |
| 46 | Frequency::Yearly => self.increment_yearly(*interval), |
| 47 | Frequency::Monthly => self.increment_monthly(*interval), |
| 48 | Frequency::Weekly => self.increment_weekly(*interval, *week_start), |
| 49 | Frequency::Daily => self.increment_daily(*interval), |
| 50 | Frequency::Hourly => self.increment_hourly(*interval, by_hour, increment_day), |
| 51 | Frequency::Minutely => { |
| 52 | self.increment_minutely(*interval, by_hour, by_minute, increment_day) |
| 53 | } |
| 54 | Frequency::Secondly => { |
| 55 | self.increment_secondly(*interval, by_hour, by_minute, by_second, increment_day) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn increment_yearly(&mut self, interval: u16) -> Result<(), RRuleError> { |
| 61 | self.year += i32::from(interval); |
| 62 | checks::check_year_range(self.year)?; |
| 63 | self.fix_day() |
| 64 | } |
| 65 | |
| 66 | fn increment_monthly(&mut self, interval: u16) -> Result<(), RRuleError> { |
| 67 | self.month += u32::from(interval); |
| 68 | if self.month > 12 { |
| 69 | let mut year_div = u16::try_from(self.month).map_err(|_| { |
| 70 | RRuleError::new_iter_err( |
| 71 | "Encountered a too high new month. Please decrease the rrule interval.", |
| 72 | ) |
| 73 | })? / 12; |
| 74 | self.month %= 12; |
| 75 | if self.month == 0 { |
| 76 | self.month = 12; |
| 77 | year_div -= 1; |
| 78 | } |
| 79 | self.year += i32::from(year_div); |
| 80 | checks::check_year_range(self.year)?; |
| 81 | } |
| 82 | Ok(()) |
| 83 | } |
| 84 | |
| 85 | fn get_weekday(&self) -> u32 { |
| 86 | let month_range_mask = if is_leap_year(self.year) { |
| 87 | &MASKS.month_366_range |
| 88 | } else { |
| 89 | &MASKS.month_365_range |
| 90 | }; |
| 91 | let year_day = u32::from(month_range_mask[self.month as usize - 1]) + self.day - 1; |
| 92 | let year_day_mod = year_day % 7; |
| 93 | let year_start_weekday = Utc |
| 94 | .with_ymd_and_hms(self.year, 1, 1, 0, 0, 0) |
| 95 | // It should never fail, since there is always a 1st of January, is there? |
| 96 | .unwrap() |
| 97 | .weekday() |
| 98 | .num_days_from_monday(); |
| 99 | |
| 100 | (year_day_mod + year_start_weekday) % 7 |
| 101 | } |
| 102 | |
| 103 | fn increment_weekly(&mut self, interval: u16, week_start: Weekday) -> Result<(), RRuleError> { |
| 104 | let weekday = self.get_weekday(); |
| 105 | let option_week_start = week_start.num_days_from_monday(); |
| 106 | let interval = u32::from(interval); |
| 107 | // Calculate the amount of day to move forward. |
| 108 | let day_delta = if option_week_start > weekday { |
| 109 | // `weekday` and `option_week_start` can only be in range `0..=6` |
| 110 | // `option_week_start` > `weekday` so: |
| 111 | // `(weekday + 1 + 6 - option_week_start)` > 0 so can never be negative. |
| 112 | interval * 7 - (weekday + 7 - option_week_start) |
| 113 | } else { |
| 114 | // This can also never be negative |
| 115 | interval * 7 - (weekday - option_week_start) |
| 116 | }; |
| 117 | self.day += day_delta; |
| 118 | self.fix_day() |
| 119 | } |
| 120 | |
| 121 | fn days_in_month(&self) -> u32 { |
| 122 | if self.month == 0 { |
| 123 | 31 |
| 124 | } else { |
| 125 | let month_range_mask = if is_leap_year(self.year) { |
| 126 | &MASKS.month_366_range |
| 127 | } else { |
| 128 | &MASKS.month_365_range |
| 129 | }; |
| 130 | let days_in_month = |
| 131 | month_range_mask[self.month as usize] - month_range_mask[self.month as usize - 1]; |
| 132 | u32::from(days_in_month) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn fix_day(&mut self) -> Result<(), RRuleError> { |
| 137 | if self.day <= 28 { |
| 138 | return Ok(()); |
| 139 | } |
| 140 | |
| 141 | let mut days_in_month = self.days_in_month(); |
| 142 | if self.day <= days_in_month { |
| 143 | return Ok(()); |
| 144 | } |
| 145 | |
| 146 | while self.day > days_in_month { |
| 147 | self.day -= days_in_month; |
| 148 | self.increment_monthly(1)?; |
| 149 | |
| 150 | days_in_month = self.days_in_month(); |
| 151 | } |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | fn increment_daily(&mut self, interval: u16) -> Result<(), RRuleError> { |
| 156 | self.day += u32::from(interval); |
| 157 | self.fix_day() |
| 158 | } |
| 159 | |
| 160 | fn increment_hourly( |
| 161 | &mut self, |
| 162 | interval: u16, |
| 163 | by_hour: &[u8], |
| 164 | increment_day: bool, |
| 165 | ) -> Result<(), RRuleError> { |
| 166 | if increment_day { |
| 167 | // Jump to one iteration before the next day |
| 168 | let temp_value = (23 - self.hour) / u32::from(interval); |
| 169 | self.hour += checked_mul_u32( |
| 170 | temp_value, |
| 171 | u32::from(interval), |
| 172 | Some("please decrease `INTERVAL`"), |
| 173 | )?; |
| 174 | } |
| 175 | |
| 176 | let mut prev_hours = HashSet::new(); |
| 177 | loop { |
| 178 | self.hour = checked_add_u32( |
| 179 | self.hour, |
| 180 | u32::from(interval), |
| 181 | Some("please decrease `INTERVAL`"), |
| 182 | )?; |
| 183 | let new_hours = u8::try_from(self.hour % 24).expect("range 0-23 is covered by u8"); |
| 184 | if by_hour.is_empty() || by_hour.iter().any(|bh| *bh == new_hours) { |
| 185 | break; |
| 186 | } |
| 187 | if prev_hours.contains(&new_hours) { |
| 188 | return Err(RRuleError::new_iter_err( |
| 189 | "Infinite loop detected. It can be resolved by changing `BYHOUR` or `INTERVAL`", |
| 190 | )); |
| 191 | } |
| 192 | prev_hours.insert(new_hours); |
| 193 | } |
| 194 | |
| 195 | let new_days = u16::try_from(self.hour / 24).map_err(|_| { |
| 196 | RRuleError::new_iter_err("unexpected high hour, please decrease the interval") |
| 197 | })?; |
| 198 | self.hour %= 24; |
| 199 | if new_days > 0 { |
| 200 | self.increment_daily(new_days) |
| 201 | } else { |
| 202 | Ok(()) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn increment_minutely( |
| 207 | &mut self, |
| 208 | interval: u16, |
| 209 | by_hour: &[u8], |
| 210 | by_minute: &[u8], |
| 211 | increment_day: bool, |
| 212 | ) -> Result<(), RRuleError> { |
| 213 | if increment_day { |
| 214 | // Jump to one iteration before the next day |
| 215 | let minutes_total = self.hour * 60 + self.minute; |
| 216 | let temp_value = (MINUTES_IN_A_DAY - 1 - minutes_total) / u32::from(interval); |
| 217 | self.minute += checked_mul_u32( |
| 218 | temp_value, |
| 219 | u32::from(interval), |
| 220 | Some("please decrease `INTERVAL`"), |
| 221 | )?; |
| 222 | } |
| 223 | |
| 224 | let mut prev_values = HashSet::new(); |
| 225 | loop { |
| 226 | self.minute += u32::from(interval); |
| 227 | let hours_div = u16::try_from(self.minute / 60).map_err(|_| { |
| 228 | RRuleError::new_iter_err("unexpected high minute, please decrease interval") |
| 229 | })?; |
| 230 | if hours_div > 0 { |
| 231 | self.minute %= 60; |
| 232 | self.increment_hourly(hours_div, by_hour, increment_day)?; |
| 233 | } |
| 234 | |
| 235 | let hours = u8::try_from(self.hour % 24).expect("range 0-23 is covered by u8"); |
| 236 | let minutes = u8::try_from(self.minute % 60).expect("range 0-59 is covered by u8"); |
| 237 | |
| 238 | if (by_hour.is_empty() || by_hour.contains(&hours)) |
| 239 | && (by_minute.is_empty() || by_minute.contains(&minutes)) |
| 240 | { |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | if prev_values.contains(&(hours, minutes)) { |
| 245 | return Err(RRuleError::new_iter_err( |
| 246 | "Infinite loop detected. It can be resolved by changing `BYMINUTE`, `BYHOUR` or `INTERVAL`", |
| 247 | )); |
| 248 | } |
| 249 | prev_values.insert((hours, minutes)); |
| 250 | } |
| 251 | |
| 252 | Ok(()) |
| 253 | } |
| 254 | |
| 255 | fn increment_secondly( |
| 256 | &mut self, |
| 257 | interval: u16, |
| 258 | by_hour: &[u8], |
| 259 | by_minute: &[u8], |
| 260 | by_second: &[u8], |
| 261 | increment_day: bool, |
| 262 | ) -> Result<(), RRuleError> { |
| 263 | if increment_day { |
| 264 | // Jump to one iteration before the next day |
| 265 | let total_seconds = self.hour * 3600 + self.minute * 60 + self.second; |
| 266 | let temp_value = (SECONDS_IN_A_DAY - 1 - total_seconds) / u32::from(interval); |
| 267 | self.second += checked_mul_u32( |
| 268 | temp_value, |
| 269 | u32::from(interval), |
| 270 | Some("please decrease `INTERVAL`"), |
| 271 | )?; |
| 272 | } |
| 273 | |
| 274 | let mut prev_values = HashSet::new(); |
| 275 | loop { |
| 276 | self.second += u32::from(interval); |
| 277 | let minutes_div = u16::try_from(self.second / 60).map_err(|_| { |
| 278 | RRuleError::new_iter_err("unexpected high second, please decrease interval") |
| 279 | })?; |
| 280 | if minutes_div > 0 { |
| 281 | self.second %= 60; |
| 282 | self.increment_minutely(minutes_div, by_hour, by_minute, increment_day)?; |
| 283 | } |
| 284 | |
| 285 | let hours = u8::try_from(self.hour % 24).expect("range 0-23 is covered by u8"); |
| 286 | let minutes = u8::try_from(self.minute % 60).expect("range 0-59 is covered by u8"); |
| 287 | let seconds = u8::try_from(self.second % 60).expect("range 0-59 is covered by u8"); |
| 288 | |
| 289 | if (by_hour.is_empty() || by_hour.contains(&hours)) |
| 290 | && (by_minute.is_empty() || by_minute.contains(&minutes)) |
| 291 | && (by_second.is_empty() || by_second.contains(&seconds)) |
| 292 | { |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | if prev_values.contains(&(hours, minutes, seconds)) { |
| 297 | return Err(RRuleError::new_iter_err( |
| 298 | "Infinite loop detected. It can be resolved by changing `BYSECOND`, `BYMINUTE`, `BYHOUR` or `INTERVAL`", |
| 299 | )); |
| 300 | } |
| 301 | prev_values.insert((hours, minutes, seconds)); |
| 302 | } |
| 303 | |
| 304 | Ok(()) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl From<&chrono::DateTime<Tz>> for DateTimeIter { |
| 309 | fn from(dt: &chrono::DateTime<Tz>) -> Self { |
| 310 | Self { |
| 311 | year: dt.year(), |
| 312 | month: dt.month(), |
| 313 | day: dt.day(), |
| 314 | hour: dt.hour(), |
| 315 | minute: dt.minute(), |
| 316 | second: dt.second(), |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | #[cfg(test)] |
| 322 | mod tests { |
| 323 | use crate::core::Tz; |
| 324 | |
| 325 | use super::*; |
| 326 | use chrono::TimeZone; |
| 327 | |
| 328 | const UTC: Tz = Tz::UTC; |
| 329 | |
| 330 | fn ymd_hms(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> DateTimeIter { |
| 331 | let dt = UTC |
| 332 | .with_ymd_and_hms(year, month, day, hour, min, sec) |
| 333 | .unwrap(); |
| 334 | DateTimeIter::from(&dt) |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn increments_counter_date_with_yearly_freq() { |
| 339 | let tests = [ |
| 340 | ( |
| 341 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 342 | 1, |
| 343 | ymd_hms(1998, 1, 1, 0, 0, 0), |
| 344 | ), |
| 345 | ( |
| 346 | ymd_hms(2020, 2, 29, 0, 0, 0), |
| 347 | 1, |
| 348 | ymd_hms(2021, 3, 1, 0, 0, 0), |
| 349 | ), |
| 350 | ( |
| 351 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 352 | 3, |
| 353 | ymd_hms(2000, 1, 1, 0, 0, 0), |
| 354 | ), |
| 355 | ( |
| 356 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 357 | 20, |
| 358 | ymd_hms(2017, 1, 1, 0, 0, 0), |
| 359 | ), |
| 360 | ]; |
| 361 | for (mut counter_date, interval, expected_output) in tests { |
| 362 | let rrule = RRule { |
| 363 | interval, |
| 364 | freq: Frequency::Yearly, |
| 365 | ..Default::default() |
| 366 | } |
| 367 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 368 | .unwrap(); |
| 369 | |
| 370 | let res = counter_date.increment(&rrule, false); |
| 371 | assert!(res.is_ok()); |
| 372 | assert_eq!(counter_date, expected_output); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #[test] |
| 377 | fn increments_counter_date_with_monthly_freq() { |
| 378 | let tests = [ |
| 379 | ( |
| 380 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 381 | 1, |
| 382 | ymd_hms(1997, 2, 1, 0, 0, 0), |
| 383 | ), |
| 384 | ( |
| 385 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 386 | 11, |
| 387 | ymd_hms(1997, 12, 1, 0, 0, 0), |
| 388 | ), |
| 389 | ( |
| 390 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 391 | 12, |
| 392 | ymd_hms(1998, 1, 1, 0, 0, 0), |
| 393 | ), |
| 394 | ( |
| 395 | ymd_hms(1997, 12, 1, 0, 0, 0), |
| 396 | 1, |
| 397 | ymd_hms(1998, 1, 1, 0, 0, 0), |
| 398 | ), |
| 399 | ( |
| 400 | ymd_hms(1997, 11, 1, 0, 0, 0), |
| 401 | 12 * 10, |
| 402 | ymd_hms(2007, 11, 1, 0, 0, 0), |
| 403 | ), |
| 404 | ]; |
| 405 | for (mut counter_date, interval, expected_output) in tests { |
| 406 | let rrule = RRule { |
| 407 | interval, |
| 408 | freq: Frequency::Monthly, |
| 409 | ..Default::default() |
| 410 | } |
| 411 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 412 | .unwrap(); |
| 413 | |
| 414 | let res = counter_date.increment(&rrule, false); |
| 415 | assert!(res.is_ok()); |
| 416 | assert_eq!(counter_date, expected_output); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | #[test] |
| 421 | fn increments_counter_date_with_weekly_freq() { |
| 422 | let tests = [ |
| 423 | ( |
| 424 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 425 | 1, |
| 426 | ymd_hms(1997, 1, 6, 0, 0, 0), |
| 427 | ), |
| 428 | ( |
| 429 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 430 | 2, |
| 431 | ymd_hms(1997, 1, 13, 0, 0, 0), |
| 432 | ), |
| 433 | ( |
| 434 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 435 | 5, |
| 436 | ymd_hms(1997, 2, 3, 0, 0, 0), |
| 437 | ), |
| 438 | ( |
| 439 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 440 | 52, |
| 441 | ymd_hms(1997, 12, 29, 0, 0, 0), |
| 442 | ), |
| 443 | ( |
| 444 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 445 | 53, |
| 446 | ymd_hms(1998, 1, 5, 0, 0, 0), |
| 447 | ), |
| 448 | ]; |
| 449 | for (mut counter_date, interval, expected_output) in tests { |
| 450 | let rrule = RRule { |
| 451 | interval, |
| 452 | freq: Frequency::Weekly, |
| 453 | ..Default::default() |
| 454 | } |
| 455 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 456 | .unwrap(); |
| 457 | |
| 458 | let res = counter_date.increment(&rrule, false); |
| 459 | assert!(res.is_ok()); |
| 460 | assert_eq!(counter_date, expected_output); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn increments_counter_date_with_daily_freq() { |
| 466 | let tests = [ |
| 467 | ( |
| 468 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 469 | 1, |
| 470 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 471 | ), |
| 472 | ( |
| 473 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 474 | 2, |
| 475 | ymd_hms(1997, 1, 3, 0, 0, 0), |
| 476 | ), |
| 477 | ( |
| 478 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 479 | 31, |
| 480 | ymd_hms(1997, 2, 1, 0, 0, 0), |
| 481 | ), |
| 482 | ( |
| 483 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 484 | 365, |
| 485 | ymd_hms(1998, 1, 1, 0, 0, 0), |
| 486 | ), |
| 487 | ]; |
| 488 | for (mut counter_date, interval, expected_output) in tests { |
| 489 | let rrule = RRule { |
| 490 | interval, |
| 491 | freq: Frequency::Daily, |
| 492 | ..Default::default() |
| 493 | } |
| 494 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 495 | .unwrap(); |
| 496 | |
| 497 | let res = counter_date.increment(&rrule, false); |
| 498 | assert!(res.is_ok()); |
| 499 | assert_eq!(counter_date, expected_output); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | #[test] |
| 504 | fn increments_counter_date_with_hourly_freq() { |
| 505 | let tests = [ |
| 506 | ( |
| 507 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 508 | 1, |
| 509 | vec![], |
| 510 | ymd_hms(1997, 1, 1, 1, 0, 0), |
| 511 | ), |
| 512 | ( |
| 513 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 514 | 2, |
| 515 | vec![], |
| 516 | ymd_hms(1997, 1, 1, 2, 0, 0), |
| 517 | ), |
| 518 | ( |
| 519 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 520 | 1, |
| 521 | vec![4], |
| 522 | ymd_hms(1997, 1, 1, 4, 0, 0), |
| 523 | ), |
| 524 | ( |
| 525 | ymd_hms(1997, 1, 1, 10, 0, 0), |
| 526 | 1, |
| 527 | vec![4, 14], |
| 528 | ymd_hms(1997, 1, 1, 14, 0, 0), |
| 529 | ), |
| 530 | ( |
| 531 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 532 | 24, |
| 533 | vec![], |
| 534 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 535 | ), |
| 536 | ( |
| 537 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 538 | 24 * 31, |
| 539 | vec![], |
| 540 | ymd_hms(1997, 2, 1, 0, 0, 0), |
| 541 | ), |
| 542 | ]; |
| 543 | for (mut counter_date, interval, by_hour, expected_output) in tests { |
| 544 | let rrule = RRule { |
| 545 | interval, |
| 546 | freq: Frequency::Hourly, |
| 547 | by_hour, |
| 548 | ..Default::default() |
| 549 | } |
| 550 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 551 | .unwrap(); |
| 552 | |
| 553 | let res = counter_date.increment(&rrule, false); |
| 554 | assert!(res.is_ok()); |
| 555 | assert_eq!(counter_date, expected_output); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn increments_counter_date_with_hourly_freq_and_day_increment() { |
| 561 | let tests = [ |
| 562 | ( |
| 563 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 564 | 1, |
| 565 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 566 | ), |
| 567 | ( |
| 568 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 569 | 2, |
| 570 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 571 | ), |
| 572 | ( |
| 573 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 574 | 5, |
| 575 | ymd_hms(1997, 1, 2, 1, 0, 0), |
| 576 | ), |
| 577 | ( |
| 578 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 579 | 24, |
| 580 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 581 | ), |
| 582 | ( |
| 583 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 584 | 24 * 31, |
| 585 | ymd_hms(1997, 2, 1, 0, 0, 0), |
| 586 | ), |
| 587 | ( |
| 588 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 589 | 24 * 31 + 1, |
| 590 | ymd_hms(1997, 2, 1, 1, 0, 0), |
| 591 | ), |
| 592 | ]; |
| 593 | for (mut counter_date, interval, expected_output) in tests { |
| 594 | let rrule = RRule { |
| 595 | interval, |
| 596 | freq: Frequency::Hourly, |
| 597 | ..Default::default() |
| 598 | } |
| 599 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 600 | .unwrap(); |
| 601 | |
| 602 | let res = counter_date.increment(&rrule, true); |
| 603 | assert!(res.is_ok()); |
| 604 | assert_eq!(counter_date, expected_output); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | #[test] |
| 609 | fn increments_counter_date_with_minutely_freq() { |
| 610 | let tests = [ |
| 611 | ( |
| 612 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 613 | 1, |
| 614 | ymd_hms(1997, 1, 1, 0, 1, 0), |
| 615 | ), |
| 616 | ( |
| 617 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 618 | 2, |
| 619 | ymd_hms(1997, 1, 1, 0, 2, 0), |
| 620 | ), |
| 621 | ( |
| 622 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 623 | 60, |
| 624 | ymd_hms(1997, 1, 1, 1, 0, 0), |
| 625 | ), |
| 626 | ( |
| 627 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 628 | 60 * 24, |
| 629 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 630 | ), |
| 631 | ]; |
| 632 | for (mut counter_date, interval, expected_output) in tests { |
| 633 | let rrule = RRule { |
| 634 | interval, |
| 635 | freq: Frequency::Minutely, |
| 636 | ..Default::default() |
| 637 | } |
| 638 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 639 | .unwrap(); |
| 640 | |
| 641 | let res = counter_date.increment(&rrule, false); |
| 642 | assert!(res.is_ok()); |
| 643 | assert_eq!(counter_date, expected_output); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | #[test] |
| 648 | fn increments_counter_date_with_minutely_freq_and_day_increment() { |
| 649 | let tests = [ |
| 650 | ( |
| 651 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 652 | 1, |
| 653 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 654 | ), |
| 655 | ( |
| 656 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 657 | 2, |
| 658 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 659 | ), |
| 660 | ( |
| 661 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 662 | 60 * 24 + 1, |
| 663 | ymd_hms(1997, 1, 2, 0, 1, 0), |
| 664 | ), |
| 665 | ]; |
| 666 | for (mut counter_date, interval, expected_output) in tests { |
| 667 | let rrule = RRule { |
| 668 | interval, |
| 669 | freq: Frequency::Minutely, |
| 670 | ..Default::default() |
| 671 | } |
| 672 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 673 | .unwrap(); |
| 674 | |
| 675 | let res = counter_date.increment(&rrule, true); |
| 676 | assert!(res.is_ok()); |
| 677 | assert_eq!(counter_date, expected_output); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | #[test] |
| 682 | fn increments_counter_date_with_secondly_freq() { |
| 683 | let tests = [ |
| 684 | ( |
| 685 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 686 | 1, |
| 687 | ymd_hms(1997, 1, 1, 0, 0, 1), |
| 688 | ), |
| 689 | ( |
| 690 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 691 | 2, |
| 692 | ymd_hms(1997, 1, 1, 0, 0, 2), |
| 693 | ), |
| 694 | ( |
| 695 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 696 | 60, |
| 697 | ymd_hms(1997, 1, 1, 0, 1, 0), |
| 698 | ), |
| 699 | ( |
| 700 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 701 | 60 * 60, |
| 702 | ymd_hms(1997, 1, 1, 1, 0, 0), |
| 703 | ), |
| 704 | ]; |
| 705 | for (mut counter_date, interval, expected_output) in tests { |
| 706 | let rrule = RRule { |
| 707 | interval, |
| 708 | freq: Frequency::Secondly, |
| 709 | ..Default::default() |
| 710 | } |
| 711 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 712 | .unwrap(); |
| 713 | |
| 714 | let res = counter_date.increment(&rrule, false); |
| 715 | assert!(res.is_ok()); |
| 716 | assert_eq!(counter_date, expected_output); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | #[test] |
| 721 | fn increments_counter_date_with_secondly_freq_and_day_increment() { |
| 722 | let tests = [ |
| 723 | ( |
| 724 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 725 | 1, |
| 726 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 727 | ), |
| 728 | ( |
| 729 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 730 | 2, |
| 731 | ymd_hms(1997, 1, 2, 0, 0, 0), |
| 732 | ), |
| 733 | ( |
| 734 | ymd_hms(1997, 1, 1, 0, 0, 0), |
| 735 | 60 * 24 + 1, |
| 736 | ymd_hms(1997, 1, 2, 0, 1, 0), |
| 737 | ), |
| 738 | ]; |
| 739 | for (mut counter_date, interval, expected_output) in tests { |
| 740 | let rrule = RRule { |
| 741 | interval, |
| 742 | freq: Frequency::Secondly, |
| 743 | ..Default::default() |
| 744 | } |
| 745 | .validate(UTC.with_ymd_and_hms(1997, 1, 1, 1, 1, 1).unwrap()) |
| 746 | .unwrap(); |
| 747 | |
| 748 | let res = counter_date.increment(&rrule, true); |
| 749 | assert!(res.is_ok()); |
| 750 | assert_eq!(counter_date, expected_output); |
| 751 | } |
| 752 | } |
| 753 | } |