/// Remove `DatePoints` outside `date_range` from `Self`.
pub fn with_range(&mut self, date_range: &DateRange) {
let start_date = match date_range.start_date {
None => self.ts.0.first().unwrap().date(),
Some(start) => start,
};
let end_date = match date_range.end_date {
None => self.ts.0.last().unwrap().date(),
Some(end) => end,
};
self.ts.0.retain(|dp| dp.date >= start_date && dp.date <= end_date)
}
}
#[test]
fn test_with_range() {
let date1 = MonthlyDate::ym(1995, 11);
let date2 = MonthlyDate::ym(1995, 12);
let date3 = MonthlyDate::ym(1996, 1);
let date4 = MonthlyDate::ym(1996, 2);
let date5 = MonthlyDate::ym(1996, 3);
let dp1 = DatePoint::new(date1, [1.2]);
let dp2 = DatePoint::new(date2, [1.4]);
let dp3 = DatePoint::new(date3, [1.6]);
let dp4 = DatePoint::new(date4, [1.8]);
let dp5 = DatePoint::new(date5, [2.0]);
let v = vec!( dp1, dp2, dp3, dp4, dp5);
let mut rts: RegularTimeSeries<1> = TimeSeries::new(v).try_into().unwrap();
let date_range = DateRange::new(Some(date2), Some(date4));
rts.with_range(&date_range);
let mut iter = rts.iter(DateRange::new(None, None));
assert_eq!(iter.next().unwrap().date(), date2);
assert_eq!(iter.next().unwrap().date(), date3);
assert_eq!(iter.next().unwrap().date(), date4);
assert!(iter.next().is_none());