omaha_client/time.rs
1// Copyright 2020 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9//! The `omaha_client::time` module provides a set of types and traits to allow for the expressing
10//! of time using both a wall-time clock, and a monotonic clock.
11//!
12//! The motivation for this is that wall-time is subject to being artibtrarily moved (forwards or
13//! backwards as the system's timekeeper decides is necessary), to keep it in sync with external
14//! systems.
15//!
16//! The monotonic clock, on the other hand, provides a consistently moving forward notion of time,
17//! but one that is not anchored at any particular external point in time. It's epoch is therefore
18//! somewhat opaque by nature. Rust's std::time::Instant takes this to the extreme of making the
19//! underlying value completely hidden from callers.
20//!
21//! One aspect of the `TimeSource` trait and the `ComplexTime` type is that they provide a way to
22//! pair the two timelines, and construct a time that is given in terms of each of these dimensions.
23//!
24//! What it doesn't try to do, is so that these pairings are the _same_ time. They can be
25//! observations made concurrently (as in the case of `TimeSource::now() -> ComplexTime`),
26//! or they can be bounds for when an event in the future can happen:
27//!
28//! ```
29//! use omaha_client::time::{ComplexTime, MockTimeSource, TimeSource, Timer};
30//! use omaha_client::time::timers::MockTimer;
31//! use std::time::{Duration, SystemTime};
32//! let past_event_wall_time: SystemTime = SystemTime::now();
33//! let source = MockTimeSource::new_from_now();
34//! let duration_to_next = Duration::from_secs(1*60*60); // one hour
35//! let rough_next_event_time = ComplexTime{
36//! wall: past_event_wall_time + duration_to_next,
37//! mono: source.now_in_monotonic() + duration_to_next
38//! };
39//! let mut timer = MockTimer::new();
40//! timer.wait_until(rough_next_event_time);
41//! ```
42//!
43//! The above setups up a `ComplexTime` as a bound that based on an expected wall time, and
44//! monotonic time relative to `now`, such that the event can be described as "at time X, or within
45//! an hour" when used with `Timer::wait_until`, or "after time X, at least an hour from now", if
46//! used with `Timer::wait_until_all`.
47//!
48//! # Usage Guidelines
49//!
50//! The `ComplexTime` and `PartialComplexTime` structs give the ability to represent a number of
51//! states of knowledge about a time.
52//!
53//! When modeling the known time for something:
54//!
55//! * `ComplexTime` - When both wall and monotonic times are always known
56//! * `PartialComplexTime` - When some time (wall, monotonic, or both) is always known
57//! * `Option<ComplexTime> - When time is either known for both timelines, or not at all.
58//! * `Option<PartialComplexTime> - Situations where time can be in any of 4 states:
59//! * None whatsoever
60//! * Only wall time
61//! * Only monotonic time
62//! * Both are known
63//!
64//! When modeling the time required (e.g. timer waits):
65//! * `ComplexTime` - When both wall and monotonic times are always required
66//! * `PartialComplexTime` - When some time (wall, monotonic, or both) is always required, but any
67//! or both will suffice.
68//!
69
70use chrono::{DateTime, Utc};
71use futures::future::BoxFuture;
72use std::{
73 fmt::{Debug, Display},
74 hash::Hash,
75 time::{Duration, Instant, SystemTime},
76};
77
78// NOTE: Implementations for the main types of this module have been moved to inner modules that
79// that are exposed via `pub use`, so that it's easier to read through the declarations of
80// the main types and see how they are meant to be used together. Implementations are in the
81// same order as the declarations of the types.
82
83// Implementations and tests for `ComplexTime` and `PartialComplexTime`
84mod complex;
85pub use complex::system_time_conversion;
86
87/// This is a complete `ComplexTime`, which has values on both the wall clock timeline and the
88/// monotonic clock timeline.
89///
90/// It is not necessarily intended that both values refer to the same moment. They can, or they
91/// can refer to a time on each clock's respective timeline, e.g. for use with the `Timer` trait.
92///
93/// The `ComplexTime` type implements all the standard math operations in `std::ops` that are
94/// implemented for both `std::time::SystemTime` and `std::time::Instant`. Like those
95/// implementations, it will panic on overflow.
96#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
97pub struct ComplexTime {
98 pub wall: SystemTime,
99 pub mono: Instant,
100}
101impl ComplexTime {
102 /// Truncate the submicrosecond part of the walltime.
103 pub fn truncate_submicrosecond_walltime(&self) -> ComplexTime {
104 let nanos_in_one_micro = Duration::from_micros(1).as_nanos();
105 let submicrosecond_nanos = match self.wall_duration_since(SystemTime::UNIX_EPOCH) {
106 Ok(duration) => duration.as_nanos() % nanos_in_one_micro,
107 Err(e) => nanos_in_one_micro - e.duration().as_nanos() % nanos_in_one_micro,
108 };
109 ComplexTime {
110 wall: self.wall - Duration::from_nanos(submicrosecond_nanos as u64),
111 mono: self.mono,
112 }
113 }
114 /// Compute the Duration since the given SystemTime, for the SystemTime component of this
115 /// ComplexTime. If this ComplexTime's SystemTime is before the given time, the Duration
116 /// is returned as Err (same as `SystemTime::duration_since()`)
117 pub fn wall_duration_since(
118 &self,
119 earlier: impl Into<SystemTime>,
120 ) -> Result<Duration, std::time::SystemTimeError> {
121 self.wall.duration_since(earlier.into())
122 }
123
124 /// Returns `true` if this ComplexTime is after either the SystemTime or the Instant of the
125 /// given PartialComplexTime.
126 pub fn is_after_or_eq_any(&self, other: impl Into<PartialComplexTime>) -> bool {
127 match other.into() {
128 PartialComplexTime::Wall(w) => self.wall >= w,
129 PartialComplexTime::Monotonic(m) => self.mono >= m,
130 PartialComplexTime::Complex(c) => self.wall >= c.wall || self.mono >= c.mono,
131 }
132 }
133}
134
135// Implementations for `Display`, `Add`, `AddAssign`, `Sub`, `SubAssign` are found in
136// `mod complex::complex_time_impls`
137//
138// Implementations for `From<>` are found in `mod complex::complex_time_type_conversions`
139
140/// `PartialComplexTime` provides a `std::interator::EitherOrBoth`-like type which is specifically
141/// for holding either one, or both, of the time types that make up a `ComplexTime`. It's a type
142/// that holds a value for at least one of the timelines.
143///
144/// The important differentiation of this vs. a struct such as:
145/// ```
146/// use std::time::SystemTime;
147/// struct MaybeBoth {
148/// wall: Option<SystemTime>,
149/// monotonic: Option<SystemTime>
150///}
151/// ```
152/// is that there is no valid `(None, None)` state for this type to be in, and so code that uses can
153/// be certain that _some_ time is specified.
154///
155/// Like `ComplexTime`, `PartialComplexTime` implements all the standard math operations in
156/// `std::ops` that are implemented for both `std::time::SystemTime` and `std::time:Instant`. Like
157/// those implementations, they will panic on overflow.
158#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
159pub enum PartialComplexTime {
160 /// Just a wall time.
161 Wall(SystemTime),
162 /// Just a monotonic time.
163 Monotonic(Instant),
164 /// Both a wall and a monotonic time.
165 Complex(ComplexTime),
166}
167impl PartialComplexTime {
168 /// Return the SystemTime component, if one exists.
169 pub fn checked_to_system_time(self) -> Option<SystemTime> {
170 self.destructure().0
171 }
172
173 /// Return the Instant component, if one exists.
174 pub fn checked_to_instant(self) -> Option<Instant> {
175 self.destructure().1
176 }
177
178 /// Convert the SystemTime component of this PartialComplexTime into i64 microseconds from
179 /// the UNIX Epoch. Provides coverage over +/- approx 30,000 years from 1970-01-01 UTC.
180 ///
181 /// Returns None if it doesn't have a wall time or on overflow (instead of panicking)
182 pub fn checked_to_micros_since_epoch(self) -> Option<i64> {
183 self.checked_to_system_time()
184 .and_then(system_time_conversion::checked_system_time_to_micros_from_epoch)
185 }
186
187 /// Return the PartialComplexTime::Wall that represents the same time as the specified
188 /// microseconds from the UNIX Epoch (1970-01-01 UTC)
189 pub fn from_micros_since_epoch(micros: i64) -> Self {
190 PartialComplexTime::from(system_time_conversion::micros_from_epoch_to_system_time(
191 micros,
192 ))
193 }
194
195 /// Return a new ComplexTime that's based on the time values of this PartialComplexTime,
196 /// setting either unknown field from the passed-in ComplexTime.
197 pub fn complete_with(&self, complex: ComplexTime) -> ComplexTime {
198 let (system, instant) = self.destructure();
199 ComplexTime::from((
200 system.unwrap_or(complex.wall),
201 instant.unwrap_or(complex.mono),
202 ))
203 }
204
205 /// Destructure the PartialComplexTime into it's two components, each as an Option.
206 pub fn destructure(&self) -> (Option<SystemTime>, Option<Instant>) {
207 match *self {
208 PartialComplexTime::Wall(w) => (Some(w), None),
209 PartialComplexTime::Monotonic(m) => (None, Some(m)),
210 PartialComplexTime::Complex(c) => (Some(c.wall), Some(c.mono)),
211 }
212 }
213}
214
215// Implementations for `Display`, `Add`, `AddAssign`, `Sub`, `SubAssign` are found in
216// `mod complex::partial_complex_time_impls`
217//
218// Implementations for `From<>` are found in `mod complex::partial_complex_time_type_conversions`
219
220/// Trait for timers that understand how to work with the `ComplexTime` and `PartialComplexTime`
221/// types.
222///
223/// When using a `PartialComplexTime`, the trait defines Fns for waiting until any of the times have
224/// been reached, or until all of them have been reached.
225pub trait Timer {
226 /// Wait until at least one of the given time bounds has been reached.
227 fn wait_until(&mut self, time: impl Into<PartialComplexTime>) -> BoxFuture<'static, ()>;
228
229 /// Wait for the given duration (from now).
230 fn wait_for(&mut self, duration: Duration) -> BoxFuture<'static, ()>;
231}
232// Implementations and tests for test `Timers`. Not marked as #[cfg(test)] to allow use in tests
233// in crates that use this.
234pub mod timers;
235
236/// `TimeSource` is a trait for providing access to both the "System" (aka "Wall") time for
237/// platform, as well as its monotonic time.
238pub trait TimeSource {
239 /// Returns the current wall time.
240 fn now_in_walltime(&self) -> SystemTime;
241
242 /// Returns the current montonic time.
243 fn now_in_monotonic(&self) -> Instant;
244
245 /// Returns the current ComplexTime (both wall and monotonic times).
246 fn now(&self) -> ComplexTime;
247}
248
249// Implementations and tests for `TimeSource`
250pub mod time_source;
251pub use time_source::MockTimeSource;
252pub use time_source::StandardTimeSource;
253
254impl<T> TimeSource for &T
255where
256 T: TimeSource,
257{
258 fn now_in_walltime(&self) -> SystemTime {
259 (*self).now_in_walltime()
260 }
261 fn now_in_monotonic(&self) -> Instant {
262 (*self).now_in_monotonic()
263 }
264 fn now(&self) -> ComplexTime {
265 (*self).now()
266 }
267}
268
269/// Helper struct for providing a consistent, readable `SystemTime`.
270///
271/// This displays a `SystemTime` in a human-readable date+time in UTC plus the raw `[seconds].[ns]`
272/// since epoch of the SystemTime.
273///
274/// # Example
275/// ```
276/// use omaha_client::time::ReadableSystemTime;
277/// use std::time::{Duration, SystemTime};
278/// let sys_time = SystemTime::UNIX_EPOCH + Duration::from_nanos(994610096026420000);
279///
280/// assert_eq!(
281/// format!("{}", ReadableSystemTime(sys_time)),
282/// "2001-07-08 16:34:56.026 UTC (994610096.026420000)"
283/// );
284/// ```
285pub struct ReadableSystemTime(pub SystemTime);
286impl Display for ReadableSystemTime {
287 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
288 let format = DateTime::<Utc>::from(self.0).format("%Y-%m-%d %H:%M:%S%.3f %Z (%s%.9f)");
289 Display::fmt(&format, f)
290 }
291}
292impl Debug for ReadableSystemTime {
293 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
294 Display::fmt(self, f)
295 }
296}
297
298#[cfg(test)]
299mod tests_for_readable_system_time {
300 use super::*;
301
302 #[test]
303 fn test_readable_system_time() {
304 let sys_time = SystemTime::UNIX_EPOCH + Duration::from_nanos(994610096026420000);
305
306 assert_eq!(
307 format!("{}", ReadableSystemTime(sys_time)),
308 "2001-07-08 16:34:56.026 UTC (994610096.026420000)"
309 );
310 }
311}