Skip to content

Commit

Permalink
Provide PSR-20 Clock implementation (#443)
Browse files Browse the repository at this point in the history
Provide PSR-20 Clock implementation

Co-authored-by: odan <Frisco02>
  • Loading branch information
odan authored Nov 9, 2023
1 parent 4ae8324 commit da6b49c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
"source": "https://github.com/cakephp/chronos"
},
"require": {
"php": ">=8.1"
"php": ">=8.1",
"psr/clock": "^1.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^5.0",
"phpunit/phpunit": "^10.1.0"
},
"provide": {
"psr/clock-implementation": "1.0"
},
"autoload": {
"psr-4": {
"Cake\\Chronos\\": "src/"
Expand Down
47 changes: 47 additions & 0 deletions src/ClockFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace Cake\Chronos;

/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

use DateTimeImmutable;
use DateTimeZone;
use Psr\Clock\ClockInterface;

/**
* PSR-20 Clock implementation.
*/
class ClockFactory implements ClockInterface
{
private DateTimeZone|string|null $timezone;

/**
* Constructor.
*
* @param \DateTimeZone|string|null $timezone The timezone
*/
public function __construct(DateTimeZone|string|null $timezone = null)
{
$this->timezone = $timezone;
}

/**
* Returns the current time object.
*
* @return \Cake\Chronos\Chronos The current time
*/
public function now(): DateTimeImmutable
{
return Chronos::now($this->timezone);
}
}
44 changes: 44 additions & 0 deletions tests/TestCase/ClockFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Chronos\Test\TestCase;

use Cake\Chronos\Chronos;
use Cake\Chronos\ClockFactory;
use DateTimeZone;

class ClockFactoryTest extends TestCase
{
public function testNow(): void
{
Chronos::setTestNow('2001-01-31 12:13:14.123456');

$clock = new ClockFactory();
$now = $clock->now();

$this->assertSame('2001-01-31', $now->toDateString());
}

public function testConstructWithTimezone(): void
{
Chronos::setTestNow('2024-01-31 12:13:14.123456');

$londonTimezone = new DateTimeZone('Europe/London');
$clock = new ClockFactory($londonTimezone);
$now = $clock->now();

$this->assertSame('2024-01-31', $now->toDateString());
$this->assertSame('Europe/London', $now->getTimezone()->getName());
}
}

0 comments on commit da6b49c

Please sign in to comment.