1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
use crate::time::{Duration, SystemTime, SystemTimeError}; use std::time; /// A reference to a system clock, useful for talking to external entities like /// the file system or other processes. /// /// This does not directly correspond to anything in `std`, however its methods /// correspond to [methods in `std::time::SystemTime`]. /// /// [methods in `std::time::SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html pub struct SystemClock(()); impl SystemClock { /// An anchor in time which can be used to create new `SystemTime` /// instances or learn about where in time a `SystemTime` lies. /// /// This corresponds to [`std::time::SystemTime::UNIX_EPOCH`]. /// /// [`std::time::SystemTime::UNIX_EPOCH`]: https://doc.rust-lang.org/std/time/constant.UNIX_EPOCH.html pub const UNIX_EPOCH: SystemTime = SystemTime { std: time::SystemTime::UNIX_EPOCH, }; /// Constructs a new instance of `Self`. /// /// # Safety /// /// This is unsafe because access to clocks is an ambient authority. #[inline] pub const unsafe fn new() -> Self { Self(()) } /// Returns an instant corresponding to "now". /// /// This corresponds to [`SystemTime::now`]. /// /// [`SystemTime::now`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#method.now #[inline] pub fn now(&self) -> SystemTime { SystemTime::from_std(time::SystemTime::now()) } /// Returns the amount of time elapsed since this instant was created. /// /// This corresponds to [`SystemTime::elapsed`]. /// /// [`SystemTime::elapsed`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#method.elapsed #[inline] pub fn elapsed(&self, system_time: SystemTime) -> Result<Duration, SystemTimeError> { system_time.std.elapsed() } }