brick / date-time

Date and time library for PHP
MIT License
321 stars 29 forks source link

Add Seconds value object #65

Closed antonkomarev closed 1 year ago

antonkomarev commented 1 year ago

It may be useful when we want to pass Value Object of TTL seconds. Duration is not the best choice in this situation because TTL cannot contain nanoseconds.

someniatko commented 1 year ago

It opens up a rabbit hole of also having Minutes, Hours, Days etc for various other business-logic driven reasons. Not saying that's an unnecessary addition though, but adding only Seconds in that case looks odd.

BenMorel commented 1 year ago

I agree with @someniatko that this opens up a rabbit hole. What's the point of a value object here? For other values like Year it may make sense, as you may need Year::isLeap(); but for Seconds?

I think just using an int is fine here. If what you need is defense against invalid (negative) values, maybe static analysis can help you, something like non-negative-int or int<0, max> in Psalm.

I'm closing this issue for now, please feel free to comment if you want to provide more arguments, I'll still be reading them.

BenMorel commented 1 year ago

Actually now that I think about it, what you're looking for is a Duration that forbids nanoseconds.

Can't you just accept a Duration, and throw an exception if the nanos are not zero?


Also, this aligns with an idea I had earlier, to provide some kind of variants for times and date/times with a given precision: minute, second, millisecond, microsecond, nanosecond.

If we had generics in PHP, I would have experimented with something like LocalTime<Minute> or Duration<Second>. But in the absence of generics, I'm quite reluctant to create like 5 variants of each class.

Ideas welcome on the subject.