-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide PSR-20 Clock implementation (#443)
Provide PSR-20 Clock implementation Co-authored-by: odan <Frisco02>
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |