File tree Expand file tree Collapse file tree 3 files changed +20
-4
lines changed Expand file tree Collapse file tree 3 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -29,12 +29,12 @@ impl Timespec {
29
29
if self >= other {
30
30
Ok ( if self . t . tv_nsec >= other. t . tv_nsec {
31
31
Duration :: new (
32
- ( self . t . tv_sec - other. t . tv_sec ) as u64 ,
32
+ self . t . tv_sec . wrapping_sub ( other. t . tv_sec ) as u64 ,
33
33
( self . t . tv_nsec - other. t . tv_nsec ) as u32 ,
34
34
)
35
35
} else {
36
36
Duration :: new (
37
- ( self . t . tv_sec - 1 - other. t . tv_sec ) as u64 ,
37
+ ( self . t . tv_sec - 1 ) . wrapping_sub ( other. t . tv_sec ) as u64 ,
38
38
( self . t . tv_nsec + NSEC_PER_SEC - other. t . tv_nsec ) as u32 ,
39
39
)
40
40
} )
Original file line number Diff line number Diff line change @@ -150,12 +150,12 @@ impl Timespec {
150
150
// directly expresses the lower-cost behavior we want from it.
151
151
let ( secs, nsec) = if self . tv_nsec . as_inner ( ) >= other. tv_nsec . as_inner ( ) {
152
152
(
153
- ( self . tv_sec - other. tv_sec ) as u64 ,
153
+ self . tv_sec . wrapping_sub ( other. tv_sec ) as u64 ,
154
154
self . tv_nsec . as_inner ( ) - other. tv_nsec . as_inner ( ) ,
155
155
)
156
156
} else {
157
157
(
158
- ( self . tv_sec - other. tv_sec - 1 ) as u64 ,
158
+ ( self . tv_sec - 1 ) . wrapping_sub ( other. tv_sec ) as u64 ,
159
159
self . tv_nsec . as_inner ( ) + ( NSEC_PER_SEC as u32 ) - other. tv_nsec . as_inner ( ) ,
160
160
)
161
161
} ;
Original file line number Diff line number Diff line change @@ -227,3 +227,19 @@ fn big_math() {
227
227
check ( instant. checked_add ( Duration :: from_secs ( 100 ) ) , Instant :: checked_sub) ;
228
228
check ( instant. checked_add ( Duration :: from_secs ( i64:: MAX as _ ) ) , Instant :: checked_sub) ;
229
229
}
230
+
231
+ #[ test]
232
+ #[ cfg( unix) ]
233
+ fn system_time_duration_since_max_range_on_unix ( ) {
234
+ // Repro regression https://github.com/rust-lang/rust/issues/146228
235
+
236
+ // Min and max values of `SystemTime` on Unix.
237
+ let min = SystemTime :: UNIX_EPOCH - ( Duration :: new ( i64:: MAX as u64 + 1 , 0 ) ) ;
238
+ let max = SystemTime :: UNIX_EPOCH + ( Duration :: new ( i64:: MAX as u64 , 999_999_999 ) ) ;
239
+
240
+ let delta_a = max. duration_since ( min) . expect ( "duration_since overflow" ) ;
241
+ let delta_b = min. duration_since ( max) . expect_err ( "duration_since overflow" ) . duration ( ) ;
242
+
243
+ assert_eq ! ( Duration :: MAX , delta_a) ;
244
+ assert_eq ! ( Duration :: MAX , delta_b) ;
245
+ }
You can’t perform that action at this time.
0 commit comments