use crate::time::epoch::Epoch;
pub struct TimeRange {
epoch_current: Epoch,
epoch_end: Epoch,
step: f64,
positive_step: bool,
}
impl TimeRange {
pub fn new(epoch_start: Epoch, epoch_end: Epoch, step: f64) -> Self {
Self {
epoch_current: epoch_start,
epoch_end,
step: step.abs(),
positive_step: epoch_end > epoch_start,
}
}
}
impl Iterator for TimeRange {
type Item = Epoch;
fn next(&mut self) -> Option<Self::Item> {
if self.epoch_end != self.epoch_current {
let epc = self.epoch_current;
let rem = (self.epoch_end - self.epoch_current).abs();
let h = if self.step < rem { self.step } else { rem };
if self.positive_step {
self.epoch_current += h;
} else {
self.epoch_current -= h;
}
Some(epc)
} else {
None
}
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use crate::time::time_types::TimeSystem;
use crate::utils::testing::setup_global_test_eop;
use super::*;
#[test]
fn test_time_series() {
setup_global_test_eop();
let mut epcv: Vec<Epoch> = Vec::new();
let epcs = Epoch::from_datetime(2022, 1, 1, 0, 0, 0.0, 0.0, TimeSystem::TAI);
let epcf = Epoch::from_datetime(2022, 1, 2, 0, 0, 0.0, 0.0, TimeSystem::TAI);
for (i, e) in TimeRange::new(epcs, epcf, 1.0).enumerate() {
assert_eq!(epcs + i as f64, e);
epcv.push(e);
}
let epcl = Epoch::from_datetime(2022, 1, 1, 23, 59, 59.0, 0.0, TimeSystem::TAI);
assert_eq!(epcv.len(), 86400);
assert!(epcv[epcv.len() - 1] != epcf);
assert!((epcv[epcv.len() - 1] - epcl).abs() < 1.0e-9);
}
#[test]
fn test_time_series_negative() {
setup_global_test_eop();
let mut epcv: Vec<Epoch> = Vec::new();
let epcs = Epoch::from_datetime(2022, 1, 2, 0, 0, 0.0, 0.0, TimeSystem::TAI);
let epcf = Epoch::from_datetime(2022, 1, 1, 0, 0, 0.0, 0.0, TimeSystem::TAI);
let mut epc = Epoch::from_datetime(2022, 1, 2, 0, 0, 0.0, 0.0, TimeSystem::TAI);
for e in TimeRange::new(epcs, epcf, 1.0) {
assert_eq!(epc, e);
epc -= 1;
epcv.push(e);
}
let epcl = Epoch::from_datetime(2022, 1, 1, 0, 0, 1.0, 0.0, TimeSystem::TAI);
assert_eq!(epcv.len(), 86400);
assert!(epcv[epcv.len() - 1] != epcf);
assert!((epcv[epcv.len() - 1] - epcl).abs() < 1.0e-9);
}
}