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
//! Module provides an iterator over the annual occurrences of a `Holiday`

use crate::*;

#[derive(Debug)]
/// An iterator over the occurrences of a Holiday
pub struct HolidayIter<'h, H: BeforeAfterDate> {
    holiday: &'h H,
    first: NaiveDate,
    last: NaiveDate,
    current: NaiveDate,
}

impl<'h, H: BeforeAfterDate> HolidayIter<'h, H> {
    /// Set the current date
    pub fn at(mut self, current_date: NaiveDate) -> Self {
        self.current = current_date.pred();
        self.shift_from(current_date);

        self
    }

    /// Start the iterator at the given date
    pub fn starting_at(mut self, start_date: NaiveDate) -> Self {
        self.first = self.holiday.after(&start_date);
        self.shift_from(start_date);

        self
    }

    /// End the iterator at the given date
    pub fn ending_at(mut self, end_date: NaiveDate) -> Self {
        self.last = self.holiday.before(&end_date);
        self.shift_from(end_date);

        self
    }

    /// Set first and last dates for the iterator if a new date is not between the first
    /// and last dates.
    pub fn shift_from(&mut self, date: NaiveDate) {
        if date < self.first {
            self.first = date;
        }

        if date > self.last {
            self.last = date;
        }
    }
}

impl<'h, H: BeforeAfterDate> Iterator for HolidayIter<'h, H> {
    type Item = NaiveDate;
    fn next(&mut self) -> Option<Self::Item> {
        let next = self.holiday.after(&self.current.succ());
        if next <= self.last {
            self.current = next;
            Some(next)
        } else {
            None
        }
    }
}

impl<'h, H: BeforeAfterDate> DoubleEndedIterator for HolidayIter<'h, H> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let prev = self.holiday.before(&self.current);
        if self.current >= self.first {
            self.current = prev;
            Some(prev)
        } else {
            None
        }
    }
}

macro_rules! impl_holiday_into_iter {
    ($ty:ty) => {
        impl<'h> IntoIterator for &'h $ty {
            type Item = NaiveDate;
            type IntoIter = HolidayIter<'h, $ty>;
            fn into_iter(self) -> Self::IntoIter {
                HolidayIter {
                    holiday: &self,
                    first: self.first_date(),
                    last: self.last_date(),
                    current: self.first_date(),
                }
            }
        }
    };

    ($ty:ty, $id:ident: $trait:ident) => {
        impl<'h, $id: $trait> IntoIterator for &'h $ty {
            type Item = NaiveDate;
            type IntoIter = HolidayIter<'h, $ty>;
            fn into_iter(self) -> Self::IntoIter {
                HolidayIter {
                    holiday: &self,
                    first: self.first_date(),
                    last: self.last_date(),
                    current: self.first_date(),
                }
            }
        }
    }
}


impl_holiday_into_iter!(Holiday<S>, S: ToString);
impl_holiday_into_iter!(HolidayDate);
impl_holiday_into_iter!(DayOfMonth);
impl_holiday_into_iter!(NthWeekdayOfMonth);

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

    #[test]
    fn tgives_iter() {
        dbg!(THANKSGIVING.first_date());
        dbg!(THANKSGIVING.last_date());

        let mut tgives = THANKSGIVING
            .into_iter()
            .at(NaiveDate::from_ymd(2020, 11, 1));
        assert_eq!(tgives.next(), Some(NaiveDate::from_ymd(2020, 11, 26)));
        assert_eq!(tgives.next(), Some(NaiveDate::from_ymd(2021, 11, 25)));
        assert_eq!(tgives.next(), Some(NaiveDate::from_ymd(2022, 11, 24)));
        assert_eq!(tgives.next_back(), Some(NaiveDate::from_ymd(2021, 11, 25)));
        assert_eq!(tgives.next_back(), Some(NaiveDate::from_ymd(2020, 11, 26)));
        assert_eq!(tgives.next_back(), Some(NaiveDate::from_ymd(2019, 11, 28)));
    }

    #[test]
    fn tgives_count() {
        let mut dates = std::collections::HashMap::new();

        for date in THANKSGIVING
            .into_iter()
            .at(NaiveDate::from_ymd(2020, 11, 1))
            .ending_at(NaiveDate::from_ymd(12020, 11, 30))
        {
            //dbg!(&date);
            *dates.entry(date.day()).or_insert(0) += 1;
        }

        let sum = dates.values().sum::<usize>();
        for (day, count) in dates.iter() {
            let pct = *count as f64 / sum as f64 * 100.0;
            println!("DAY: {}: {:.2}%", day, pct);
        }

        let most_days = dates
            .into_iter()
            .max_by(|(_k0, v0), (_k1, v1)| v0.cmp(v1))
            .unwrap()
            .0;

        assert_eq!(most_days, 26);
    }

    #[test]
    fn jan_dec() {
        use NthWeekday::*;
        let jan = holiday!("First Wednesday in January", First, Weekday::Wed, January);
        let mut jan_iter = jan.into_iter().at(NaiveDate::from_ymd(2020, 1, 1));
        assert_eq!(jan_iter.next(), Some(NaiveDate::from_ymd(2020, 1, 1)));
        assert_eq!(jan_iter.next(), Some(NaiveDate::from_ymd(2021, 1, 6)));
        assert_eq!(jan_iter.next(), Some(NaiveDate::from_ymd(2022, 1, 5)));
        assert_eq!(jan_iter.next(), Some(NaiveDate::from_ymd(2023, 1, 4)));

        let dec = holiday!("Fifth Wednesday in December", Fifth, Weekday::Wed, December);
        let mut dec_iter = dec.into_iter().at(NaiveDate::from_ymd(2020, 12, 1));
        assert_eq!(dec_iter.next(), Some(NaiveDate::from_ymd(2020, 12, 30)));
        assert_eq!(dec_iter.next(), Some(NaiveDate::from_ymd(2021, 12, 29)));
        assert_eq!(dec_iter.next(), Some(NaiveDate::from_ymd(2025, 12, 31)));
    }
}