Skip to main content

array_format/
timestamp.rs

1//! Nanosecond-precision timestamp wrapper.
2
3use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
4
5/// Nanoseconds since the Unix epoch (1970-01-01 00:00:00 UTC).
6///
7/// Layout-compatible with `i64` (`#[repr(transparent)]`); on-disk chunk
8/// encoding is identical to a raw `i64` chunk.
9#[repr(transparent)]
10#[derive(
11    Copy,
12    Clone,
13    Debug,
14    Default,
15    PartialEq,
16    Eq,
17    PartialOrd,
18    Ord,
19    Hash,
20    FromBytes,
21    IntoBytes,
22    KnownLayout,
23    Immutable,
24)]
25pub struct TimestampNs(pub i64);
26
27impl TimestampNs {
28    /// Wraps a raw nanoseconds-since-epoch value.
29    pub const fn new(nanos: i64) -> Self {
30        Self(nanos)
31    }
32
33    /// Returns the underlying nanoseconds-since-epoch value.
34    pub const fn nanos(self) -> i64 {
35        self.0
36    }
37}
38
39impl From<i64> for TimestampNs {
40    fn from(v: i64) -> Self {
41        Self(v)
42    }
43}
44
45impl From<TimestampNs> for i64 {
46    fn from(v: TimestampNs) -> i64 {
47        v.0
48    }
49}