chronotope / chrono

Date and time library for Rust
Other
3.3k stars 523 forks source link

Add checked_mul and checked_div #1565

Closed Zomtir closed 5 months ago

Zomtir commented 5 months ago

Adds checked variants of TimeDelta::mul and TimeDelta::div as proposed in #1469.

If you spot other unhandled cases let me know.

Regarding checked_div, I think it is impossible to overflow because you cannot divide by integers between -1 and +1 other than 0.

cc @pitdicker

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 96.77419% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 91.81%. Comparing base (f8cecbe) to head (80c1bb0).

Files Patch % Lines
src/time_delta.rs 96.77% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1565 +/- ## ========================================== + Coverage 91.80% 91.81% +0.01% ========================================== Files 37 37 Lines 18151 18164 +13 ========================================== + Hits 16664 16678 +14 + Misses 1487 1486 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Zomtir commented 5 months ago

Half-open range patterns became stable in Rust 1.59.0, the CI is on an older version so i had to use full ranges

    // half-open
    ..=-1 => (secs - 1, nanos + NANOS_PER_SEC),
    NANOS_PER_SEC.. => (secs + 1, nanos - NANOS_PER_SEC),

    // full range
    i32::MIN..=-1 => (secs - 1, nanos + NANOS_PER_SEC),
    NANOS_PER_SEC..=i32::MAX => (secs + 1, nanos - NANOS_PER_SEC),
pitdicker commented 5 months ago

Half-open range patterns became stable in Rust 1.59.0, the CI is on an older version so i had to use full ranges

The CI is checking our MSRV of 1.61.

Zomtir commented 5 months ago

Half-open range patterns became stable in Rust 1.59.0, the CI is on an older version so i had to use full ranges

The CI is checking our MSRV of 1.61.

Woops! Half-open ranges work on my system (TM) : Rust 1.74. So it got stable somewhere in between.

pitdicker commented 5 months ago

Rust 1.66 stabilized the feature: https://blog.rust-lang.org/2022/12/15/Rust-1.66.0.html#other-changes