Re: [RFC] Duration class

From: Date: Thu, 25 Jun 2026 16:26:49 +0000
Subject: Re: [RFC] Duration class
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-131536@lists.php.net to get a copy of this message
Hi, On Mon, 22 Jun 2026 at 21:03, Tim Düsterhus <tim@bastelstu.be> 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

« previous php.internals (#131536) next »