1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! A uniform time series between two epochs.
//!
//! The iterator yields `start`, `start + step`, `start + 2*step`, ... up
//! to `end`. It is deliberately simple and integer-based: no floating
//! point accumulation is involved.
use crate::duration::Duration;
use crate::epoch::Epoch;
/// An iterator over uniformly spaced epochs.
#[derive(Clone, Debug)]
pub struct TimeSeries {
end: Epoch,
step: Duration,
inclusive: bool,
cursor: Option<Epoch>,
}
impl TimeSeries {
/// Creates a series that includes `end` when it lies exactly on a step.
pub fn inclusive(start: Epoch, end: Epoch, step: Duration) -> Self {
Self {
end,
step,
inclusive: true,
cursor: Some(start),
}
}
/// Creates a series that stops before `end`.
pub fn exclusive(start: Epoch, end: Epoch, step: Duration) -> Self {
Self {
end,
step,
inclusive: false,
cursor: Some(start),
}
}
}
impl Iterator for TimeSeries {
type Item = Epoch;
fn next(&mut self) -> Option<Self::Item> {
if self.step <= Duration::ZERO {
self.cursor = None;
return None;
}
let current = self.cursor?;
let within = if self.inclusive {
current <= self.end
} else {
current < self.end
};
if !within {
self.cursor = None;
return None;
}
self.cursor = current.checked_add(self.step);
Some(current)
}
}