cosmwasm_std/
timestamp.rs

1use core::fmt;
2use serde::{Deserialize, Serialize};
3
4use crate::Uint64;
5
6/// A point in time in nanosecond precision.
7///
8/// This type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.
9///
10/// ## Examples
11///
12/// ```
13/// # use cosmwasm_std::Timestamp;
14/// let ts = Timestamp::from_nanos(1_000_000_202);
15/// assert_eq!(ts.nanos(), 1_000_000_202);
16/// assert_eq!(ts.seconds(), 1);
17/// assert_eq!(ts.subsec_nanos(), 202);
18///
19/// let ts = ts.plus_seconds(2);
20/// assert_eq!(ts.nanos(), 3_000_000_202);
21/// assert_eq!(ts.seconds(), 3);
22/// assert_eq!(ts.subsec_nanos(), 202);
23/// ```
24#[derive(
25    Serialize,
26    Deserialize,
27    Copy,
28    Clone,
29    Default,
30    Debug,
31    PartialEq,
32    Eq,
33    PartialOrd,
34    Ord,
35    schemars::JsonSchema,
36)]
37pub struct Timestamp(Uint64);
38
39impl Timestamp {
40    /// Creates a timestamp from nanoseconds since epoch
41    pub const fn from_nanos(nanos_since_epoch: u64) -> Self {
42        Timestamp(Uint64::new(nanos_since_epoch))
43    }
44
45    /// Creates a timestamp from seconds since epoch
46    pub const fn from_seconds(seconds_since_epoch: u64) -> Self {
47        Timestamp(Uint64::new(seconds_since_epoch * 1_000_000_000))
48    }
49
50    /// Adds the given amount of days to the timestamp and
51    /// returns the result. The original value remains unchanged.
52    ///
53    /// Panics if the result exceeds the value range of [`Timestamp`].
54    #[must_use = "this returns the result of the operation, without modifying the original"]
55    #[inline]
56    pub const fn plus_days(&self, addition: u64) -> Timestamp {
57        self.plus_hours(addition * 24)
58    }
59
60    /// Adds the given amount of hours to the timestamp and
61    /// returns the result. The original value remains unchanged.
62    ///
63    /// Panics if the result exceeds the value range of [`Timestamp`].
64    #[must_use = "this returns the result of the operation, without modifying the original"]
65    #[inline]
66    pub const fn plus_hours(&self, addition: u64) -> Timestamp {
67        self.plus_minutes(addition * 60)
68    }
69
70    /// Adds the given amount of minutes to the timestamp and
71    /// returns the result. The original value remains unchanged.
72    ///
73    /// Panics if the result exceeds the value range of [`Timestamp`].
74    #[must_use = "this returns the result of the operation, without modifying the original"]
75    #[inline]
76    pub const fn plus_minutes(&self, addition: u64) -> Timestamp {
77        self.plus_seconds(addition * 60)
78    }
79
80    /// Adds the given amount of seconds to the timestamp and
81    /// returns the result. The original value remains unchanged.
82    ///
83    /// Panics if the result exceeds the value range of [`Timestamp`].
84    #[must_use = "this returns the result of the operation, without modifying the original"]
85    #[inline]
86    pub const fn plus_seconds(&self, addition: u64) -> Timestamp {
87        self.plus_nanos(addition * 1_000_000_000)
88    }
89
90    /// Adds the given amount of nanoseconds to the timestamp and
91    /// returns the result. The original value remains unchanged.
92    ///
93    /// Panics if the result exceeds the value range of [`Timestamp`].
94    #[must_use = "this returns the result of the operation, without modifying the original"]
95    // no #[inline] here as this could be shared with all the callers
96    pub const fn plus_nanos(&self, addition: u64) -> Timestamp {
97        let nanos = self.0.strict_add(Uint64::new(addition));
98        Timestamp(nanos)
99    }
100
101    /// Subtracts the given amount of days from the timestamp and
102    /// returns the result. The original value remains unchanged.
103    ///
104    /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
105    #[must_use = "this returns the result of the operation, without modifying the original"]
106    #[inline]
107    pub const fn minus_days(&self, subtrahend: u64) -> Timestamp {
108        self.minus_hours(subtrahend * 24)
109    }
110
111    /// Subtracts the given amount of hours from the timestamp and
112    /// returns the result. The original value remains unchanged.
113    ///
114    /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
115    #[must_use = "this returns the result of the operation, without modifying the original"]
116    #[inline]
117    pub const fn minus_hours(&self, subtrahend: u64) -> Timestamp {
118        self.minus_minutes(subtrahend * 60)
119    }
120
121    /// Subtracts the given amount of minutes from the timestamp and
122    /// returns the result. The original value remains unchanged.
123    ///
124    /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
125    #[must_use = "this returns the result of the operation, without modifying the original"]
126    #[inline]
127    pub const fn minus_minutes(&self, subtrahend: u64) -> Timestamp {
128        self.minus_seconds(subtrahend * 60)
129    }
130
131    /// Subtracts the given amount of seconds from the timestamp and
132    /// returns the result. The original value remains unchanged.
133    ///
134    /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
135    #[must_use = "this returns the result of the operation, without modifying the original"]
136    #[inline]
137    pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp {
138        self.minus_nanos(subtrahend * 1_000_000_000)
139    }
140
141    /// Subtracts the given amount of nanoseconds from the timestamp and
142    /// returns the result. The original value remains unchanged.
143    ///
144    /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
145    #[must_use = "this returns the result of the operation, without modifying the original"]
146    // no #[inline] here as this could be shared with all the callers
147    pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp {
148        Timestamp(self.0.strict_sub(Uint64::new(subtrahend)))
149    }
150
151    /// Returns nanoseconds since epoch
152    #[inline]
153    pub fn nanos(&self) -> u64 {
154        self.0.u64()
155    }
156
157    /// Returns seconds since epoch (truncate nanoseconds)
158    #[inline]
159    pub fn seconds(&self) -> u64 {
160        self.0.u64() / 1_000_000_000
161    }
162
163    /// Returns nanoseconds since the last whole second (the remainder truncated
164    /// by `seconds()`)
165    #[inline]
166    pub fn subsec_nanos(&self) -> u64 {
167        self.0.u64() % 1_000_000_000
168    }
169}
170
171impl fmt::Display for Timestamp {
172    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173        let whole = self.seconds();
174        let fractional = self.subsec_nanos();
175        write!(f, "{whole}.{fractional:09}")
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182
183    #[test]
184    fn timestamp_from_nanos() {
185        let t = Timestamp::from_nanos(123);
186        assert_eq!(t.0.u64(), 123);
187        let t = Timestamp::from_nanos(0);
188        assert_eq!(t.0.u64(), 0);
189    }
190
191    #[test]
192    fn timestamp_from_seconds() {
193        let t = Timestamp::from_seconds(123);
194        assert_eq!(t.0.u64(), 123_000_000_000);
195        let t = Timestamp::from_seconds(0);
196        assert_eq!(t.0.u64(), 0);
197    }
198
199    #[test]
200    fn timestamp_plus_seconds() {
201        let sum = Timestamp::from_nanos(123).plus_seconds(42);
202        assert_eq!(sum.0.u64(), 42_000_000_123);
203        let sum = Timestamp::from_nanos(123).plus_seconds(0);
204        assert_eq!(sum.0.u64(), 123);
205    }
206
207    #[test]
208    fn timestamp_plus_nanos() {
209        let sum = Timestamp::from_nanos(123).plus_nanos(3);
210        assert_eq!(sum.0.u64(), 126);
211        let sum = Timestamp::from_nanos(123).plus_nanos(0);
212        assert_eq!(sum.0.u64(), 123);
213    }
214
215    #[test]
216    #[should_panic(expected = "attempt to add with overflow")]
217    fn timestamp_plus_nanos_panics_on_overflow() {
218        let max = Timestamp::from_nanos(u64::MAX);
219        let _earlier = max.plus_nanos(20);
220    }
221
222    #[test]
223    fn timestamp_minus_seconds() {
224        let earlier = Timestamp::from_seconds(123).minus_seconds(0);
225        assert_eq!(earlier.0.u64(), 123_000_000_000);
226        let earlier = Timestamp::from_seconds(123).minus_seconds(3);
227        assert_eq!(earlier.0.u64(), 120_000_000_000);
228        let earlier = Timestamp::from_seconds(123).minus_seconds(123);
229        assert_eq!(earlier.0.u64(), 0);
230    }
231
232    #[test]
233    #[should_panic(expected = "attempt to subtract with overflow")]
234    fn timestamp_minus_seconds_panics_on_overflow() {
235        let _earlier = Timestamp::from_seconds(100).minus_seconds(101);
236    }
237
238    #[test]
239    fn timestamp_minus_nanos() {
240        let earlier = Timestamp::from_seconds(123).minus_nanos(0);
241        assert_eq!(earlier.0.u64(), 123_000_000_000);
242        let earlier = Timestamp::from_seconds(123).minus_nanos(3);
243        assert_eq!(earlier.0.u64(), 122_999_999_997);
244        let earlier = Timestamp::from_seconds(123).minus_nanos(123_000_000_000);
245        assert_eq!(earlier.0.u64(), 0);
246    }
247
248    #[test]
249    #[should_panic(expected = "attempt to subtract with overflow")]
250    fn timestamp_minus_nanos_panics_on_overflow() {
251        let _earlier = Timestamp::from_nanos(100).minus_nanos(101);
252    }
253
254    #[test]
255    fn timestamp_plus_days() {
256        let ts = Timestamp::from_seconds(123).plus_days(0);
257        assert_eq!(ts.0.u64(), 123_000_000_000);
258        let ts = Timestamp::from_seconds(123).plus_days(10);
259        assert_eq!(ts.0.u64(), 864_123_000_000_000);
260    }
261
262    #[test]
263    fn timestamp_minus_days() {
264        let ts = Timestamp::from_seconds(123).minus_days(0);
265        assert_eq!(ts.0.u64(), 123_000_000_000);
266        let ts = Timestamp::from_seconds(2 * 86400 + 123).minus_days(1);
267        assert_eq!(ts.0.u64(), 86_523_000_000_000);
268        let ts = Timestamp::from_seconds(86400).minus_days(1);
269        assert_eq!(ts.0.u64(), 0);
270    }
271
272    #[test]
273    fn timestamp_plus_hours() {
274        let ts = Timestamp::from_seconds(123).plus_hours(0);
275        assert_eq!(ts.0.u64(), 123_000_000_000);
276        let ts = Timestamp::from_seconds(123).plus_hours(2);
277        assert_eq!(ts.0.u64(), 123_000_000_000 + 60 * 60 * 2 * 1_000_000_000);
278    }
279
280    #[test]
281    fn timestamp_minus_hours() {
282        let ts = Timestamp::from_seconds(2 * 60 * 60).minus_hours(0);
283        assert_eq!(ts.0.u64(), 2 * 60 * 60 * 1_000_000_000);
284        let ts = Timestamp::from_seconds(2 * 60 * 60 + 123).minus_hours(1);
285        assert_eq!(ts.0.u64(), 60 * 60 * 1_000_000_000 + 123_000_000_000);
286    }
287
288    #[test]
289    fn timestamp_plus_minutes() {
290        let ts = Timestamp::from_seconds(123).plus_minutes(0);
291        assert_eq!(ts.0.u64(), 123_000_000_000);
292        let ts = Timestamp::from_seconds(123).plus_minutes(2);
293        assert_eq!(ts.0.u64(), 123_000_000_000 + 60 * 2 * 1_000_000_000);
294    }
295
296    #[test]
297    fn timestamp_minus_minutes() {
298        let ts = Timestamp::from_seconds(5 * 60).minus_minutes(0);
299        assert_eq!(ts.0.u64(), 5 * 60 * 1_000_000_000);
300        let ts = Timestamp::from_seconds(5 * 60 + 123).minus_minutes(1);
301        assert_eq!(ts.0.u64(), 4 * 60 * 1_000_000_000 + 123_000_000_000);
302    }
303
304    #[test]
305    fn timestamp_nanos() {
306        let sum = Timestamp::from_nanos(123);
307        assert_eq!(sum.nanos(), 123);
308        let sum = Timestamp::from_nanos(0);
309        assert_eq!(sum.nanos(), 0);
310        let sum = Timestamp::from_nanos(987654321000);
311        assert_eq!(sum.nanos(), 987654321000);
312    }
313
314    #[test]
315    fn timestamp_seconds() {
316        let sum = Timestamp::from_nanos(987654321000);
317        assert_eq!(sum.seconds(), 987);
318        let sum = Timestamp::from_seconds(1234567).plus_nanos(8765436);
319        assert_eq!(sum.seconds(), 1234567);
320    }
321
322    #[test]
323    fn timestamp_subsec_nanos() {
324        let sum = Timestamp::from_nanos(987654321000);
325        assert_eq!(sum.subsec_nanos(), 654321000);
326        let sum = Timestamp::from_seconds(1234567).plus_nanos(8765436);
327        assert_eq!(sum.subsec_nanos(), 8765436);
328    }
329
330    #[test]
331    fn timestamp_implements_display() {
332        let embedded = format!("Time: {}", Timestamp::from_nanos(0));
333        assert_eq!(embedded, "Time: 0.000000000");
334        let embedded = format!("Time: {}", Timestamp::from_nanos(1));
335        assert_eq!(embedded, "Time: 0.000000001");
336        let embedded = format!("Time: {}", Timestamp::from_nanos(10));
337        assert_eq!(embedded, "Time: 0.000000010");
338        let embedded = format!("Time: {}", Timestamp::from_nanos(100));
339        assert_eq!(embedded, "Time: 0.000000100");
340        let embedded = format!("Time: {}", Timestamp::from_nanos(1000));
341        assert_eq!(embedded, "Time: 0.000001000");
342        let embedded = format!("Time: {}", Timestamp::from_nanos(10000));
343        assert_eq!(embedded, "Time: 0.000010000");
344        let embedded = format!("Time: {}", Timestamp::from_nanos(100000));
345        assert_eq!(embedded, "Time: 0.000100000");
346        let embedded = format!("Time: {}", Timestamp::from_nanos(1000000));
347        assert_eq!(embedded, "Time: 0.001000000");
348        let embedded = format!("Time: {}", Timestamp::from_nanos(1000000));
349        assert_eq!(embedded, "Time: 0.001000000");
350        let embedded = format!("Time: {}", Timestamp::from_nanos(10000000));
351        assert_eq!(embedded, "Time: 0.010000000");
352        let embedded = format!("Time: {}", Timestamp::from_nanos(100000000));
353        assert_eq!(embedded, "Time: 0.100000000");
354        let embedded = format!("Time: {}", Timestamp::from_nanos(1000000000));
355        assert_eq!(embedded, "Time: 1.000000000");
356        let embedded = format!("Time: {}", Timestamp::from_nanos(10000000000));
357        assert_eq!(embedded, "Time: 10.000000000");
358        let embedded = format!("Time: {}", Timestamp::from_nanos(100000000000));
359        assert_eq!(embedded, "Time: 100.000000000");
360    }
361}