diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/iter.rs | 148 | ||||
-rw-r--r-- | src/iterinfo.rs | 18 | ||||
-rw-r--r-- | src/masks.rs | 4 | ||||
-rw-r--r-- | src/poslist.rs | 2 | ||||
-rw-r--r-- | src/yearinfo.rs | 2 |
5 files changed, 126 insertions, 48 deletions
diff --git a/src/iter.rs b/src/iter.rs index 4ab9a69..cd9b93d 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -77,20 +77,21 @@ impl IterResult { return true; } - pub fn get_value(&self) -> Option<Vec<DateTime<Utc>>> { - match self.method { - QueryMethodTypes::ALL => None, - QueryMethodTypes::BETWEEN => Some(self._result.clone()), - QueryMethodTypes::BEFORE => None, - QueryMethodTypes::AFTER => None, - } + pub fn get_value(&self) -> Vec<DateTime<Utc>> { + self._result.clone() + //match self.method { + //QueryMethodTypes::BETWEEN => Some(self._result.clone()), + //_ => { + //if self._result.is_empty() { + //return None; + //} + //Some(vec![self._result[self._result.len() - 1].clone()]) + //} + //} } } -pub fn iter( - iter_result: &mut IterResult, - options: &mut ParsedOptions, -) -> Option<Vec<DateTime<Utc>>> { +pub fn iter(iter_result: &mut IterResult, options: &mut ParsedOptions) -> Vec<DateTime<Utc>> { if (options.count.is_some() && options.count.unwrap() == 0) || options.interval == 0 { return iter_result.get_value(); } @@ -100,9 +101,13 @@ pub fn iter( ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); let mut timeset = make_timeset(&ii, &counter_date, options); + let mut count = match options.count { + Some(count) => count, + _ => 0, + }; loop { - let (mut dayset, start, end) = ii.getdayset( + let (dayset, start, end) = ii.getdayset( &options.freq, counter_date.year() as isize, counter_date.month() as usize, @@ -131,10 +136,9 @@ pub fn iter( return iter_result.get_value(); } - if options.count.is_some() { - //options.count.unwrap() += 1; - //options.count = Some(options.count.unwrap() - 1); - if options.count.unwrap() == 0 { + if count > 0 { + count -= 1; + if count == 0 { return iter_result.get_value(); } } @@ -146,8 +150,8 @@ pub fn iter( if current_day.is_none() { continue; } - let current_day = current_day.unwrap(); + let current_day = current_day.unwrap(); let date = from_ordinal(ii.yearordinal().unwrap() + current_day); for k in 0..timeset.len() { let res = Utc.ymd(date.year(), date.month(), date.day()).and_hms( @@ -161,13 +165,12 @@ pub fn iter( if res >= options.dtstart { //let rezoned_date = rezone_if_needed(&res, &options); let rezoned_date = res.clone(); - if iter_result.accept(rezoned_date) { + if !iter_result.accept(rezoned_date) { return iter_result.get_value(); } - if options.count.is_some() { - //options.count.unwrap() += 1; - //options.count = Some(options.count.unwrap() - 1); - if options.count.unwrap() == 0 { + if count > 0 { + count -= 1; + if count == 0 { return iter_result.get_value(); } } @@ -181,9 +184,9 @@ pub fn iter( } // Handle frequency and interval - increment_counter_date(&mut counter_date, options, filtered); + counter_date = increment_counter_date(counter_date, options, filtered); - if counter_date.year() > 9999 { + if counter_date.year() > 2200 { return iter_result.get_value(); } @@ -205,16 +208,16 @@ pub fn iter( } pub fn increment_counter_date( - counter_date: &mut DateTime<Utc>, + counter_date: DateTime<Utc>, options: &ParsedOptions, filtered: bool, -) { +) -> DateTime<Utc> { match options.freq { - Frequenzy::YEARLY => { - counter_date.with_year(counter_date.year() + options.interval as i32); - } + Frequenzy::YEARLY => counter_date + .with_year(counter_date.year() + options.interval as i32) + .unwrap(), Frequenzy::MONTHLY => { - let mut new_month = counter_date.month() + options.interval as u32; + let new_month = counter_date.month() + options.interval as u32; if new_month > 12 { let mut year_div = new_month / 12; let mut new_month = new_month % 12; @@ -223,18 +226,28 @@ pub fn increment_counter_date( year_div -= 1; } let new_year = counter_date.year() + year_div as i32; - counter_date + return counter_date .with_month(new_month) .unwrap() .with_year(new_year) .unwrap(); } else { - counter_date.with_month(new_month); + return counter_date.with_month(new_month).unwrap(); } - counter_date.with_year(counter_date.year() + options.interval as i32); } - _ => (), - }; + Frequenzy::WEEKLY => { + let mut day_delta = 0; + let weekday = get_weekday_val(&counter_date.weekday()); + if options.wkst > weekday { + day_delta += -((weekday + 1 + (6 - options.wkst)) as isize) + + (options.interval as isize) * 7; + } else { + day_delta += -((weekday - options.wkst) as isize) + (options.interval as isize) * 7; + } + counter_date + Duration::days(day_delta as i64) + } + _ => panic!("hfoashfosa"), + } } pub fn includes<T>(v: &Vec<T>, el: &T) -> bool @@ -254,7 +267,9 @@ pub fn is_filtered(ii: &IterInfo, current_day: usize, options: &ParsedOptions) - || (not_empty(&options.byweekno) && (ii.wnomask().unwrap()[current_day]) == 0) || (not_empty(&options.byweekday) && !includes(&options.byweekday, &ii.wdaymask().unwrap()[current_day])) - || (not_empty(ii.nwdaymask().unwrap()) && (ii.nwdaymask().unwrap()[current_day]) == 0) + || (ii.nwdaymask().is_some() + && not_empty(ii.nwdaymask().unwrap()) + && (ii.nwdaymask().unwrap()[current_day]) == 0) || ((not_empty(&options.bymonthday) || not_empty(&options.bynmonthday)) && !includes(&options.bymonthday, &ii.mdaymask().unwrap()[current_day]) && !includes(&options.bynmonthday, &ii.nmdaymask().unwrap()[current_day])) @@ -366,3 +381,64 @@ pub fn make_timeset( counter_date.timestamp_subsec_millis() as usize, ); } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn int_works() { + let iter_args = IterArgs { + inc: true, + before: Utc::now(), + after: Utc::now(), + dt: Utc::now(), + _value: Some(vec![]), + }; + let mut iter_res = IterResult::new(QueryMethodTypes::ALL, iter_args); + let mut options = ParsedOptions { + freq: Frequenzy::MONTHLY, + dtstart: Utc.ymd(2012, 1, 1).and_hms(10, 30, 0), + until: None, + //until: Some(Utc.ymd(2012, 12, 31).and_hms(10, 30, 0)), + tzid: None, + interval: 5, + wkst: 0, + count: Some(5), + bysecond: vec![0], + byminute: vec![30], + byhour: vec![10], + bymonth: vec![], + bymonthday: vec![], + bysetpos: vec![], + byweekno: vec![], + byyearday: vec![], + byweekday: vec![0, 4], + bynweekday: vec![], + bynmonthday: vec![], + }; + let mut options_2 = ParsedOptions { + freq: Frequenzy::WEEKLY, + dtstart: Utc.ymd(2012, 1, 1).and_hms(10, 30, 0), + until: None, + //until: Some(Utc.ymd(2012, 12, 31).and_hms(10, 30, 0)), + tzid: None, + interval: 5, + wkst: 0, + count: Some(5), + bysecond: vec![0], + byminute: vec![30], + byhour: vec![10], + bymonth: vec![6], + bymonthday: vec![], + bysetpos: vec![], + byweekno: vec![], + byyearday: vec![], + byweekday: vec![0, 4], + bynweekday: vec![], + bynmonthday: vec![], + }; + let res = iter(&mut iter_res, &mut options); + println!("Res: {:?}", res); + } +} diff --git a/src/iterinfo.rs b/src/iterinfo.rs index bfd1421..475cd23 100644 --- a/src/iterinfo.rs +++ b/src/iterinfo.rs @@ -134,22 +134,25 @@ impl<'a> IterInfo<'a> { let end = mrange[month]; let mut set = vec![0; self.yearlen().unwrap()]; for i in start..end { - set[i] = 1; + set[i] = i; } (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()]; + let year_len = self.yearlen().unwrap(); + let mut set = vec![0; year_len]; - // should it be month - 1 ?????? let mut i = (to_ordinal( - &Utc.ymd(year as i32, month as u32 - 1, day as u32) + &Utc.ymd(year as i32, month as u32, day as u32) .and_hms(0, 0, 0), ) - self.yearordinal().unwrap()) as usize; let start = i; for _ in 0..7 { + if i >= year_len { + break; + } set[i] = i; i += 1; if self.wdaymask().unwrap()[i] == self.options.wkst { @@ -160,11 +163,10 @@ impl<'a> IterInfo<'a> { } pub fn ddayset(&self, year: isize, month: usize, day: usize) -> (Vec<usize>, usize, usize) { - let mut set = vec![0; self.yearlen().unwrap()]; + let 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) + let i = (to_ordinal( + &Utc.ymd(year as i32, month as u32, day as u32) .and_hms(0, 0, 0), ) - self.yearordinal().unwrap()) as usize; diff --git a/src/masks.rs b/src/masks.rs index 75730d8..3b609b4 100644 --- a/src/masks.rs +++ b/src/masks.rs @@ -45,7 +45,7 @@ impl Masks { vec![9; 30], vec![10; 31], vec![11; 30], - vec![12, 31], + vec![12; 31], vec![1; 7], ] .into_iter() @@ -101,7 +101,7 @@ impl Masks { vec![9; 30], vec![10; 31], vec![11; 30], - vec![12, 31], + vec![12; 31], vec![1; 7], ] .into_iter() diff --git a/src/poslist.rs b/src/poslist.rs index e962e0c..af0da7f 100644 --- a/src/poslist.rs +++ b/src/poslist.rs @@ -5,7 +5,7 @@ use chrono::prelude::*; use chrono::*; pub fn from_ordinal(ordinal: isize) -> DateTime<Utc> { - let timestamp = ordinal * 24 * 60 * 60 * 1000; + let timestamp = ordinal * 24 * 60 * 60; let naive = NaiveDateTime::from_timestamp(timestamp as i64, 0); DateTime::from_utc(naive, Utc) } diff --git a/src/yearinfo.rs b/src/yearinfo.rs index c41f66c..37af64d 100644 --- a/src/yearinfo.rs +++ b/src/yearinfo.rs @@ -64,7 +64,7 @@ pub fn to_ordinal(date: &DateTime<Utc>) -> isize { (date.timestamp() / 60 / 60 / 24) as isize } -fn get_weekday_val(wk: &Weekday) -> usize { +pub fn get_weekday_val(wk: &Weekday) -> usize { match wk { Weekday::Mon => 0, Weekday::Tue => 1, |