omaha_client/
clock.rs

1// Copyright 2019 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
9use std::time::SystemTime;
10
11#[cfg(not(test))]
12pub fn now() -> SystemTime {
13    SystemTime::now()
14}
15
16#[cfg(test)]
17pub use mock::now;
18
19#[cfg(test)]
20pub mod mock {
21    use super::*;
22    use std::cell::RefCell;
23
24    thread_local!(static MOCK_TIME: RefCell<SystemTime> = RefCell::new(SystemTime::now()));
25
26    pub fn now() -> SystemTime {
27        MOCK_TIME.with(|time| *time.borrow())
28    }
29
30    pub fn set(new_time: SystemTime) {
31        MOCK_TIME.with(|time| *time.borrow_mut() = new_time);
32    }
33}