time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.06k stars 261 forks source link

Expose `Time::MIN` and `Time::MAX` #686

Open jun-sheaf opened 1 month ago

jun-sheaf commented 1 month ago

This PR exposes the internal max/min values for Time. These are useful for building ranges.

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.7%. Comparing base (4a74924) to head (956d90d). Report is 41 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #686 +/- ## ======================================= - Coverage 97.8% 97.7% -0.1% ======================================= Files 81 83 +2 Lines 9378 9000 -378 ======================================= - Hits 9169 8793 -376 + Misses 209 207 -2 ```

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

jhpratt commented 1 month ago

Can you elaborate on your use case? Even within this repository the values are used in very few locations, all of which could easily be replaced without issue.

jun-sheaf commented 1 month ago

Can you elaborate on your use case? Even within this repository the values are used in very few locations, all of which could easily be replaced without issue.

We have a scheduling app that accepts missing start and end times. To make computations easier, we replace these bounds with actual values, specifically Time::MIN and Time::MAX.

jhpratt commented 1 month ago

In that case, why wouldn't you want to be explicit about the exact value being added?

jun-sheaf commented 1 month ago

In that case, why wouldn't you want to be explicit about the exact value being added?

We are being explicit. By not specifying the range ends, you opt in to MIN as the minimum and MAX as the maximum. We could enforce using MIN instead of a missing lower bound, but for the upper bound, the problem is slightly more difficult as specifying an upper bound is dependent on the precision.

That said, we were able to workaround MIN/MAX by subtracting 1.nanosecond() from MIDNIGHT. If you're not convinced of this PR, maybe we can add some documentation about max bounds and how to achieve them (perhaps using the workaround just mentioned).

jun-sheaf commented 3 weeks ago

@jhpratt Gentle ping, could you please take a look? If you feel this PR isn't useful, please close.

jhpratt commented 3 weeks ago

I've decided that this would cause minimal confusion given that Ord is already implemented. However, MIDNIGHT already exists with identical semantics as MIN, so I'd like to avoid exposing that (I'll probably look into removing it). I would accept an update that makes MAX and only MAX public. You can put #[doc(alias = "MIN")] on MIDNIGHT if you'd like as well.