use core::time::Duration;
use crate::Result;
use crate::sys;
#[cfg_attr(not(feature = "std"), allow(unused_imports))]
use crate::ErrorKind;
const NANOS_PER_SEC: u32 = 1_000_000_000;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp {
pub secs: i64,
pub nanos: u32,
}
impl Timestamp {
pub const UNIX_EPOCH: Self = Self { secs: 0, nanos: 0 };
const fn is_valid(self) -> bool {
self.nanos < NANOS_PER_SEC
}
pub const fn checked_add(self, duration: Duration) -> Option<Self> {
if !self.is_valid() || duration.as_secs() > i64::MAX as u64 {
return None;
}
let Some(mut secs) = self.secs.checked_add(duration.as_secs() as i64) else {
return None;
};
let mut nanos = self.nanos + duration.subsec_nanos();
if nanos >= NANOS_PER_SEC {
nanos -= NANOS_PER_SEC;
secs = match secs.checked_add(1) {
Some(secs) => secs,
None => return None,
};
}
Some(Self { secs, nanos })
}
pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
if !self.is_valid() || duration.as_secs() > i64::MAX as u64 {
return None;
}
let Some(mut secs) = self.secs.checked_sub(duration.as_secs() as i64) else {
return None;
};
let mut nanos = self.nanos;
if nanos < duration.subsec_nanos() {
nanos += NANOS_PER_SEC;
secs = match secs.checked_sub(1) {
Some(secs) => secs,
None => return None,
};
}
nanos -= duration.subsec_nanos();
Some(Self { secs, nanos })
}
pub const fn duration_since(self, earlier: Self) -> Option<Duration> {
if !self.is_valid() || !earlier.is_valid() {
return None;
}
let mut secs = self.secs as i128 - earlier.secs as i128;
let mut nanos = self.nanos as i64 - earlier.nanos as i64;
if nanos < 0 {
nanos += NANOS_PER_SEC as i64;
secs -= 1;
}
if secs < 0 || secs > u64::MAX as i128 {
return None;
}
Some(Duration::new(secs as u64, nanos as u32))
}
pub const fn from_unix(duration: Duration) -> Option<Self> {
Self::UNIX_EPOCH.checked_add(duration)
}
pub const fn to_unix(self) -> Option<Duration> {
self.duration_since(Self::UNIX_EPOCH)
}
}
#[cfg(feature = "std")]
impl From<std::time::SystemTime> for Timestamp {
fn from(time: std::time::SystemTime) -> Self {
let epoch = std::time::SystemTime::UNIX_EPOCH;
let converted = match time.duration_since(epoch) {
Ok(after) => Self::UNIX_EPOCH.checked_add(after),
Err(before) => Self::UNIX_EPOCH.checked_sub(before.duration()),
};
converted.unwrap_or(if time < epoch {
Self {
secs: i64::MIN,
nanos: 0,
}
} else {
Self {
secs: i64::MAX,
nanos: NANOS_PER_SEC - 1,
}
})
}
}
#[cfg(feature = "std")]
impl TryFrom<Timestamp> for std::time::SystemTime {
type Error = crate::Error;
fn try_from(time: Timestamp) -> Result<Self> {
let epoch = Self::UNIX_EPOCH;
let converted = if !time.is_valid() {
None
} else if time.secs >= 0 {
epoch.checked_add(Duration::new(time.secs as u64, time.nanos))
} else {
epoch
.checked_sub(Duration::from_secs(time.secs.unsigned_abs()))
.and_then(|t| t.checked_add(Duration::new(0, time.nanos)))
};
converted.ok_or(ErrorKind::InvalidInput.into())
}
}
pub fn now() -> Result<Timestamp> {
sys::time::now()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Clock {
Monotonic,
Boottime,
ProcessCpu,
ThreadCpu,
}
impl Clock {
pub fn now(self) -> Result<Duration> {
sys::time::clock(self)
}
pub fn resolution(self) -> Result<Duration> {
sys::time::resolution(self)
}
}
pub fn sleep(duration: Duration) -> Result<()> {
if duration.is_zero() {
return Ok(());
}
sys::time::sleep(duration)
}
pub fn sleep_until(deadline: Duration) -> Result<()> {
sys::time::sleep_until(deadline)
}