1use core::fmt;
2use saa_schema::saa_str_struct;
3use super::uints::Uint64;
4
5#[saa_str_struct]
6pub struct Timestamp(Uint64);
7
8impl Timestamp {
9 pub const fn from_nanos(nanos_since_epoch: u64) -> Self {
11 Timestamp(Uint64::new(nanos_since_epoch))
12 }
13
14 pub const fn from_seconds(seconds_since_epoch: u64) -> Self {
16 Timestamp(Uint64::new(seconds_since_epoch * 1_000_000_000))
17 }
18
19 #[must_use = "this returns the result of the operation, without modifying the original"]
24 #[inline]
25 pub const fn plus_days(&self, addition: u64) -> Timestamp {
26 self.plus_hours(addition * 24)
27 }
28
29 #[must_use = "this returns the result of the operation, without modifying the original"]
34 #[inline]
35 pub const fn plus_hours(&self, addition: u64) -> Timestamp {
36 self.plus_minutes(addition * 60)
37 }
38
39 #[must_use = "this returns the result of the operation, without modifying the original"]
44 #[inline]
45 pub const fn plus_minutes(&self, addition: u64) -> Timestamp {
46 self.plus_seconds(addition * 60)
47 }
48
49 #[must_use = "this returns the result of the operation, without modifying the original"]
54 #[inline]
55 pub const fn plus_seconds(&self, addition: u64) -> Timestamp {
56 self.plus_nanos(addition * 1_000_000_000)
57 }
58
59 #[must_use = "this returns the result of the operation, without modifying the original"]
64 pub const fn plus_nanos(&self, addition: u64) -> Timestamp {
66 let nanos = self.0.strict_add(Uint64::new(addition));
67 Timestamp(nanos)
68 }
69
70 #[must_use = "this returns the result of the operation, without modifying the original"]
75 #[inline]
76 pub const fn minus_days(&self, subtrahend: u64) -> Timestamp {
77 self.minus_hours(subtrahend * 24)
78 }
79
80 #[must_use = "this returns the result of the operation, without modifying the original"]
85 #[inline]
86 pub const fn minus_hours(&self, subtrahend: u64) -> Timestamp {
87 self.minus_minutes(subtrahend * 60)
88 }
89
90 #[must_use = "this returns the result of the operation, without modifying the original"]
95 #[inline]
96 pub const fn minus_minutes(&self, subtrahend: u64) -> Timestamp {
97 self.minus_seconds(subtrahend * 60)
98 }
99
100 #[must_use = "this returns the result of the operation, without modifying the original"]
105 #[inline]
106 pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp {
107 self.minus_nanos(subtrahend * 1_000_000_000)
108 }
109
110 #[must_use = "this returns the result of the operation, without modifying the original"]
115 pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp {
117 Timestamp(self.0.strict_sub(Uint64::new(subtrahend)))
118 }
119
120 #[inline]
122 pub fn nanos(&self) -> u64 {
123 self.0.u64()
124 }
125
126 #[inline]
128 pub fn seconds(&self) -> u64 {
129 self.0.u64() / 1_000_000_000
130 }
131
132 #[inline]
135 pub fn subsec_nanos(&self) -> u64 {
136 self.0.u64() % 1_000_000_000
137 }
138}
139
140impl fmt::Display for Timestamp {
141 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142 let whole = self.seconds();
143 let fractional = self.subsec_nanos();
144 write!(f, "{whole}.{fractional:09}")
145 }
146}