use std::fmt::Display;
use derive_more::{
Add,
AddAssign,
Div,
Mul,
Sub,
};
use serde::{
Deserialize,
Serialize,
};
pub(crate) const NANOS_PER_SECOND: i64 = 1_000_000_000;
#[derive(
Default,
Debug,
Clone,
Copy,
PartialEq,
PartialOrd,
Ord,
Eq,
Add,
Sub,
Div,
AddAssign,
Mul,
Serialize,
Deserialize,
)]
#[div(forward)]
#[mul(forward)]
#[repr(transparent)]
pub struct TimestampNs(i64);
impl TimestampNs {
#[inline(always)]
pub fn floor_to_nearest_second(self) -> Self {
(self.0 - self.0 % NANOS_PER_SECOND).into()
}
#[inline(always)]
pub fn get(&self) -> i64 {
self.0
}
}
impl From<i64> for TimestampNs {
#[inline(always)]
fn from(value: i64) -> Self {
Self(value)
}
}
impl From<TimestampNs> for i64 {
#[inline(always)]
fn from(val: TimestampNs) -> Self {
val.0
}
}
impl Display for TimestampNs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn timestamp_ns_floor_to_nearest_second() {
let ts = TimestampNs::from(1742475657135330098);
assert_eq!(ts.floor_to_nearest_second(), 1742475657000000000.into());
assert_eq!(i64::from(ts), 1742475657135330098)
}
}