Re: [PHP-DEV] [RFC] Duration class

[email protected] (Tim Düsterhus) Tue, 14 Jul 2026 10:36:18 +0200
Newsgroups php.internals
Message-ID <[email protected]>
Hi

On 2026-07-14 05:20, Pierre Joye wrote:
>> As for the specific case of “the constructors should be injective
>> functions”, you missed the second half of the quoted sentence: “and 
>> also
>> based on our experience of working with dates and times both in PHP 
>> and
>> other programming languages”,
> 
> ok, let's agree to disagree here, it is hard to agree on "experiences". 
> :)

That would've been my suggestion as well. I think it's only natural that 
100% agreement on everything is impossible for a project as widely used 
as PHP.

>>  although native support for fixed-point
>> decimals would certainly made the API nicer.
> 
> What would be nicer is common practice and prior art(s). The setSecond
> makes no sense as of now for two reaons:
> - the limits because an instant is measured using 24 hours, 60 minutes
> per hour, 60 seconds per minutes etc. does not mean a duration common
> case do so. Common, and that's barely arguable, a duration is rarely
> measured like an instant in time. If ever,

Sorry, I'm not sure I quite understand what you are trying to say here.

When discussing a Duration as in stop-watch time, I think it's natural 
to say “Usain Bolt broke the 100 m world record with 9 seconds 58 
hundreths”, which is exactly the fixed-point decimal representation that 
the `fromSeconds()` constructor also uses. For longer races one might 
say “1 minute, 40 seconds and 91 hundreths” rather than “100 seconds 91 
hundreths”, but none of the components overflow, and the “subseconds” 
are never further divided down into millis, micros, nanos, but just use 
the desired precision directly.

> - it is awkward to have nanoseconds in setSeconds out of nowhere. I
> understand some external factor (other entities) leading to that
> decision but that creates illogical, inconsistent and confusing
> semantic boundaries. A box can contain apples does not mean
> setDimension should use apples with a limit on top. Or an apple can be
> used with a box that does not make an apple's unit in square feet.

As outlined above, I believe this special casing of the “subsecond 
boundary” is common in the real world and human communication.

Best regards
Tim Düsterhus