diff options
author | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-14 15:41:27 +0200 |
---|---|---|
committer | Fredrik Meringdal <fmeringdal@hotmail.com> | 2020-10-14 15:41:27 +0200 |
commit | 69ac9584f3148e3f0fa2d587ca3c7beff6b223f7 (patch) | |
tree | 486f407f34704a7edcd66eb2e4573f670b39c85b | |
parent | 15acffa779ff21c53752149bd9d248f769a0b7a3 (diff) | |
download | rust_rrule-69ac9584f3148e3f0fa2d587ca3c7beff6b223f7.zip |
iterinfo first port
-rw-r--r-- | src/iterinfo.rs | 70 | ||||
-rw-r--r-- | src/yearinfo.rs | 2 |
2 files changed, 71 insertions, 1 deletions
diff --git a/src/iterinfo.rs b/src/iterinfo.rs index bf7f1d7..98edd45 100644 --- a/src/iterinfo.rs +++ b/src/iterinfo.rs @@ -1,5 +1,7 @@ use crate::monthinfo::*; use crate::yearinfo::*; +use chrono::prelude::*; +use chrono::*; struct IterInfo<'a> { pub yearinfo: Option<YearInfo>, @@ -115,4 +117,72 @@ impl<'a> IterInfo<'a> { None => None, } } + + pub fn ydayset(&self) -> (Vec<usize>, usize, usize) { + let yearlen = self.yearlen().unwrap(); + let mut v = Vec::with_capacity(yearlen); + for i in 0..yearlen { + v.push(i); + } + (v, 0, yearlen) + } + + pub fn mdayset(&self, month: usize) -> (Vec<usize>, usize, usize) { + let mrange = self.mrange().unwrap(); + let start = mrange[month - 1]; + let end = mrange[month]; + let mut set = vec![0; self.yearlen().unwrap()]; + for i in start..end { + set[i] = 1; + } + (set, start, end) + } + + pub fn wdayset(&self, year: isize, month: usize, day: usize) -> (Vec<usize>, usize, usize) { + let mut set = vec![0; self.yearlen().unwrap()]; + + // should it be month - 1 ?????? + let mut i = (to_ordinal( + &Utc.ymd(year as i32, month as u32 - 1, day as u32) + .and_hms(0, 0, 0), + ) - self.yearordinal().unwrap()) as usize; + + let start = i; + for _ in 0..7 { + set[i] = i; + i += 1; + if self.wdaymask().unwrap()[i] == self.options.wkst { + break; + } + } + (set, start, i) + } + + pub fn ddayset(&self, year: isize, month: usize, day: usize) -> (Vec<usize>, usize, usize) { + let mut set = vec![0; self.yearlen().unwrap()]; + + // should it be month - 1 ?????? + let mut i = (to_ordinal( + &Utc.ymd(year as i32, month as u32 - 1, day as u32) + .and_hms(0, 0, 0), + ) - self.yearordinal().unwrap()) as usize; + + (set, i, i + 1) + } + + pub fn getdayset( + &self, + freq: Frequenzy, + year: isize, + month: usize, + day: usize, + ) -> (Vec<usize>, usize, usize) { + match freq { + Frequenzy::YEARLY => self.ydayset(), + Frequenzy::MONTHLY => self.mdayset(month), + Frequenzy::WEEKLY => self.wdayset(year, month, day), + Frequenzy::DAILY => self.ddayset(year, month, day), + _ => self.ydayset(), + } + } } diff --git a/src/yearinfo.rs b/src/yearinfo.rs index 37767c1..0628485 100644 --- a/src/yearinfo.rs +++ b/src/yearinfo.rs @@ -60,7 +60,7 @@ fn get_year_len(year: i32) -> usize { 365 } -fn to_ordinal(date: &DateTime<Utc>) -> isize { +pub fn to_ordinal(date: &DateTime<Utc>) -> isize { (date.timestamp() / 60 / 60 / 24) as isize } |