use core::time::Duration;
#[derive(Clone, Copy, Debug)]
pub struct GenericTime {
#[cfg(feature = "std")]
inner: std::time::SystemTime,
}
impl GenericTime {
#[inline]
pub fn now() -> crate::Result<Self> {
#[cfg(feature = "std")]
{
Ok(Self { inner: std::time::SystemTime::now() })
}
#[cfg(not(feature = "std"))]
{
Err(crate::Error::ItIsNotPossibleToUseTimeInNoStd)
}
}
#[allow(
// Depends on the feature
clippy::used_underscore_binding
)]
#[inline]
pub fn duration_since(&self, _earlier: Self) -> crate::Result<Duration> {
#[cfg(feature = "std")]
{
self.inner.duration_since(_earlier.inner).map_err(|_err| crate::Error::IncorrectHardwareTime)
}
#[cfg(not(feature = "std"))]
{
Err(crate::Error::ItIsNotPossibleToUseTimeInNoStd)
}
}
#[inline]
pub fn elapsed(&self) -> crate::Result<Duration> {
Self::now()?.duration_since(*self)
}
#[inline]
pub fn timestamp(&self) -> crate::Result<Duration> {
#[cfg(feature = "std")]
{
self.duration_since(Self { inner: std::time::UNIX_EPOCH })
}
#[cfg(not(feature = "std"))]
{
Err(crate::Error::ItIsNotPossibleToUseTimeInNoStd)
}
}
}