Matthias Krüger d2644d9fe9
Rollup merge of #114238 - jhpratt:fix-duration-div, r=thomcc
Fix implementation of `Duration::checked_div`

I ran across this while running some sanity checks on `time`. Quickcheck immediately found a bug, and as I'd modified the code from `std` I knew there was a bug here as well.

tl;dr this code fails ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1189a3efcdfc192c27d6d87815359353))

```rust
use std::time::Duration;

fn main() {
    assert_eq!(
        Duration::new(1, 1).checked_div(7),
        Some(Duration::new(0, 142_857_143)),
    );
}
```

The existing code determines that 1/7 = 0 (seconds), 1/7 = 0 (nanoseconds), 1 billion / 7 = 142,857,142 (extra nanoseconds). The billion comes from multiplying the remainder of the seconds (1) by the number of nanoseconds in a second. However, **this wrongly ignores any remaining nanoseconds**. This PR takes that into consideration, adds a test, and also changes the roundabout way of calculating the remainder into directly computing it.

Note: This is _not_ a rounding error. This result divides evenly.

`@rustbot` label +A-time +C-bug +S-waiting-on-reviewer +T-libs
2023-08-28 08:13:57 +02:00
..
2023-04-16 11:38:52 +00:00
2023-08-11 07:26:51 +08:00
2023-06-14 15:21:15 -04:00
2022-10-08 11:48:53 +00:00
2022-07-13 17:07:41 -07:00
2023-08-13 13:07:53 -06:00
2023-04-26 02:10:22 -04:00
2023-04-16 11:38:52 +00:00
2023-04-16 11:38:52 +00:00
2023-04-16 11:38:52 +00:00
2023-04-16 11:38:52 +00:00
2023-05-30 08:00:10 -04:00
2023-04-16 11:38:52 +00:00
2023-04-16 11:38:52 +00:00
2023-08-13 13:07:53 -06:00
2023-07-03 14:05:55 +02:00
2022-02-25 08:00:52 -05:00
2023-07-12 21:38:55 -04:00
2023-05-30 08:00:10 -04:00
2023-04-16 11:38:52 +00:00
2022-09-27 19:23:52 +00:00
2022-08-20 12:49:20 -04:00
2022-12-30 14:00:42 +01:00
2023-01-02 10:33:23 -08:00
2022-07-08 21:18:15 +00:00
2022-06-27 13:30:44 -04:00