mirror of
https://gitlab.com/veilid/veilid.git
synced 2024-10-01 01:26:08 -04:00
add missing files
This commit is contained in:
parent
1e83cd1349
commit
63b5845a8b
62
veilid-core/src/veilid_api/types/timestamp.rs
Normal file
62
veilid-core/src/veilid_api/types/timestamp.rs
Normal 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))
|
||||
}
|
||||
}
|
33
veilid-core/src/veilid_api/types/timestamp_duration.rs
Normal file
33
veilid-core/src/veilid_api/types/timestamp_duration.rs
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user