Re: [RFC] Time\Instant and Time\Clock
| From: | Tim Düsterhus | Date: | Wed, 04 Nov 2026 16:43:30 +0000 |
| Subject: | Re: [RFC] Time\Instant and Time\Clock | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132999@lists.php.net to get a copy of this message | ||
Hi
On 2026-10-09 08:20, marc@mabe.berlin wrote:
Fair enough regarding the performance argument. I was specifically thinking about use cases where truncating to whole seconds is required. I've added a “CoarseSystemClock” to the possible future scope list. Having it as a separate class makes sense to me to make using the coarse system clock a very explicit and deliberate choice. The correct default for the majority of use cases is the SystemClock, the coarse clock potentially doesn't even have millisecond resolution depending on your configured tick rate, isn't necessarily aligned to second boundaries and …As with the testing clocks, we consider aFine by me to keep it out of scope for this RFC. For a future RFC, though, I'd prefer a built-in CoarseSystemClock over a generic decorator. A decorator always has to read the precise clock first and only then truncates, so it can never be cheaper than the clock it wraps. A built-in system clock configured for second (or millisecond) resolution could read a coarse clock source directly instead, such as CLOCK_REALTIME_COARSE on Linux.CoarseClockout of scope for this RFC. With a possible futuretruncateTo()that I already mentioned elsewhere in the discussion, a userland implementation would become trivial for the use cases where a coarser clock is required.
On Linux arm64 I measured:… 10 nanoseconds is likely not all that much compared to the overhead that PHP adds (e.g. object allocation).clock_gettime(CLOCK_REALTIME) 13.3 ns clock_gettime(CLOCK_REALTIME_COARSE) 3.3 ns
I'll put this on my list with Derick.We believe that with the renaming of the “Unix timestamp” converter to use theI'm not convinced yet […] getNanoOfSecond() removes the ambiguity entirely, at the cost of a few characters. If that's too far from the existing naming, getNanosecond() would at least match DateTime::getMicrosecond().toprefix and the lack of “Unix” in the name ofgetNanoseconds()the naming is clear enough. It is also easy to see that the result ofgetNanoseconds()can't be the number of nanoseconds elapsed since the Unix epoch, because the value is guaranteed to be less than 1_000_000_000 (i.e. less than 1 second). Usingnanosecondsas the fractional part is consistent withDuration::$nanoseconds.
See my reply in https://news-web.php.net/php.internals/132953. Best regards Tim DüsterhusFor completeness I'd like to note that the epoch of the Instant is intentionally left unspecified and that the Unix epoch is just one of several possible epochs.Agreed, and that's exactly the point I made about "follows POSIX time": if the epoch and the representation are unspecified, the class definition shouldn't state that Instants follow POSIX time. That belongs to the Unix timestamp conversion methods.