diff options
| author | Ruben Pollan | 2021-09-29 20:38:30 +0200 | 
|---|---|---|
| committer | Ruben Pollan | 2021-09-29 20:38:30 +0200 | 
| commit | 4116776e1d78568febf98b183738ae82263f01eb (patch) | |
| tree | 166d8ebd08492c3141b8c3d5f4035f4ef467adbd /src/periodic.rs | |
| parent | a66cfcac58ae8c9662776741d9727dfb6585fbcc (diff) | |
Add support to BYDAY in MONTHLY frequency
Diffstat (limited to 'src/periodic.rs')
| -rw-r--r-- | src/periodic.rs | 45 | 
1 files changed, 35 insertions, 10 deletions
| diff --git a/src/periodic.rs b/src/periodic.rs index 19874e4..c0d0ad3 100644 --- a/src/periodic.rs +++ b/src/periodic.rs @@ -1,5 +1,6 @@  use std::fmt;  use std::str::FromStr; +use std::ops::Add;  use std::collections::HashMap;  use chrono::{Duration, Weekday}; @@ -129,16 +130,40 @@ impl<'a> Iter<'a> {                  }              }              Freq::Monthly => { -                // TODO: byday... -                let new_date = if date.month() == 12 { -                    date.with_month(1) -                        .unwrap() -                        .with_year(date.year() + 1) -                        .unwrap() -                } else { -                    date.with_month(date.month() + 1).unwrap() -                }; -                new_date - date +                match &p.byday { +                    Some(byday) => { +                        let mut next = date; +                        if p.interval > 1 { +                            for _ in 1..p.interval { +                                next = next.add(Duration::days(next.days_in_month().into())); +                            } +                        } +                        loop { +                            next = next.add(Duration::days(1)); +                            let (week, neg_week) = next.week_of_month(); + +                            match byday.get(&next.weekday()) { +                                Some(occurrences) => +                                    if occurrences.contains(&0) || occurrences.contains(&week) ||occurrences.contains(&neg_week) { +                                        break; +                                    } +                                None => {} +                            }; +                        }; +                        next - date +                    } +                    None => { +                        let new_date = if date.month() == 12 { +                            date.with_month(1) +                                .unwrap() +                                .with_year(date.year() + 1) +                                .unwrap() +                        } else { +                            date.with_month(date.month() + 1).unwrap() +                        }; +                        new_date - date +                    } +                }              }              // TODO: byday...              Freq::Yearly => date.with_year(date.year() + 1).unwrap() - date, | 
