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.
89use std::time::SystemTime;
1011#[cfg(not(test))]
12pub fn now() -> SystemTime {
13 SystemTime::now()
14}
1516#[cfg(test)]
17pub use mock::now;
1819#[cfg(test)]
20pub mod mock {
21use super::*;
22use std::cell::RefCell;
2324thread_local!(static MOCK_TIME: RefCell<SystemTime> = RefCell::new(SystemTime::now()));
2526pub fn now() -> SystemTime {
27 MOCK_TIME.with(|time| *time.borrow())
28 }
2930pub fn set(new_time: SystemTime) {
31 MOCK_TIME.with(|time| *time.borrow_mut() = new_time);
32 }
33}