symfony/clock

Decouples applications from the system clock

Installs: 47 634 592

Dependents: 71

Suggesters: 2

Security: 0

Stars: 313

Watchers: 5

Forks: 4

v7.2.0-RC1 2024-09-25 14:21 UTC

README

Symfony Clock decouples applications from the system clock.

Getting Started

composer require symfony/clock
use Symfony\Component\Clock\NativeClock;
use Symfony\Component\Clock\ClockInterface;

class MyClockSensitiveClass
{
    public function __construct(
        private ClockInterface $clock,
    ) {
        // Only if you need to force a timezone:
        //$this->clock = $clock->withTimeZone('UTC');
    }

    public function doSomething()
    {
        $now = $this->clock->now();
        // [...] do something with $now, which is a \DateTimeImmutable object

        $this->clock->sleep(2.5); // Pause execution for 2.5 seconds
    }
}

$clock = new NativeClock();
$service = new MyClockSensitiveClass($clock);
$service->doSomething();

Resources