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

[email protected] ("Marc B.") Sat, 4 Jul 2026 11:32:33 +0200
Newsgroups php.internals
Message-ID <[email protected]>
Hi

On 04.07.26 00:16, Tim Düsterhus wrote:
> Hi
>
> On 7/3/26 22:45, Marc B. wrote:
>> An alternative would be to use the same sign for nanoseconds as for
>> seconds (while 0 seconds is neither positive or negative)
>
> Yes, this is effectively mentioned in the “Design Considerations” 
> section of the RFC.

I think you missed my point and truncated my explanation example.

1.5s = 1 second + 500_000_000 nanoseconds
-1.5s = -1 seconds + -500_000_000 nanoseconds
-0.5s = 0 seconds + -500_000_000 nanoseconds

No negative flag needed to be handled separately.


>
>> "... if you are interested in absolute values, you have the magnitude
>> right there and can ignore the `$negative` flag."
>>
>> No I can't
>> * adding two durations does NOT guaranties a value greater than the two
>> * passing a negative duration as timeout might throw
>> * ...
>
> The use cases you listed there are all passing the Duration object 
> somewhere else, which I do not consider to be a case of “if you are 
> interested […]”.
>
> The quoted sentence if referring to situations where your code is the 
> end user of the Duration object and extracts the contents from it for 
> further processing. And in those situations a sign-magnitude 
> representation makes processing easier, because you have a single 
> authoritative source of the sign, not one in each component and 
> because you can extract the magnitude right away, without needing to 
> flip the sign of each component.

If you care about positive values only - use `absolute()`
If you want to know the sign of the duration - the is still the is 
negative property - but it's a getter and not a required to be stored flag.


>
>> This does not answer the meaning difference of `seconds` vs.
>> `nanoseconds` - it's confusing naming that `seconds` is the total amount
>> of seconds in this duration but `nanoseconds` is the number of fractions
>> of a second.
>
> A value-object, such as Duration, is uniquely described by the sum of 
> its properties. And in this case, it's quite literally the sum of the 
> $seconds and $nanoseconds.

You missed your own $negative flag

Am I get you right that you dislike property get hooks on value objects 
in general?

See https://externals.io/message/131376#131755


>
> I'm afraid I don't see how one would get the assumption that $seconds 
> would redundantly be equal to ($nanoseconds / 1_000_000_000) or 
> something like that when the object only has $seconds and $nanoseconds 
> properties without also having $milliseconds, $minutes, or something 
> else. The confusion would also be quickly cleared up just by looking 
> at the object and seeing that the $nanoseconds value is always smaller 
> than 1 billion.
>
> In my opinion renaming the $nanoseconds to $fractionalNanoseconds 
> would greatly decrease the ergonomics of the class.

I more thought about renaming `$seconds` into `$totalSeconds`.

Regards,
Marc