#[doc(no_inline)]
pub use core::time::Duration;
#[cfg(not(semihosting_no_duration_checked_float))]
#[doc(no_inline)]
pub use core::time::TryFromFloatSecsError;
use core::{
fmt,
ops::{Add, AddAssign, Sub, SubAssign},
};
use crate::sys::time;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(time::Instant);
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SystemTime(time::SystemTime);
#[derive(Clone, Debug)]
pub struct SystemTimeError(Duration);
impl Instant {
#[must_use]
pub fn now() -> Self {
Self(time::Instant::now().unwrap())
}
#[must_use]
pub fn duration_since(&self, earlier: Self) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}
#[must_use]
pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
self.0.checked_sub_instant(&earlier.0)
}
#[must_use]
pub fn saturating_duration_since(&self, earlier: Self) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}
#[must_use]
pub fn elapsed(&self) -> Duration {
Self::now() - *self
}
pub fn checked_add(&self, duration: Duration) -> Option<Self> {
self.0.checked_add_duration(&duration).map(Self)
}
pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
self.0.checked_sub_duration(&duration).map(Self)
}
}
impl Add<Duration> for Instant {
type Output = Self;
fn add(self, other: Duration) -> Self {
self.checked_add(other).expect("overflow when adding duration to instant")
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}
impl Sub<Duration> for Instant {
type Output = Self;
fn sub(self, other: Duration) -> Self::Output {
self.checked_sub(other).expect("overflow when subtracting duration from instant")
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, other: Self) -> Self::Output {
self.duration_since(other)
}
}
impl fmt::Debug for Instant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl SystemTime {
pub const UNIX_EPOCH: Self = Self(time::UNIX_EPOCH);
#[must_use]
pub fn now() -> Self {
Self(time::SystemTime::now().unwrap())
}
pub fn duration_since(&self, earlier: Self) -> Result<Duration, SystemTimeError> {
self.0.sub_time(&earlier.0).map_err(SystemTimeError)
}
pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
Self::now().duration_since(*self)
}
pub fn checked_add(&self, duration: Duration) -> Option<Self> {
self.0.checked_add_duration(&duration).map(Self)
}
pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
self.0.checked_sub_duration(&duration).map(Self)
}
}
impl Add<Duration> for SystemTime {
type Output = Self;
fn add(self, dur: Duration) -> Self::Output {
self.checked_add(dur).expect("overflow when adding duration to instant")
}
}
impl AddAssign<Duration> for SystemTime {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}
impl Sub<Duration> for SystemTime {
type Output = Self;
fn sub(self, dur: Duration) -> Self::Output {
self.checked_sub(dur).expect("overflow when subtracting duration from instant")
}
}
impl SubAssign<Duration> for SystemTime {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}
impl fmt::Debug for SystemTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl SystemTimeError {
#[must_use]
pub fn duration(&self) -> Duration {
self.0
}
}
#[cfg(not(semihosting_no_error_in_core))]
impl core::error::Error for SystemTimeError {}
impl fmt::Display for SystemTimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("second time provided was later than self")
}
}