From ea55cc181894b57be2b26af052c278662f4dee3b Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 24 Oct 2023 10:05:44 +0200 Subject: [PATCH 1/9] Provide PSR-20 Clock implementation --- composer.json | 6 ++++- src/ChronosClock.php | 34 ++++++++++++++++++++++++++++ tests/TestCase/ChronosClockTest.php | 35 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/ChronosClock.php create mode 100644 tests/TestCase/ChronosClockTest.php diff --git a/composer.json b/composer.json index e235cb5..e511ba2 100644 --- a/composer.json +++ b/composer.json @@ -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/" diff --git a/src/ChronosClock.php b/src/ChronosClock.php new file mode 100644 index 0000000..c404d2d --- /dev/null +++ b/src/ChronosClock.php @@ -0,0 +1,34 @@ +now(); + + $this->assertInstanceOf(DateTimeImmutable::class, $now); + $this->assertInstanceOf(Chronos::class, $now); + $this->assertSame('2001-01-31', $now->toDateString()); + } +} From ac94c9c6cbc6c0df1c89f6decc53f1ff9926eb71 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 24 Oct 2023 10:18:43 +0200 Subject: [PATCH 2/9] Fix code styles --- src/ChronosClock.php | 5 +++-- tests/TestCase/ChronosClockTest.php | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index c404d2d..87ffaaf 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -1,4 +1,5 @@ Date: Tue, 24 Oct 2023 10:28:56 +0200 Subject: [PATCH 3/9] Add timezone parameter to ChronosClock --- src/ChronosClock.php | 15 ++++++++++++++- tests/TestCase/ChronosClockTest.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index 87ffaaf..0bfd627 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -16,6 +16,7 @@ */ use DateTimeImmutable; +use DateTimeZone; use Psr\Clock\ClockInterface; /** @@ -23,6 +24,18 @@ */ class ChronosClock implements ClockInterface { + private DateTimeZone|string|null $timezone; + + /** + * Constructor. + * + * @param DateTimeZone|string|null $timezone + */ + public function __construct(DateTimeZone|string|null $timezone = null) + { + $this->timezone = $timezone; + } + /** * Returns the current time as a Chronos Object * @@ -30,6 +43,6 @@ class ChronosClock implements ClockInterface */ public function now(): DateTimeImmutable { - return Chronos::now(); + return Chronos::now($this->timezone); } } diff --git a/tests/TestCase/ChronosClockTest.php b/tests/TestCase/ChronosClockTest.php index 9b48e17..db16cc8 100644 --- a/tests/TestCase/ChronosClockTest.php +++ b/tests/TestCase/ChronosClockTest.php @@ -1,4 +1,5 @@ assertInstanceOf(Chronos::class, $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 ChronosClock($londonTimezone); + $now = $clock->now(); + + $this->assertInstanceOf(DateTimeImmutable::class, $now); + $this->assertInstanceOf(Chronos::class, $now); + $this->assertSame('2024-01-31', $now->toDateString()); + $this->assertSame('Europe/London', $now->getTimezone()->getName()); + } } From a5ca848b93511a987fb1f10574998a30e701b292 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 24 Oct 2023 10:33:29 +0200 Subject: [PATCH 4/9] Fix code styles --- src/ChronosClock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index 0bfd627..ab88d8e 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -29,7 +29,7 @@ class ChronosClock implements ClockInterface /** * Constructor. * - * @param DateTimeZone|string|null $timezone + * @param DateTimeZone|string|null $timezone The timezone */ public function __construct(DateTimeZone|string|null $timezone = null) { From 4c67457a463d55c61f7e650198298483b4988630 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 24 Oct 2023 12:40:08 +0200 Subject: [PATCH 5/9] Fix code styles --- src/ChronosClock.php | 2 +- tests/TestCase/ChronosClockTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index ab88d8e..be58425 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -29,7 +29,7 @@ class ChronosClock implements ClockInterface /** * Constructor. * - * @param DateTimeZone|string|null $timezone The timezone + * @param \DateTimeZone|string|null $timezone The timezone */ public function __construct(DateTimeZone|string|null $timezone = null) { diff --git a/tests/TestCase/ChronosClockTest.php b/tests/TestCase/ChronosClockTest.php index db16cc8..507a2ea 100644 --- a/tests/TestCase/ChronosClockTest.php +++ b/tests/TestCase/ChronosClockTest.php @@ -1,5 +1,4 @@ Date: Mon, 30 Oct 2023 09:47:58 +0100 Subject: [PATCH 6/9] Fix return type in docblock --- src/ChronosClock.php | 4 ++-- tests/TestCase/ChronosClockTest.php | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index be58425..f78ef04 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -37,9 +37,9 @@ public function __construct(DateTimeZone|string|null $timezone = null) } /** - * Returns the current time as a Chronos Object + * Returns the current time object. * - * @return \Cake\Chronos\Chronos The current time + * @return \DateTimeImmutable The current time */ public function now(): DateTimeImmutable { diff --git a/tests/TestCase/ChronosClockTest.php b/tests/TestCase/ChronosClockTest.php index 507a2ea..77ac87f 100644 --- a/tests/TestCase/ChronosClockTest.php +++ b/tests/TestCase/ChronosClockTest.php @@ -28,8 +28,6 @@ public function testNow(): void $clock = new ChronosClock(); $now = $clock->now(); - $this->assertInstanceOf(DateTimeImmutable::class, $now); - $this->assertInstanceOf(Chronos::class, $now); $this->assertSame('2001-01-31', $now->toDateString()); } @@ -41,8 +39,6 @@ public function testConstructWithTimezone(): void $clock = new ChronosClock($londonTimezone); $now = $clock->now(); - $this->assertInstanceOf(DateTimeImmutable::class, $now); - $this->assertInstanceOf(Chronos::class, $now); $this->assertSame('2024-01-31', $now->toDateString()); $this->assertSame('Europe/London', $now->getTimezone()->getName()); } From c0bf2f4b9d13d28bc136e71c0cdd3585cb0a7414 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 7 Nov 2023 12:18:09 +0100 Subject: [PATCH 7/9] Fix return type in docblock --- src/ChronosClock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ChronosClock.php b/src/ChronosClock.php index f78ef04..2de179e 100644 --- a/src/ChronosClock.php +++ b/src/ChronosClock.php @@ -39,7 +39,7 @@ public function __construct(DateTimeZone|string|null $timezone = null) /** * Returns the current time object. * - * @return \DateTimeImmutable The current time + * @return \Cake\Chronos\Chronos The current time */ public function now(): DateTimeImmutable { From 0b0c0a39812316e5c4f71d2500f88aa3be9352b2 Mon Sep 17 00:00:00 2001 From: odan Date: Thu, 9 Nov 2023 08:04:59 +0100 Subject: [PATCH 8/9] Rename to ClockFactory --- src/{ChronosClock.php => ClockFactory.php} | 2 +- .../{ChronosClockTest.php => ClockFactoryTest.php} | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) rename src/{ChronosClock.php => ClockFactory.php} (96%) rename tests/TestCase/{ChronosClockTest.php => ClockFactoryTest.php} (85%) diff --git a/src/ChronosClock.php b/src/ClockFactory.php similarity index 96% rename from src/ChronosClock.php rename to src/ClockFactory.php index 2de179e..fffae11 100644 --- a/src/ChronosClock.php +++ b/src/ClockFactory.php @@ -22,7 +22,7 @@ /** * PSR-20 Clock implementation. */ -class ChronosClock implements ClockInterface +class ClockFactory implements ClockInterface { private DateTimeZone|string|null $timezone; diff --git a/tests/TestCase/ChronosClockTest.php b/tests/TestCase/ClockFactoryTest.php similarity index 85% rename from tests/TestCase/ChronosClockTest.php rename to tests/TestCase/ClockFactoryTest.php index 77ac87f..02f4f85 100644 --- a/tests/TestCase/ChronosClockTest.php +++ b/tests/TestCase/ClockFactoryTest.php @@ -15,17 +15,16 @@ namespace Cake\Chronos\Test\TestCase; use Cake\Chronos\Chronos; -use Cake\Chronos\ChronosClock; -use DateTimeImmutable; +use Cake\Chronos\ClockFactory; use DateTimeZone; -class ChronosClockTest extends TestCase +class ClockFactoryTest extends TestCase { public function testNow(): void { Chronos::setTestNow('2001-01-31 12:13:14.123456'); - $clock = new ChronosClock(); + $clock = new ClockFactory(); $now = $clock->now(); $this->assertSame('2001-01-31', $now->toDateString()); @@ -36,7 +35,7 @@ public function testConstructWithTimezone(): void Chronos::setTestNow('2024-01-31 12:13:14.123456'); $londonTimezone = new DateTimeZone('Europe/London'); - $clock = new ChronosClock($londonTimezone); + $clock = new ClockFactory($londonTimezone); $now = $clock->now(); $this->assertSame('2024-01-31', $now->toDateString()); From 8dd61c3e3082d936d458830c2ce67c541d555419 Mon Sep 17 00:00:00 2001 From: odan Date: Thu, 9 Nov 2023 13:43:56 +0100 Subject: [PATCH 9/9] Remove copyright --- src/ClockFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ClockFactory.php b/src/ClockFactory.php index fffae11..a5dbc17 100644 --- a/src/ClockFactory.php +++ b/src/ClockFactory.php @@ -10,7 +10,6 @@ * 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 */