1 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
2 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // You may not use this file except in accordance with one or both of these
7 //! [`Time`] trait and different implementations. Currently, it's mainly used in tests so we can
8 //! manually advance time.
9 //! Other crates may symlink this file to use it while [`Time`] trait is sealed here.
12 use core::time::Duration;
14 /// A measurement of time.
15 pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
16 /// Returns an instance corresponding to the current moment.
19 /// Returns the amount of time passed between `earlier` and `self`.
20 fn duration_since(&self, earlier: Self) -> Duration;
23 /// A state in which time has no meaning.
24 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
27 impl Time for Eternity {
32 fn duration_since(&self, _earlier: Self) -> Duration {
33 Duration::from_secs(0)
37 impl Sub<Duration> for Eternity {
40 fn sub(self, _other: Duration) -> Self {
45 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
46 #[cfg(feature = "std")]
47 pub struct MonotonicTime(std::time::Instant);
49 /// The amount of time to shift `Instant` forward to prevent overflow when subtracting a `Duration`
50 /// from `Instant::now` on some operating systems (e.g., iOS representing `Instance` as `u64`).
51 #[cfg(feature = "std")]
52 const SHIFT: Duration = Duration::from_secs(10 * 365 * 24 * 60 * 60); // 10 years.
54 #[cfg(feature = "std")]
55 impl Time for MonotonicTime {
57 let instant = std::time::Instant::now().checked_add(SHIFT).expect("Overflow on MonotonicTime instantiation");
61 fn duration_since(&self, earlier: Self) -> Duration {
62 // On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
63 // However, we support rust versions prior to 1.60 and some users appear to have "monotonic
64 // clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
65 // manually check for time going backwards here and return a duration of zero in that case.
66 let now = Self::now();
67 if now.0 > earlier.0 { now.0 - earlier.0 } else { Duration::from_secs(0) }
71 #[cfg(feature = "std")]
72 impl Sub<Duration> for MonotonicTime {
75 fn sub(self, other: Duration) -> Self {
76 let instant = self.0.checked_sub(other).expect("MonotonicTime is not supposed to go backward futher than 10 years");
83 use super::{Time, Eternity};
85 use core::time::Duration;
89 /// Time that can be advanced manually in tests.
90 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
91 pub struct SinceEpoch(Duration);
95 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
98 pub fn advance(duration: Duration) {
99 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
103 impl Time for SinceEpoch {
105 Self(Self::ELAPSED.with(|elapsed| elapsed.get()))
108 fn duration_since(&self, earlier: Self) -> Duration {
113 impl Sub<Duration> for SinceEpoch {
116 fn sub(self, other: Duration) -> Self {
122 fn time_passes_when_advanced() {
123 let now = SinceEpoch::now();
125 SinceEpoch::advance(Duration::from_secs(1));
126 SinceEpoch::advance(Duration::from_secs(1));
128 let later = SinceEpoch::now();
130 assert_eq!(now.0 + Duration::from_secs(2), later.0);
134 fn time_never_passes_in_an_eternity() {
135 let now = Eternity::now();
136 let later = Eternity::now();
138 assert_eq!(later, now);