#[cfg(not(feature = "shim"))]
pub use std::time::*;
#[cfg(feature = "shim")]
pub use std::time::{Duration, TryFromFloatSecsError};
#[cfg(feature = "shim")]
pub use shimmed::{Instant, SystemTime, SystemTimeError, UNIX_EPOCH};
#[cfg(feature = "shim")]
mod shimmed {
use std::error::Error;
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
use crate::state;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(Duration);
impl Instant {
pub fn now() -> Instant {
Instant(state::clock_mono_now())
}
pub fn duration_since(&self, earlier: Instant) -> Duration {
self.0.saturating_sub(earlier.0)
}
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.0.saturating_sub(earlier.0)
}
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
self.0.checked_sub(earlier.0)
}
pub fn elapsed(&self) -> Duration {
Instant::now().0.saturating_sub(self.0)
}
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
self.0.checked_add(duration).map(Instant)
}
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
self.0.checked_sub(duration).map(Instant)
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Instant {
self.checked_add(rhs)
.expect("overflow when adding duration to instant")
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}
impl Sub<Duration> for Instant {
type Output = Instant;
fn sub(self, rhs: Duration) -> Instant {
self.checked_sub(rhs)
.expect("overflow when subtracting duration from instant")
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, rhs: Instant) -> Duration {
self.duration_since(rhs)
}
}
impl fmt::Debug for Instant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Instant").field(&self.0).finish()
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime(Duration);
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::ZERO);
impl SystemTime {
pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH;
pub fn now() -> SystemTime {
SystemTime(state::clock_wall_now())
}
pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
if self.0 >= earlier.0 {
Ok(self.0 - earlier.0)
} else {
Err(SystemTimeError(earlier.0 - self.0))
}
}
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
SystemTime::now().duration_since(*self)
}
pub fn checked_add(&self, duration: Duration) -> Option<SystemTime> {
self.0.checked_add(duration).map(SystemTime)
}
pub fn checked_sub(&self, duration: Duration) -> Option<SystemTime> {
self.0.checked_sub(duration).map(SystemTime)
}
}
impl Add<Duration> for SystemTime {
type Output = SystemTime;
fn add(self, rhs: Duration) -> SystemTime {
self.checked_add(rhs)
.expect("overflow when adding duration to system time")
}
}
impl AddAssign<Duration> for SystemTime {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}
impl Sub<Duration> for SystemTime {
type Output = SystemTime;
fn sub(self, rhs: Duration) -> SystemTime {
self.checked_sub(rhs)
.expect("overflow when subtracting duration from system time")
}
}
impl SubAssign<Duration> for SystemTime {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
impl fmt::Debug for SystemTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("SystemTime").field(&self.0).finish()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SystemTimeError(Duration);
impl SystemTimeError {
pub fn duration(&self) -> Duration {
self.0
}
}
impl fmt::Display for SystemTimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "second time provided was later than self")
}
}
impl Error for SystemTimeError {}
}