Collection of PSR-20 clocks

1.1.0 2025-02-08 18:40 UTC

This package is auto-updated.

Last update: 2025-02-08 17:42:01 UTC


README

Total Downloads Latest Stable Version build status coverage report License

Collection of PSR-20 clocks

Installation

The best way to install Clock is via Composer. Just add konecnyjakub/clock to your dependencies.

Usage

System clock

This clock returns current time. It uses default timezone in PHP.

<?php
declare(strict_types=1);

use DateTimeZone;
use Konecnyjakub\Clock\SystemClock;

(new SystemClock())->now();

Local clock

This clock returns current time in the specified time zone.

<?php
declare(strict_types=1);

use DateTimeZone;
use Konecnyjakub\Clock\LocalClock;

(new LocalClock(new DateTimeZone("Europe/Prague")))->now();

Frozen clock

This clock always returns set time which makes it useful for tests.

<?php
declare(strict_types=1);

use DateTimeImmutable;
use Konecnyjakub\Clock\FrozenClock;

(new FrozenClock(new DateTimeImmutable("1970-01-01")))->now();

If you need different values on subsequent calls, you can use FrozenClocksCollection. It returns the values in order which they were passed in or throws an exception if all values were already returned (each value is returned only 1 time). Example:

<?php
declare(strict_types=1);

use DateTimeImmutable;
use Konecnyjakub\Clock\FrozenClocksCollection;

$dt1 = new DateTimeImmutable("2025-01-01");
$dt2 = new DateTimeImmutable("2025-02-01");
$clock = new FrozenClocksCollection($dt1, $dt2);
$clock->now(); // returns $dt1
$clock->now(); // returns $dt2
$clock->now(); // throws an exception

UTC clock

This clock return current time in UTC.

<?php
declare(strict_types=1);

use DateTimeImmutable;
use Konecnyjakub\Clock\UTCClock;

(new UTCClock())->now();