Skip to content

bag2php/clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bag2\Clock

A PSR-20 Clock implementation.

Interface

This is the only method provided by the PSR-20 Clock interface.

<?php

namespace Psr\Clock;

interface ClockInterface
{
    /**
     * Returns the current time as a DateTimeImmutable Object
     */
    public function now(): \DateTimeImmutable;

}

Interoperability with DateTime Libraries

Today, Carbon and Chronos are known as useful libraries that extend the DateTime class.

Both provide classes that extend from DateTimeImmutable, so it's easy to get an adapter.

If the PSR defines the Clock interface, it is convenient for these adapters to be shipped with their respective libraries for the user.

DateTime for Testing

Writing tests for time-dependent functions is hard. Clock Interface is one of the solutions for that.

However, this requires the application to remove the new DateTime() (no arguments) and the time functions (date(), time(), strtotime()).

Other options are php-timecop, Chronos::setTestNow(), Carbon::setTestNow() and rashidlaasri/travel. However, they are not object-oriented as they depend on global state (or static properties).

Testing helper classes

About

PoC implementation PSR-20 Clock interface

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages