Repository navigation
Implement PSR-20 ClockInterface on all clocks - #132
joelbutcher wants to merge 1 commit into
Conversation
Clock now extends Psr\Clock\ClockInterface, so every built-in clock implements now(), returning a DateTimeImmutable (microsecond precision). SystemClock and FixedClock accept an optional TimeZone for now(), defaulting to UTC so behaviour stays independent of the PHP default timezone. OffsetClock and ScaleClock use their reference clock's zone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011ogebQW2VgoLNsRLse8AFX
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #132 +/- ##
=========================================
Coverage 99.30% 99.30%
- Complexity 1109 1114 +5
=========================================
Files 48 48
Lines 2445 2458 +13
=========================================
+ Hits 2428 2441 +13
Misses 17 17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi, I think that if we're going down this route, we should add a Now, to stay consistent, methods like One possible way to solve this could be to make the Thoughts? |
|
Maybe we should deprecate |
|
@solodkiy Thank you for your feedback. I personally think that adding everything to the But I'm not sure if it helps at all; which reads best? $this->clock->localDate();
$this->currentTimeService->localDate();
LocalDate::now($this->clock);I personally think that the last one is fine. What's important I think is to keep the ability to explicitly provide a time zone when calling LocalDate::now($this->clock->withTimeZone('Europe/Paris'));I'd also appreciate feedback from @zjbarg, @nkamuo, @brunocassol here, as you've all been involved in |
|
@BenMorel a couple of thoughts, if you don't mind:
One suggestion could be to provide PSR adapters for the existing final class PsrClock implements \Psr\Clock\ClockInterface
{
public function __construct(
private readonly Clock $clock,
private readonly TimeZone $timeZone,
) {}
public function now(): DateTimeImmutable
{
return $this->clock->getTime()->atTimeZone($this->timeZone)->toNativeDateTimeImmutable();
}
}
final class FromPsrClock implements Clock
{
public function __construct(private readonly \Psr\Clock\ClockInterface $clock) {}
public function getTime(): Instant
{
return ZonedDateTime::fromNativeDateTime($this->clock->now())->getInstant();
}
} |
|
@joelbutcher Thank you for your feedback.
I also came across Noda Time, which has 2 clocks: To get the current date, you either start from a LocalDate today = clock.GetCurrentInstant().InZone(zone).Date;Or get it from a LocalDate today = zonedClock.GetCurrentDate();Just like Java, you can convert a Clock to a ZonedClock by calling ZonedClock zonedClock = clock.InZone(zone);What I like about this idea of splitting the Clock in two is that it accommodates both types of applications: those that only ever deal with a single timezone (inject a They do have the "bloated" ZonedClock API that I complained about earlier, but it fits your intuition, @solodkiy; I may relax my point of view here.
Agreed, and I am a bit afraid of making the timezone any less mandatory. If we follow the above, though, and make
I have on my agenda to remove
Breaking changes are absolutely not a problem before 1.0, now is the time to break things! |
|
@BenMorel Could we treat the direction of Clock and ZonedClock different from adding PSR-20 support then? I'd be in favour of splitting the two and would be happy to contribute this myself, but cautious that we don't want to bloat this PR beyond the PSR capability today. |
|
Closing in favour of #133, which follows the direction we landed on here:
The approach changed enough that a fresh PR is easier to review than rewriting this one. Breaking changes are called out there; per Ben, now is the time to break things! |
Summary
Clocknow extends PSR-20Psr\Clock\ClockInterface, so every built-in clock can be used anywhere a PSR clock is expected.now(): DateTimeImmutable. Native dates only hold microseconds, so nanoseconds are rounded down, just asZonedDateTime::toNativeDateTimeImmutable()does.SystemClockandFixedClockaccept an optionalTimeZonefor the dates returned bynow(), defaulting to UTC.OffsetClockandScaleClockhave no new parameter:now()uses their reference clock's time zone, so a wrapper can't disagree with the clock it wraps.Why UTC and not the PHP default time zone?
The time zone only changes the wall-clock fields of the returned date. The moment is the same either way, so timestamps, comparisons and differences don't depend on it. This library never reads
date_default_timezone_get(); everynow()that returns local fields asks for an explicitTimeZone. Defaulting to UTC keeps the new method in line with that. Apps that want local wall-clock output can pass a zone.Breaking changes
Clockimplementations must now implementnow().psr/clock^1.0.Trade-off
To find its zone, a decorator clock calls its reference clock's
now(). ForSystemClockthat reads the system time a second time, but only the zone from that call is used. An alternative would be agetTimeZone()on the clocks. Happy to switch if you prefer that.Tests
now()tests for each clock, with expected values worked out by hand: the UTC default, an explicit zone on the leaf clocks, and the zone inherited by the decorators.🤖 Generated with Claude Code
https://claude.ai/code/session_011ogebQW2VgoLNsRLse8AFX