clack_common/utils/
timestamp.rs1use std::fmt::{Debug, Display, Formatter};
2use std::num::NonZeroU64;
3use std::time::Duration;
4
5#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
7pub struct Timestamp(NonZeroU64);
8
9impl Timestamp {
10 #[inline]
14 pub const fn from_raw(raw: u64) -> Option<Self> {
15 match NonZeroU64::new(raw) {
16 Some(raw) => Some(Self(raw)),
17 None => None,
18 }
19 }
20
21 #[inline]
23 pub const fn seconds_since_epoch(self) -> u64 {
24 self.0.get()
25 }
26
27 #[inline]
29 pub fn duration_since_epoch(self) -> Duration {
30 Duration::from_secs(self.0.get())
31 }
32
33 #[inline]
37 pub fn optional_to_raw(timestamp: Option<Self>) -> u64 {
38 match timestamp {
39 None => 0,
40 Some(t) => t.0.get(),
41 }
42 }
43}
44
45impl Debug for Timestamp {
46 #[inline]
47 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48 Display::fmt(&self.0.get(), f)
49 }
50}