Hi,

On Mon, 22 Jun 2026 at 21:03, Tim Düsterhus <[email protected]> wrote:
> I don't think I've seen your name on the list before. If this indeed
> your first email here: Welcome and thank you for contributing!

Thank you for the warm welcome!

I have a suggestion for the RFC: treating negative durations as normal
rather than unusual; specifically, allowing negative integers in the
constructors.

While it's true that waiting for a negative amount of time is
meaningless, the RFC notes that the Duration class will eventually do
double duty: it will be used both as a timeout and as a "time delta"
for time arithmetic. In the latter context, negative durations are
mathematically sound and practically useful. For example, here's a use
case from one of my past projects, translated to PHP from memory:

/**
 * Truncates `$events` so that they fit within the daylight window.
 * The allowed time window (dawn to dusk) is expanded on both ends
 * by `$daytimePadding`, which can be negative to shrink the window.
 * Events that would fall entirely outside this window are discarded.
 *
 * @param list<Event> $events
 * @return list<Event>
 */
function clampEventsToDaylight(
    DayAstronomicalData $day,
    Duration $daytimePadding,
    array $events,
): array

Instead of preventing the construction of some invalid timeouts at the
cost of making time arithmetic more clunky, it seems better to keep
the constructors flexible and let functions that take Duration as an
argument enforce their own rules when appropriate. That's the way
other languages tend to handle the issue. For example,
Duration.ofHours(-2) is legal in Java and Temporal.Duration.from({
hours: -2 }) is legal in JavaScript. I'm less familiar with Rust, but
from what I've read, its standard Duration type forbids negative
values specifically because Rust doesn't come with a full date/time
API and Duration is used for timeouts and system timers. The most
popular Rust library for complex date/time handling, chrono, provides
a TimeDelta type (renamed from Duration) that accepts negative values
in its constructors.

Best regards,
Paul

Reply via email to