@@ -227,13 +227,18 @@ impl Duration {
227227 /// assert_eq!(Duration::new(1, 2_000_000_000), 3.seconds());
228228 /// ```
229229 pub const fn new ( mut seconds : i64 , mut nanoseconds : i32 ) -> Self {
230- seconds += nanoseconds as i64 / 1_000_000_000 ;
230+ seconds = expect_opt ! (
231+ seconds. checked_add( nanoseconds as i64 / 1_000_000_000 ) ,
232+ "overflow constructing `time::Duration`"
233+ ) ;
231234 nanoseconds %= 1_000_000_000 ;
232235
233236 if seconds > 0 && nanoseconds < 0 {
237+ // `seconds` cannot overflow here because it is positive.
234238 seconds -= 1 ;
235239 nanoseconds += 1_000_000_000 ;
236240 } else if seconds < 0 && nanoseconds > 0 {
241+ // `seconds` cannot overflow here because it is negative.
237242 seconds += 1 ;
238243 nanoseconds -= 1_000_000_000 ;
239244 }
@@ -249,7 +254,10 @@ impl Duration {
249254 /// assert_eq!(Duration::weeks(1), 604_800.seconds());
250255 /// ```
251256 pub const fn weeks ( weeks : i64 ) -> Self {
252- Self :: seconds ( weeks * 604_800 )
257+ Self :: seconds ( expect_opt ! (
258+ weeks. checked_mul( 604_800 ) ,
259+ "overflow constructing `time::Duration`"
260+ ) )
253261 }
254262
255263 /// Create a new `Duration` with the given number of days. Equivalent to
@@ -260,7 +268,10 @@ impl Duration {
260268 /// assert_eq!(Duration::days(1), 86_400.seconds());
261269 /// ```
262270 pub const fn days ( days : i64 ) -> Self {
263- Self :: seconds ( days * 86_400 )
271+ Self :: seconds ( expect_opt ! (
272+ days. checked_mul( 86_400 ) ,
273+ "overflow constructing `time::Duration`"
274+ ) )
264275 }
265276
266277 /// Create a new `Duration` with the given number of hours. Equivalent to
@@ -271,7 +282,10 @@ impl Duration {
271282 /// assert_eq!(Duration::hours(1), 3_600.seconds());
272283 /// ```
273284 pub const fn hours ( hours : i64 ) -> Self {
274- Self :: seconds ( hours * 3_600 )
285+ Self :: seconds ( expect_opt ! (
286+ hours. checked_mul( 3_600 ) ,
287+ "overflow constructing `time::Duration`"
288+ ) )
275289 }
276290
277291 /// Create a new `Duration` with the given number of minutes. Equivalent to
@@ -282,7 +296,10 @@ impl Duration {
282296 /// assert_eq!(Duration::minutes(1), 60.seconds());
283297 /// ```
284298 pub const fn minutes ( minutes : i64 ) -> Self {
285- Self :: seconds ( minutes * 60 )
299+ Self :: seconds ( expect_opt ! (
300+ minutes. checked_mul( 60 ) ,
301+ "overflow constructing `time::Duration`"
302+ ) )
286303 }
287304
288305 /// Create a new `Duration` with the given number of seconds.
@@ -303,6 +320,9 @@ impl Duration {
303320 /// assert_eq!(Duration::seconds_f64(-0.5), -0.5.seconds());
304321 /// ```
305322 pub fn seconds_f64 ( seconds : f64 ) -> Self {
323+ if seconds > i64:: MAX as f64 || seconds < i64:: MIN as f64 {
324+ crate :: expect_failed ( "overflow constructing `time::Duration`" ) ;
325+ }
306326 Self :: new_unchecked ( seconds as _ , ( ( seconds % 1. ) * 1_000_000_000. ) as _ )
307327 }
308328
@@ -314,6 +334,9 @@ impl Duration {
314334 /// assert_eq!(Duration::seconds_f32(-0.5), (-0.5).seconds());
315335 /// ```
316336 pub fn seconds_f32 ( seconds : f32 ) -> Self {
337+ if seconds > i64:: MAX as f32 || seconds < i64:: MIN as f32 {
338+ crate :: expect_failed ( "overflow constructing `time::Duration`" ) ;
339+ }
317340 Self :: new_unchecked ( seconds as _ , ( ( seconds % 1. ) * 1_000_000_000. ) as _ )
318341 }
319342
@@ -364,10 +387,14 @@ impl Duration {
364387 /// As the input range cannot be fully mapped to the output, this should only be used where it's
365388 /// known to result in a valid value.
366389 pub ( crate ) const fn nanoseconds_i128 ( nanoseconds : i128 ) -> Self {
367- Self :: new_unchecked (
368- ( nanoseconds / 1_000_000_000 ) as _ ,
369- ( nanoseconds % 1_000_000_000 ) as _ ,
370- )
390+ let seconds = nanoseconds / 1_000_000_000 ;
391+ let nanoseconds = nanoseconds % 1_000_000_000 ;
392+
393+ if seconds > i64:: MAX as i128 || seconds < i64:: MIN as i128 {
394+ crate :: expect_failed ( "overflow constructing `time::Duration`" ) ;
395+ }
396+
397+ Self :: new_unchecked ( seconds as _ , nanoseconds as _ )
371398 }
372399 // endregion constructors
373400
0 commit comments