Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide PSR-20 Clock implementation #443

Merged
merged 9 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}