Provides current time for runtime and controllable time for testing
PSR-20 compatible
Install with Composer
composer require orisai/clockCreate Clock instance and setup for shortcut function usage
use Orisai\Clock\SystemClock;
use Orisai\Clock\ClockHolder;
$clock = new SystemClock();
ClockHolder::setClock($clock);Request Clock interface and get current time (or Psr\Clock\ClockInterface for psr/clock compatibility)
use Orisai\Clock\Clock;
class ExampleService
{
private Clock $clock;
public function __construct(Clock $clock)
{
$this->clock = $clock;
}
public function doSomething(): void
{
$currentTime = $this->clock->now();
}
}To prevent waiting periods in tests, replace calls to sleep() and usleep() functions with Clock->sleep() method.
$clock->sleep(
1, // Seconds
2, // Milliseconds
3, // Microseconds
);Or with named arguments, just:
$clock->sleep(microseconds: 10);Get current time statically
use function Orisai\Clock\now;
$currentTime = now(); // \DateTimeImmutableIt is also possible to access clock statically
use Orisai\Clock\ClockHolder;
$clock = ClockHolder::getClock();For standard usage in application runtime. Returns same time as new DateTimeImmutable('now') would.
use Orisai\Clock\SystemClock;
$clock = new SystemClock();Specify timezone (otherwise current timezone is used)
use DateTimeZone;
$clock = new SystemClock(new DateTimeZone('UTC'));For testing exact times. Does not change unless explicitly requested.
use Orisai\Clock\FrozenClock;
$timestamp = 0.0;
$clock = new FrozenClock($timestamp);
$clock->now()->format('U.u'); // 0.000_000Specify timezone (otherwise current timezone is used)
use DateTimeZone;
$clock = new FrozenClock(0, new DateTimeZone('UTC'));You can also create clock from datetime
use DateTime;
use DateTimeImmutable;
$clock = new FrozenClock(new DateTimeImmutable());
// or
$clock = new FrozenClock(new DateTime());Sleeping does not put thread to sleep but just moves timestamp of clock's internal DateTimeImmutable.
For accurate measurement of time. Unlike standard system clock is not affected by clock drifting (which is wanted for normal usage, but makes time measuring unreliable).
use Orisai\Clock\MeasurementClock;
$clock = new MeasurementClock();Specify timezone (otherwise current timezone is used)
use DateTimeZone;
$clock = new MeasurementClock(new DateTimeZone('UTC'));Use ClockAdapterFactory to automatically create the best adapter for Orisai\Clock\Clock compatibility from
your Psr\Clock\ClockInterface.
- It creates no adapter for
Orisai\Clock\Clockinstance and just returns it - It prefers Symfony adapter over PSR adapter for
sleep()method compatibility
use Orisai\Clock\Adapter\ClockAdapterFactory;
use Symfony\Component\Clock\NativeClock;
$clock = ClockAdapterFactory::create($somePsrCompatibleClock);Decorate any Psr\Clock\ClockInterface implementation to conform interface Orisai\Clock\Clock.
use Orisai\Clock\Adapter\PsrToOrisaiClockAdapter;
$clock = new PsrToOrisaiClockAdapter(new ExamplePsrClock());Decorate any Symfony\Component\Clock\ClockInterface implementation to conform interface Orisai\Clock\Clock.
use Orisai\Clock\Adapter\SymfonyToOrisaiClockAdapter;
use Symfony\Component\Clock\NativeClock;
$clock = new SymfonyToOrisaiClockAdapter(new NativeClock());Decorate any Orisai\Clock\Clock implementation to conform interface Symfony\Component\Clock\ClockInterface.
use Orisai\Clock\Adapter\OrisaiToSymfonyClockAdapter;
use Orisai\Clock\SystemClock;
$clock = new OrisaiToSymfonyClockAdapter(new SystemClock());- Nette integration - orisai/nette-clock