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