-
Notifications
You must be signed in to change notification settings - Fork 64
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
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ea55cc1
Provide PSR-20 Clock implementation
ac94c9c
Fix code styles
e3c3f6e
Add timezone parameter to ChronosClock
a5ca848
Fix code styles
4c67457
Fix code styles
0850408
Fix return type in docblock
c0bf2f4
Fix return type in docblock
0b0c0a3
Rename to ClockFactory
8dd61c3
Remove copyright
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,48 @@ | ||
<?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) | ||
* @copyright Copyright (c) Daniel Opitz | ||
* @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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need separate copyright, just the cakephp one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this, because it was also added here:
https://github.com/cakephp/chronos/blob/3.x/src/Chronos.php#L11
https://github.com/cakephp/chronos/blob/3.x/src/DifferenceFormatter.php#L11
Maybe we remove the other redundant copyright tags as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The very original Chronos was forked from a different project so Mark inherited a copyright for some of the files. It's very close to not applying now, but definitely not new code.