@@ -227,3 +227,29 @@ fn big_math() {
227227 check ( instant. checked_add ( Duration :: from_secs ( 100 ) ) , Instant :: checked_sub) ;
228228 check ( instant. checked_add ( Duration :: from_secs ( i64:: MAX as _ ) ) , Instant :: checked_sub) ;
229229}
230+
231+ #[ test]
232+ #[ cfg( unix) ]
233+ fn system_time_extreme_values_regression ( ) {
234+ // Test for regression in SystemTime comparison with extreme values
235+ // This test covers the bug introduced in PR #144519 where integer overflow
236+ // in the comparison logic caused incorrect results when dealing with times
237+ // near i64::MIN and i64::MAX.
238+ //
239+ // This is the exact test case from GitHub issue #146228
240+ let t = SystemTime :: UNIX_EPOCH ;
241+ let early = t - ( Duration :: from_secs ( i64:: MAX as u64 + 1 ) ) ;
242+ let later = t + ( Duration :: from_secs ( i64:: MAX as u64 ) + Duration :: from_nanos ( 999_999_999 ) ) ;
243+
244+ // This should succeed and not return a SystemTimeError due to incorrect comparison overflow
245+ let delta =
246+ later. duration_since ( early) . expect ( "duration_since should work with extreme values" ) ;
247+
248+ // Verify that the delta calculation is reasonable
249+ // early is at approximately -i64::MAX-1 seconds from epoch
250+ // later is at approximately i64::MAX seconds + 999_999_999 nanoseconds from epoch
251+ // So delta should be approximately (i64::MAX + i64::MAX + 1) seconds + 999_999_999 nanoseconds
252+ let expected_secs = ( i64:: MAX as u64 ) * 2 + 1 ;
253+ let expected = Duration :: new ( expected_secs, 999_999_999 ) ;
254+ assert_eq ! ( delta, expected, "Duration calculation should be correct for extreme values" ) ;
255+ }
0 commit comments