Struct tinytime::TimeWindow
source · pub struct TimeWindow { /* private fields */ }Expand description
An interval or range of time: [start,end).
Debug-asserts ensure that start <= end.
If compiled in release mode, the invariant of start <= end is maintained, by
correcting invalid use of the API (and setting end to start).
Implementations§
source§impl TimeWindow
impl TimeWindow
sourcepub fn new(start: Time, end: Time) -> Self
pub fn new(start: Time, end: Time) -> Self
Constructs a new TimeWindow.
debug_asserts that start < end. Sets end to start in release mode
if start > end.
sourcepub fn new_checked(start: Time, end: Time) -> Result<Self, TimeWindowError>
pub fn new_checked(start: Time, end: Time) -> Result<Self, TimeWindowError>
Constructs a new TimeWindow. Validates that start <= end and
returns an error if not.
§Examples
assert!(TimeWindow::new_checked(Time::hours(1), Time::hours(2)).is_ok());
assert_eq!(
Err(TimeWindowError::StartAfterEnd),
TimeWindow::new_checked(Time::hours(2), Time::hours(1))
);sourcepub fn epoch_to(end: Time) -> Self
pub fn epoch_to(end: Time) -> Self
Returns TimeWindow with range [Time::EPOCH, end)
pub fn from_minutes(a: i64, b: i64) -> Self
pub fn from_seconds(a: i64, b: i64) -> Self
sourcepub fn from_length_starting_at(length: Duration, start: Time) -> Self
pub fn from_length_starting_at(length: Duration, start: Time) -> Self
Creates time window from start time and length.
Negative lengths are treated as Duration::ZERO.
§Examples
assert_eq!(
TimeWindow::from_seconds(1, 3),
TimeWindow::from_length_starting_at(Duration::seconds(2), Time::seconds(1))
);
assert_eq!(
TimeWindow::from_seconds(1, 1),
TimeWindow::from_length_starting_at(Duration::seconds(-2), Time::seconds(1))
);sourcepub fn from_length_ending_at(length: Duration, end: Time) -> Self
pub fn from_length_ending_at(length: Duration, end: Time) -> Self
Creates time window from length and end time.
Negative lengths are treated as Duration::ZERO.
§Examples
assert_eq!(
TimeWindow::from_seconds(1, 3),
TimeWindow::from_length_ending_at(Duration::seconds(2), Time::seconds(3))
);
assert_eq!(
TimeWindow::from_seconds(3, 3),
TimeWindow::from_length_ending_at(Duration::seconds(-2), Time::seconds(3))
);pub const fn instant(time: Time) -> Self
pub fn widest() -> Self
pub fn instant_seconds(seconds: i64) -> Self
pub const fn start(&self) -> Time
pub const fn end(&self) -> Time
pub fn length(&self) -> Duration
sourcepub fn with_start(&self, new_start: Time) -> Self
pub fn with_start(&self, new_start: Time) -> Self
Creates a new TimeWindow with start set to new_start. If
new_start is greater than or equal to end the start will be set
equal to end.
sourcepub fn with_end(&self, new_end: Time) -> Self
pub fn with_end(&self, new_end: Time) -> Self
Creates a new TimeWindow with end set to new_end. If new_end is
smaller or equal to start, the end will be set to start.
sourcepub fn prepone_start_to(&self, new_start: Time) -> Self
pub fn prepone_start_to(&self, new_start: Time) -> Self
Creates a new TimeWindow with the start preponed to the given value.
If new_start isn’t earlier than the current time window start, a copy
of self is returned.
§Examples
let x = TimeWindow::from_seconds(4, 5);
assert_eq!(
TimeWindow::from_seconds(3, 5),
x.prepone_start_to(Time::seconds(3))
);
assert_eq!(
TimeWindow::from_seconds(4, 5),
x.prepone_start_to(Time::seconds(6))
);sourcepub fn postpone_end_to(&self, new_end: Time) -> Self
pub fn postpone_end_to(&self, new_end: Time) -> Self
Creates a new TimeWindow with the end postponed to the given value.
If new_end isn’t later than the current time window end, a copy of
self is returned.
§Examples
let x = TimeWindow::from_seconds(1, 2);
assert_eq!(
TimeWindow::from_seconds(1, 3),
x.postpone_end_to(Time::seconds(3))
);
assert_eq!(
TimeWindow::from_seconds(1, 2),
x.postpone_end_to(Time::EPOCH)
);sourcepub fn prepone_start_by(&self, duration: Duration) -> Self
pub fn prepone_start_by(&self, duration: Duration) -> Self
Creates a new TimeWindow with the start preponed by the given
duration.
Negative durations are treated as Duration::ZERO.
§Examples
let tw = TimeWindow::from_seconds(8, 9);
assert_eq!(
TimeWindow::from_seconds(5, 9),
tw.prepone_start_by(Duration::seconds(3))
);
assert_eq!(
TimeWindow::from_seconds(8, 9),
tw.prepone_start_by(Duration::seconds(-3))
);sourcepub fn postpone_end_by(&self, duration: Duration) -> Self
pub fn postpone_end_by(&self, duration: Duration) -> Self
Creates a new TimeWindow with the end postponed by the given
duration.
Negative durations are treated as Duration::ZERO.
§Examples
let tw = TimeWindow::from_seconds(1, 2);
assert_eq!(
TimeWindow::from_seconds(1, 5),
tw.postpone_end_by(Duration::seconds(3))
);
assert_eq!(
TimeWindow::from_seconds(1, 2),
tw.postpone_end_by(Duration::seconds(-3))
);sourcepub fn postpone_start_to(&self, new_start: Time) -> Self
pub fn postpone_start_to(&self, new_start: Time) -> Self
Creates a new TimeWindow with the start postponed to the given
value.
Returns a copy of self when the given value isn’t later than the
current time window start. Will never postpone the start past the
end of the time window.
§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
TimeWindow::from_seconds(1, 3),
tw.postpone_start_to(Time::EPOCH)
);
assert_eq!(
TimeWindow::from_seconds(2, 3),
tw.postpone_start_to(Time::seconds(2))
);
assert_eq!(
TimeWindow::from_seconds(3, 3),
tw.postpone_start_to(Time::seconds(3))
);sourcepub fn prepone_end_to(&self, new_end: Time) -> Self
pub fn prepone_end_to(&self, new_end: Time) -> Self
Creates a new TimeWindow with the end preponed to the given value.
Returns a copy of self when the given value isn’t earlier than the
current time window end. Will never prepone the end more than to the
start of the time window.
§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
TimeWindow::from_seconds(1, 3),
tw.prepone_end_to(Time::seconds(4))
);
assert_eq!(
TimeWindow::from_seconds(1, 2),
tw.prepone_end_to(Time::seconds(2))
);
assert_eq!(
TimeWindow::from_seconds(1, 1),
tw.prepone_end_to(Time::EPOCH)
);sourcepub fn postpone_start_shrink_to(&self, new_length: Duration) -> Self
pub fn postpone_start_shrink_to(&self, new_length: Duration) -> Self
Creates a new TimeWindow with the start postponed so that the new
time window length matches the given value.
Returns a copy of self if the new length is smaller than the current
one. Negative length will set the resulting time window length to zero.
§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
TimeWindow::from_seconds(3, 3),
tw.postpone_start_shrink_to(Duration::seconds(-1))
);
assert_eq!(
TimeWindow::from_seconds(3, 3),
tw.postpone_start_shrink_to(Duration::seconds(0))
);
assert_eq!(
TimeWindow::from_seconds(2, 3),
tw.postpone_start_shrink_to(Duration::seconds(1))
);
assert_eq!(
TimeWindow::from_seconds(1, 3),
tw.postpone_start_shrink_to(Duration::seconds(5))
);sourcepub fn prepone_end_shrink_to(&self, new_length: Duration) -> Self
pub fn prepone_end_shrink_to(&self, new_length: Duration) -> Self
Creates a new TimeWindow with the end preponed so that the new time
window length matches the given value.
Returns a copy of self if the new length is smaller than the current
one. Negative length will set the resulting time window length to zero.
§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
TimeWindow::from_seconds(1, 1),
tw.prepone_end_shrink_to(Duration::seconds(-1))
);
assert_eq!(
TimeWindow::from_seconds(1, 1),
tw.prepone_end_shrink_to(Duration::seconds(0))
);
assert_eq!(
TimeWindow::from_seconds(1, 2),
tw.prepone_end_shrink_to(Duration::seconds(1))
);
assert_eq!(
TimeWindow::from_seconds(1, 3),
tw.prepone_end_shrink_to(Duration::seconds(5))
);sourcepub fn contains(&self, that: Time) -> bool
pub fn contains(&self, that: Time) -> bool
Returns true if this time window contains the given time.
§Examples
let mut x = TimeWindow::from_seconds(5, 10);
assert!(!x.contains(Time::seconds(4)));
assert!(x.contains(Time::seconds(5)));
assert!(x.contains(Time::seconds(7)));
assert!(x.contains(Time::seconds(10)));
assert!(!x.contains(Time::seconds(11)));sourcepub fn overlaps(&self, that: &TimeWindow) -> bool
pub fn overlaps(&self, that: &TimeWindow) -> bool
Returns true if this time window overlaps with another one
§Examples
let mut x = TimeWindow::from_seconds(5, 10);
assert!(x.overlaps(&TimeWindow::from_seconds(5, 10)));
assert!(x.overlaps(&TimeWindow::from_seconds(3, 12)));
assert!(x.overlaps(&TimeWindow::from_seconds(6, 9)));
assert!(x.overlaps(&TimeWindow::from_seconds(6, 12)));
assert!(x.overlaps(&TimeWindow::from_seconds(3, 9)));
assert!(!x.overlaps(&TimeWindow::from_seconds(1, 4)));
assert!(!x.overlaps(&TimeWindow::from_seconds(1, 5)));
assert!(!x.overlaps(&TimeWindow::from_seconds(10, 15)));
assert!(!x.overlaps(&TimeWindow::from_seconds(11, 15)));sourcepub fn intersect(&self, that: &TimeWindow) -> Option<TimeWindow>
pub fn intersect(&self, that: &TimeWindow) -> Option<TimeWindow>
Returns time window that is an intersection between this time window and another one. Returns None if time windows don’t overlap.
§Examples
let x = TimeWindow::from_seconds(5, 10);
assert_eq!(
Some(TimeWindow::from_seconds(5, 10)),
x.intersect(&TimeWindow::from_seconds(5, 10)),
"time windows are equal"
);
assert_eq!(
Some(TimeWindow::from_seconds(5, 10)),
x.intersect(&TimeWindow::from_seconds(3, 12)),
"that contains x"
);
assert_eq!(
Some(TimeWindow::from_seconds(6, 9)),
x.intersect(&TimeWindow::from_seconds(6, 9)),
"x contains that"
);
assert_eq!(
Some(TimeWindow::from_seconds(6, 10)),
x.intersect(&TimeWindow::from_seconds(6, 12))
);
assert_eq!(
Some(TimeWindow::from_seconds(5, 9)),
x.intersect(&TimeWindow::from_seconds(3, 9))
);
assert_eq!(
None,
x.intersect(&TimeWindow::from_seconds(1, 4)),
"that is before x"
);
assert_eq!(
Some(TimeWindow::from_seconds(5, 5)),
x.intersect(&TimeWindow::from_seconds(1, 5)),
"single-point intersection"
);
assert_eq!(
Some(TimeWindow::from_seconds(10, 10)),
x.intersect(&TimeWindow::from_seconds(10, 15)),
"single-point intersection"
);
assert_eq!(
None,
x.intersect(&TimeWindow::from_seconds(11, 15)),
"that is after x"
);sourcepub fn shift(&mut self, duration: Duration)
pub fn shift(&mut self, duration: Duration)
Shifts this time window by duration into the future. Affects both
start and end equally, leaving the length untouched.
§Examples
let mut tw = TimeWindow::new(Time::EPOCH, Time::minutes(15));
// shift to the future
tw.shift(Duration::minutes(30));
assert_eq!(TimeWindow::new(Time::minutes(30), Time::minutes(45)), tw);
// shift into the past
tw.shift(-Duration::minutes(15));
assert_eq!(TimeWindow::new(Time::minutes(15), Time::minutes(30)), tw);Trait Implementations§
source§impl Clone for TimeWindow
impl Clone for TimeWindow
source§fn clone(&self) -> TimeWindow
fn clone(&self) -> TimeWindow
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for TimeWindow
impl Debug for TimeWindow
source§impl Default for TimeWindow
impl Default for TimeWindow
source§fn default() -> TimeWindow
fn default() -> TimeWindow
source§impl<'de> Deserialize<'de> for TimeWindow
impl<'de> Deserialize<'de> for TimeWindow
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for TimeWindow
impl Display for TimeWindow
source§impl From<TimeWindow> for (Time, Time)
impl From<TimeWindow> for (Time, Time)
source§fn from(original: TimeWindow) -> Self
fn from(original: TimeWindow) -> Self
source§impl Hash for TimeWindow
impl Hash for TimeWindow
source§impl PartialEq for TimeWindow
impl PartialEq for TimeWindow
source§fn eq(&self, other: &TimeWindow) -> bool
fn eq(&self, other: &TimeWindow) -> bool
self and other values to be equal, and is used
by ==.