packet_strata/
timestamp.rs1use chrono::DateTime;
2use serde::{Deserialize, Serialize};
3use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
6pub struct Timestamp(pub u64);
7
8impl fmt::Display for Timestamp {
9 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10 if let Some(date_time) = DateTime::from_timestamp(
11 self.0 as i64 / 1_000_000_000,
12 (self.0 % 1_000_000_000) as u32,
13 ) {
14 if !f.alternate() {
15 return write!(f, "{}", date_time.format("%Y-%m-%d %H:%M:%S%.6f UTC"));
16 }
17 }
18
19 write!(
20 f,
21 "{}.{:09}",
22 self.0 / 1_000_000_000,
23 self.0 % 1_000_000_000
24 )
25 }
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
29pub struct Nanoseconds(pub u64);
30
31impl fmt::Display for Nanoseconds {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(
34 f,
35 "{}.{:09}",
36 self.0 / 1_000_000_000,
37 self.0 % 1_000_000_000
38 )
39 }
40}