#[cfg(all(feature = "enable", feature = "freertos", not(feature = "std")))]
pub(crate) use veecle_osal_freertos::time::*;
#[cfg(all(feature = "enable", feature = "std"))]
pub(crate) use veecle_osal_std::time::*;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Timestamp(pub u64);
impl Timestamp {
pub fn as_nanos(&self) -> u64 {
self.0
}
}
impl core::fmt::Display for Timestamp {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.0, f)
}
}
#[cfg(feature = "enable")]
pub(crate) fn now() -> Timestamp {
#[cfg(feature = "system_time")]
let timestamp_micros = match Time::duration_since_epoch() {
Ok(duration) => duration.as_micros(),
Err(SystemTimeError::Unsynchronized) => Time::now()
.duration_since(Instant::MIN)
.expect("should be able to get a duration since the MIN value")
.as_micros(),
Err(SystemTimeError::EpochIsLaterThanStartTime) => {
panic!(
"Failed to get duration since epoch: {:?}",
SystemTimeError::EpochIsLaterThanStartTime
);
}
};
#[cfg(not(feature = "system_time"))]
let timestamp_micros = Time::now()
.duration_since(Instant::MIN)
.expect("should be able to get a duration since the MIN value")
.as_micros();
Timestamp(timestamp_micros * 1000)
}