Re: [RFC] Duration class
| From: | Marc B. | Date: | Sat, 04 Jul 2026 09:32:33 +0000 |
| Subject: | Re: [RFC] Duration class | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131757@lists.php.net to get a copy of this message | ||
Hi
On 04.07.26 00:16, Tim Düsterhus wrote:
Hi On 7/3/26 22:45, Marc B. wrote: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.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.
If you care about positive values only - use"... if you are interested in absolute values, you have the magnitude right there and can ignore theThe 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.$negativeflag." No I can't * adding two durations does NOT guaranties a value greater than the two * passing a negative duration as timeout might throw * ...
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.
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#131755This does not answer the meaning difference ofA 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.secondsvs.nanoseconds- it's confusing naming thatsecondsis the total amount of seconds in this duration butnanosecondsis the number of fractions of a second.
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