1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Implements `DateRule` - an iterator yielding evenly spaced dates.
use std::iter::Iterator;

use super::delta::with_day;
use super::relative_duration::RelativeDuration;
use chrono::{Date, DateTime, Datelike, NaiveDate, NaiveDateTime, TimeZone};

/// DateRule is an iterator for yielding evenly spaced dates
/// according to a given RelativeDuration. It avoids some
/// of the pitfalls that naive usage of RelativeDuration
/// can incur.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct DateRule<D: Datelike> {
    freq: RelativeDuration,
    start: D,
    end: Option<D>,
    count: Option<usize>,
    rolling_day: Option<u32>,
    _current_count: usize,
}

impl<D> DateRule<D>
where
    D: Datelike + Copy,
{
    /// Creates a new `DateRule` from an initial date and relative duration.
    #[inline]
    pub fn new(start: D, freq: RelativeDuration) -> Self {
        Self {
            freq,
            start,
            end: None,
            count: None,
            rolling_day: None,
            _current_count: 0,
        }
    }

    /// Creates a `DateRule` yielding dates one second appart.
    #[inline]
    pub fn secondly(from: D) -> Self {
        Self::new(from, RelativeDuration::seconds(1))
    }

    /// Creates a `DateRule` yielding dates one minute appart.
    #[inline]
    pub fn minutely(from: D) -> Self {
        Self::new(from, RelativeDuration::minutes(1))
    }

    /// Creates a `DateRule` yielding dates one hour appart.
    #[inline]
    pub fn hourly(from: D) -> Self {
        Self::new(from, RelativeDuration::hours(1))
    }

    /// Creates a `DateRule` yielding dates one day appart.
    #[inline]
    pub fn daily(from: D) -> Self {
        Self::new(from, RelativeDuration::days(1))
    }

    /// Creates a `DateRule` yielding dates one week appart.
    #[inline]
    pub fn weekly(from: D) -> Self {
        Self::new(from, RelativeDuration::weeks(1))
    }

    /// Creates a `DateRule` yielding dates one month appart.
    /// Ambiguous month-ends are shifted backwards as necessary.
    #[inline]
    pub fn monthly(from: D) -> Self {
        Self::new(from, RelativeDuration::months(1))
    }

    /// Creates a `DateRule` yielding dates one year appart.
    /// Ambiguous month-ends are shifted backwards as necessary.
    #[inline]
    pub fn yearly(from: D) -> Self {
        Self::new(from, RelativeDuration::years(1))
    }

    /// Limits the `DateRule` to a given number of dates.
    pub fn with_count(&self, number: usize) -> Self {
        Self {
            freq: self.freq,
            start: self.start,
            end: None,
            count: Some(number),
            rolling_day: self.rolling_day,
            _current_count: 0,
        }
    }

    /// Limits the `DateRule` to an extremal date (exclusive).
    ///
    /// If using a `RelativeDuration` which shifts dates backwards, the `end` date should
    /// be before the current date.
    ///
    /// WARNING: a forward-shifting duration with an end-date before the initial date
    /// will result in an iterator which does not terminate.
    pub fn with_end(&self, end: D) -> Self {
        Self {
            freq: self.freq,
            start: self.start,
            end: Some(end),
            count: None,
            rolling_day: self.rolling_day,
            _current_count: 0,
        }
    }

    /// Ensure the `DateRule` yields new dates which are *always* fall on the
    /// given rolling day (modulo backwards shifting for month ends). Returns
    /// Err if the rolling day is not in the range 1-31.
    ///
    /// For example:
    /// ```rust
    /// # use chrono::NaiveDate;
    /// # use chronoutil::DateRule;
    /// let start = NaiveDate::from_ymd(2020, 2, 29);
    /// let mut rule = DateRule::monthly(start).with_rolling_day(31).unwrap();
    ///
    /// assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd(2020, 2, 29));
    /// assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd(2020, 3, 31));
    /// assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd(2020, 4, 30));
    /// assert_eq!(rule.next().unwrap(), NaiveDate::from_ymd(2020, 5, 31));
    /// // etc.
    /// ```
    ///
    /// It produces values equivalent to
    /// ```ignore
    /// rule.map(|d| with_day(d, rolling_day).unwrap())
    /// ```
    pub fn with_rolling_day(&self, rolling_day: u32) -> Result<Self, String> {
        if rolling_day == 0 || rolling_day > 31 {
            Err(format!("Rolling day {} not in range 1-31", rolling_day))
        } else {
            Ok(Self {
                freq: self.freq,
                start: self.start,
                end: self.end,
                count: self.count,
                rolling_day: Some(rolling_day),
                _current_count: self._current_count,
            })
        }
    }
}

