A few times now I’ve [bumped] into [situations] where a method takes a
std::time::duration::Duration as a parameter but has ambiguous or strangely defined behavior in the event that the underlying signed value is negative. The accepted solution seems to be to silently convert negative values to zero, which seems to violate the principal of least surprise.
The fact that a duration can be negative is a bit odd to me; in my mind, a duration is always a positive value. The difference between today and two days ago is always “two days”, just as the difference between
-2. However, Java 8 (and Joda Time) offer [the ability to negate a Duration], so clearly this is an established practice.
I have two questions:
- What’s the use case for a negative Duration?
- It seems as though there are a number of APIs that would benefit from the addition of a unambiguously positive variant of Duration (e.g.
UnsignedDuration) to the
timelibrary. Is there a reason not to add one? I’d be happy to work on this.
Thanks! : https://github.com/rust-lang/rust/pull/16626 : https://github.com/carllerche/mio/pull/48#issuecomment-62726710 : https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#negated--