@@ -226,7 +226,7 @@ impl Instant {
226
226
/// Returns the `epochMilliseconds` value for this `Instant`.
227
227
#[ must_use]
228
228
pub fn epoch_milliseconds ( & self ) -> i64 {
229
- ( self . as_i128 ( ) / 1_000_000 ) as i64
229
+ self . as_i128 ( ) . div_euclid ( 1_000_000 ) as i64
230
230
}
231
231
232
232
/// Returns the `epochNanoseconds` value for this `Instant`.
@@ -360,6 +360,28 @@ mod tests {
360
360
assert ! ( Instant :: try_new( min_minus_one) . is_err( ) ) ;
361
361
}
362
362
363
+ #[ test]
364
+ fn max_min_epoch_millseconds ( ) {
365
+ // Assert the casting is valid.
366
+ let max = NS_MAX_INSTANT ;
367
+ let min = NS_MIN_INSTANT ;
368
+ let max_instant = Instant :: try_new ( max) . unwrap ( ) ;
369
+ let min_instant = Instant :: try_new ( min) . unwrap ( ) ;
370
+
371
+ // Assert max and min are valid for casting.
372
+ assert_eq ! (
373
+ max_instant. epoch_milliseconds( ) ,
374
+ max. div_euclid( 1_000_000 ) as i64
375
+ ) ;
376
+ assert_eq ! (
377
+ min_instant. epoch_milliseconds( ) ,
378
+ min. div_euclid( 1_000_000 ) as i64
379
+ ) ;
380
+ // Assert the max and min are not being truncated.
381
+ assert_ne ! ( max_instant. epoch_milliseconds( ) , i64 :: MAX ) ;
382
+ assert_ne ! ( max_instant. epoch_milliseconds( ) , i64 :: MIN ) ;
383
+ }
384
+
363
385
#[ test]
364
386
fn instant_parsing_limits ( ) {
365
387
// valid cases
0 commit comments