// The following is just copy-pasta, mostly because we
// can't impl<T> Add<RelativeDuration> for T with T: Datelike
impl Iterator for DateRule<NaiveDate> {
    type Item = NaiveDate;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count.is_some() && self._current_count >= self.count.unwrap() {
            return None;
        }

        let mut current_date = self.start + self.freq * self._current_count as i32;
        if let Some(rolling_day) = self.rolling_day {
            current_date = with_day(current_date, rolling_day).unwrap();
        }

        if let Some(end) = &self.end {
            if (*end >= self.start && current_date >= *end)
                || (*end < self.start && current_date <= *end)
            {
                return None;
            }
        }

        self._current_count += 1;
        Some(current_date)
    }
}

impl Iterator for DateRule<NaiveDateTime> {
    type Item = NaiveDateTime;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count.is_some() && self._current_count >= self.count.unwrap() {
            return None;
        }

        let mut current_date = self.start + self.freq * self._current_count as i32;
        if let Some(rolling_day) = self.rolling_day {
            current_date = with_day(current_date, rolling_day).unwrap();
        }

        if let Some(end) = &self.end {
            if (*end >= self.start && current_date >= *end)
                || (*end < self.start && current_date <= *end)
            {
                return None;
            }
        }

        self._current_count += 1;
        Some(current_date)
    }
}

impl<Tz> Iterator for DateRule<Date<Tz>>
where
    Tz: TimeZone,
{
    type Item = Date<Tz>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count.is_some() && self._current_count >= self.count.unwrap() {
            return None;
        }

        let mut current_date = self.start.clone() + self.freq * self._current_count as i32;
        if let Some(rolling_day) = self.rolling_day {
            current_date = with_day(current_date, rolling_day).unwrap();
        }

        if let Some(end) = &self.end {
            if (*end >= self.start && current_date >= *end)
                || (*end < self.start && current_date <= *end)
            {
                return None;
            }
        }

        self._current_count += 1;
        Some(current_date)
    }
}

