add missing files

This commit is contained in:
Christien Rioux 2024-08-01 20:39:44 -05:00
parent 1e83cd1349
commit 63b5845a8b
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,62 @@
/// Microseconds since epoch
use super::*;
aligned_u64_type!(Timestamp);
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", debug_ts(self.as_u64()))
}
}
impl fmt::Debug for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", debug_ts(self.as_u64()))
}
}
impl core::ops::Add<TimestampDuration> for Timestamp {
type Output = Self;
fn add(self, rhs: TimestampDuration) -> Self {
Self(self.0 + rhs.as_u64())
}
}
impl core::ops::AddAssign<TimestampDuration> for Timestamp {
fn add_assign(&mut self, rhs: TimestampDuration) {
self.0 += rhs.as_u64();
}
}
impl core::ops::Sub<Timestamp> for Timestamp {
type Output = TimestampDuration;
fn sub(self, rhs: Timestamp) -> TimestampDuration {
TimestampDuration::new(self.0 - rhs.as_u64())
}
}
impl core::ops::Sub<TimestampDuration> for Timestamp {
type Output = Timestamp;
fn sub(self, rhs: TimestampDuration) -> Timestamp {
Timestamp(self.0 - rhs.as_u64())
}
}
impl core::ops::SubAssign<TimestampDuration> for Timestamp {
fn sub_assign(&mut self, rhs: TimestampDuration) {
self.0 -= rhs.as_u64();
}
}
impl Timestamp {
pub fn now() -> Timestamp {
Timestamp::new(get_timestamp())
}
pub fn saturating_sub(self, rhs: Self) -> TimestampDuration {
TimestampDuration::new(self.0.saturating_sub(rhs.0))
}
}

View File

@ -0,0 +1,33 @@
/// Microseconds since epoch
use super::*;
aligned_u64_type!(TimestampDuration);
aligned_u64_type_default_math_impl!(TimestampDuration);
impl fmt::Display for TimestampDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(&self.0 as &dyn fmt::Display).fmt(f)
}
}
impl FromStr for TimestampDuration {
type Err = <u64 as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(TimestampDuration(u64::from_str(s)?))
}
}
impl fmt::Debug for TimestampDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(&self.0 as &dyn fmt::Debug).fmt(f)
}
}
impl TimestampDuration {
pub fn new_secs<N: num_traits::Unsigned + num_traits::ToPrimitive>(secs: N) -> Self {
TimestampDuration::new(secs.to_u64().unwrap() * 1_000_000u64)
}
pub fn new_ms<N: num_traits::Unsigned + num_traits::ToPrimitive>(ms: N) -> Self {
TimestampDuration::new(ms.to_u64().unwrap() * 1_000u64)
}
}