diff --git a/src/lib.rs b/src/lib.rs index 60735a0..75629cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -548,6 +548,75 @@ impl TimeWindow { } } + /// Postpones the time window start so that the new duration matches the + /// given value. Is a No-Op if the new duration is smaller than the + /// current one. Negative durations set the result time window size to + /// zero. + /// + /// # Examples + /// ``` + /// # use tinytime::Duration; + /// # use tinytime::TimeWindow; + /// let x = TimeWindow::from_seconds(1, 3); + /// assert_eq!( + /// TimeWindow::from_seconds(3, 3), + /// x.shrink_towards_end_to(Duration::seconds(-1)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(3, 3), + /// x.shrink_towards_end_to(Duration::seconds(0)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(2, 3), + /// x.shrink_towards_end_to(Duration::seconds(1)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(1, 3), + /// x.shrink_towards_end_to(Duration::seconds(5)) + /// ); + /// ``` + pub fn shrink_towards_end_to(self, new_duration: Duration) -> TimeWindow { + let duration = new_duration + .min(self.duration()) // Resize only if new duration is smaller than the current one + .max(Duration::ZERO); // Make sure the new duration is non-negative + + TimeWindow::from_end(duration, self.end) + } + + /// Prepones the time window end so that the new duration matches the given + /// value. Is a No-Op if the new duration is smaller than the current + /// one. Negative durations set the result time window size to zero. + /// + /// # Examples + /// ``` + /// # use tinytime::Duration; + /// # use tinytime::TimeWindow; + /// let x = TimeWindow::from_seconds(1, 3); + /// assert_eq!( + /// TimeWindow::from_seconds(1, 1), + /// x.shrink_towards_start_to(Duration::seconds(-1)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(1, 1), + /// x.shrink_towards_start_to(Duration::seconds(0)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(1, 2), + /// x.shrink_towards_start_to(Duration::seconds(1)) + /// ); + /// assert_eq!( + /// TimeWindow::from_seconds(1, 3), + /// x.shrink_towards_start_to(Duration::seconds(5)) + /// ); + /// ``` + pub fn shrink_towards_start_to(self, new_duration: Duration) -> TimeWindow { + let duration = new_duration + .min(self.duration()) // Resize only if new duration is smaller than the current one + .max(Duration::ZERO); // Make sure the new duration is non-negative + + TimeWindow::from_duration(self.start, duration) + } + /// Returns true if this time window contains the given time. /// # Examples ///