@@ -37,34 +37,54 @@ impl Timestamp {
37
37
Timestamp ( Uint64 :: new ( seconds_since_epoch * 1_000_000_000 ) )
38
38
}
39
39
40
+ /// Adds the given amount of days to the timestamp and
41
+ /// returns the result. The original value remains unchanged.
42
+ ///
43
+ /// Panics if the result exceeds the value range of [`Timestamp`].
40
44
#[ must_use = "this returns the result of the operation, without modifying the original" ]
41
45
#[ inline]
42
46
pub const fn plus_days ( & self , addition : u64 ) -> Timestamp {
43
47
self . plus_hours ( addition * 24 )
44
48
}
45
49
50
+ /// Adds the given amount of hours 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`].
46
54
#[ must_use = "this returns the result of the operation, without modifying the original" ]
47
55
#[ inline]
48
56
pub const fn plus_hours ( & self , addition : u64 ) -> Timestamp {
49
57
self . plus_minutes ( addition * 60 )
50
58
}
51
59
60
+ /// Adds the given amount of minutes 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`].
52
64
#[ must_use = "this returns the result of the operation, without modifying the original" ]
53
65
#[ inline]
54
66
pub const fn plus_minutes ( & self , addition : u64 ) -> Timestamp {
55
67
self . plus_seconds ( addition * 60 )
56
68
}
57
69
70
+ /// Adds the given amount of seconds 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`].
58
74
#[ must_use = "this returns the result of the operation, without modifying the original" ]
59
75
#[ inline]
60
76
pub const fn plus_seconds ( & self , addition : u64 ) -> Timestamp {
61
77
self . plus_nanos ( addition * 1_000_000_000 )
62
78
}
63
79
80
+ /// Adds the given amount of nanoseconds 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`].
64
84
#[ must_use = "this returns the result of the operation, without modifying the original" ]
65
85
// no #[inline] here as this could be shared with all the callers
66
86
pub const fn plus_nanos ( & self , addition : u64 ) -> Timestamp {
67
- let nanos = Uint64 :: new ( self . 0 . u64 ( ) + addition) ;
87
+ let nanos = self . 0 . panicking_add ( Uint64 :: new ( addition) ) ;
68
88
Timestamp ( nanos)
69
89
}
70
90
@@ -182,6 +202,13 @@ mod tests {
182
202
assert_eq ! ( sum. 0 . u64 ( ) , 123 ) ;
183
203
}
184
204
205
+ #[ test]
206
+ #[ should_panic( expected = "attempt to add with overflow" ) ]
207
+ fn timestamp_plus_nanos_panics_on_overflow ( ) {
208
+ let max = Timestamp :: from_nanos ( u64:: MAX ) ;
209
+ let _earlier = max. plus_nanos ( 20 ) ;
210
+ }
211
+
185
212
#[ test]
186
213
fn timestamp_minus_seconds ( ) {
187
214
let earlier = Timestamp :: from_seconds ( 123 ) . minus_seconds ( 0 ) ;
0 commit comments