impl<Tz> Iterator for DateRule<DateTime<Tz>>
where
    Tz: TimeZone,
{
    type Item = DateTime<Tz>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count.is_some() && self._current_count >= self.count.unwrap() {
            return None;
        }

        let mut current_date = self.start.clone() + self.freq * self._current_count as i32;
        if let Some(rolling_day) = self.rolling_day {
            current_date = with_day(current_date, rolling_day).unwrap();
        }

        if let Some(end) = &self.end {
            if (*end >= self.start && current_date >= *end)
                || (*end < self.start && current_date <= *end)
            {
                return None;
            }
        }

        self._current_count += 1;
        Some(current_date)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use chrono::{Duration, NaiveDateTime, NaiveTime};

    #[test]
    fn test_rrule_with_date() {
        let start = NaiveDate::from_ymd(2020, 1, 1);

        // Seconds, hours, minutes etc
        for (i, date) in DateRule::secondly(start)
            .with_count(24 * 60 * 60 * 2)
            .enumerate()
        {
            if i < 24 * 60 * 60 {
                assert_eq!(date, start, "Expected {} seconds to be on first day", i);
            } else {
                assert_eq!(
                    date,
                    start + Duration::days(1),
                    "Expected {} seconds to be on second day",
                    i
                );
            }
        }
        for (i, date) in DateRule::minutely(start)
            .with_count(24 * 60 * 2)
            .enumerate()
        {
            if i < 24 * 60 {
                assert_eq!(date, start, "Expected {} minutes to be on first day", i);
            } else {
                assert_eq!(
                    date,
                    start + Duration::days(1),
                    "Expected {} minutes to be on second day",
                    i
                );
            }
        }
        for (i, date) in DateRule::hourly(start).with_count(24 * 2).enumerate() {
            if i < 24 {
                assert_eq!(date, start, "Expected {} hours to be on first day", i);
            } else {
                assert_eq!(
                    date,
                    start + Duration::days(1),
                    "Expected {} hours to be on second day",
                    i
                );
            }
        }

        // Days, weeks
        let days: Vec<NaiveDate> = DateRule::daily(start).with_count(5).collect();
        assert_eq!(days[0], start, "DateRule should start at the initial day");
        assert_eq!(
            days[1],
            start + Duration::days(1),
            "DateRule should increment in days"
        );
        assert_eq!(
            days.len(),
            5,
            "DateRule should finish before the count is up"
        );

        let finish = NaiveDate::from_ymd(2020, 1, 29);
        let weeks: Vec<NaiveDate> = DateRule::weekly(start).with_end(finish).collect();
        assert_eq!(weeks[0], start, "DateRule should start at the initial day");
        assert_eq!(
            weeks[1],
            start + Duration::days(7),
            "DateRule should increment in weeks"
        );
        assert_eq!(
            weeks.len(),
            4,
            "DateRule should finish before the final day"
        );

        // Months, years
        let interesting = NaiveDate::from_ymd(2020, 1, 30); // The day will change each month

        let months: Vec<NaiveDate> = DateRule::monthly(interesting).with_count(5).collect();
        assert_eq!(
            months[0], interesting,
            "DateRule should start at the initial day"
        );
        assert_eq!(
            months[1],
            NaiveDate::from_ymd(2020, 2, 29),
            "DateRule should handle Feb"
        );
        assert_eq!(
            months[2],
            NaiveDate::from_ymd(2020, 3, 30),
            "DateRule should not loose days"
        );
        assert_eq!(
            months.len(),
            5,
            "DateRule should finish before the count is up"
        );

        let years: Vec<NaiveDate> = DateRule::yearly(interesting).with_count(3).collect();
        assert_eq!(
            years[0], interesting,
            "DateRule should start at the initial day"
        );
        assert_eq!(
            years[1],
            NaiveDate::from_ymd(2021, 1, 30),
            "DateRule should increment in years"
        );
        assert_eq!(
            years.len(),
            3,
            "DateRule should finish before the count is up"
        );
    }

    #[test]
    fn test_rrule_with_datetime() {
        // Seconds
        let o_clock = NaiveTime::from_hms(1, 2, 3);
        let day = NaiveDate::from_ymd(2020, 1, 1);
        let start = NaiveDateTime::new(day, o_clock);

        let seconds_passed = 60 * 60 + 2 * 60 + 3;

        for (i, date) in DateRule::secondly(start)
            .with_count(24 * 60 * 60 * 2)
            .enumerate()
        {
            if i > 0 {
                assert!(date > start, "Time should increase");
            }

            if i < 24 * 60 * 60 - seconds_passed {
                assert_eq!(
                    date.date(),
                    day,
                    "Expected {} seconds to be on first day",
                    date
                );
            } else if i < 2 * 24 * 60 * 60 - seconds_passed {
                assert_eq!(
                    date.date(),
                    day + Duration::days(1),
                    "Expected {} to be on second day",
                    date
                );
            } else {
                assert_eq!(
                    date.date(),
                    day + Duration::days(2),
                    "Expected {} to be on third day",
                    date
                );
            }
        }

        // Months
        let interesting = NaiveDate::from_ymd(2020, 1, 30); // The day will change each month
        let istart = NaiveDateTime::new(interesting, o_clock);

        let months: Vec<NaiveDateTime> = DateRule::monthly(istart).with_count(5).collect();
        assert_eq!(
            months[0], istart,
            "DateRule should start at the initial day"
        );
        assert_eq!(
            months[1].date(),
            NaiveDate::from_ymd(2020, 2, 29),
            "DateRule should handle Feb"
        );
        assert_eq!(months[1].time(), o_clock, "Time should remain the same");
        assert_eq!(
            months[2].date(),
            NaiveDate::from_ymd(2020, 3, 30),
            "DateRule should not loose days"
        );
        assert_eq!(months[2].time(), o_clock, "Time should remain the same");
    }

    #[test]
    fn test_rrule_edge_cases() {
        let start = NaiveDate::from_ymd(2020, 1, 1);

        // Zero count
        let mut dates: Vec<NaiveDate> = DateRule::daily(start).with_count(0).collect();
        assert_eq!(dates.len(), 0);

        // End equals start
        dates = DateRule::daily(start).with_end(start).collect();
        assert_eq!(dates.len(), 0);

        // End before start
        // TODO: the only way to know to stop is to determine the forward/backwardness of the duration.
        // This is a concept which is ill formed (e.g. +1 month - 30 days) so needs thought.
        // dates = DateRule::daily(start).with_end(start - Duration::days(1)).collect();
        // assert_eq!(dates.len(), 0);
    }

    #[test]
    fn test_backwards_rrule() {
        let start = NaiveDate::from_ymd(2020, 3, 31);
        let end = NaiveDate::from_ymd(2019, 12, 31);
        let freq = RelativeDuration::months(-1);

        let dates1: Vec<NaiveDate> = DateRule::new(start, freq).with_count(3).collect();
        assert_eq!(dates1.len(), 3);
        assert_eq!(dates1[0], NaiveDate::from_ymd(2020, 3, 31));
        assert_eq!(dates1[1], NaiveDate::from_ymd(2020, 2, 29));
        assert_eq!(dates1[2], NaiveDate::from_ymd(2020, 1, 31));

        let dates2: Vec<NaiveDate> = DateRule::new(start, freq).with_end(end).collect();

        assert_eq!(dates1.len(), dates2.len());
        for k in 0..dates1.len() {
            assert_eq!(dates1[k], dates2[k]);
        }
    }
}