From 8904ae27cbc9e942b8c0476fe6a5ee9a9785a78d Mon Sep 17 00:00:00 2001 From: mjansen Date: Sat, 5 Feb 2022 12:16:56 +0100 Subject: [PATCH] Clock: Introduce `PSR-20 Clock Service` --- src/DI/Container.php | 9 ++-- src/Data/Clock/ClockFactory.php | 28 ++++++++++++ src/Data/Clock/ClockFactoryImpl.php | 46 +++++++++++++++++++ src/Data/Clock/ClockInterface.php | 24 ++++++++++ src/Data/Clock/LocalClock.php | 35 +++++++++++++++ src/Data/Clock/SystemClock.php | 27 ++++++++++++ src/Data/Clock/UtcClock.php | 28 ++++++++++++ src/Data/Factory.php | 8 ++++ src/Data/README.md | 68 +++++++++++++++++++++++++++++ src/Data/ROADMAP.md | 1 + tests/Data/LocalClockTest.php | 45 +++++++++++++++++++ tests/Data/SystemClockTest.php | 44 +++++++++++++++++++ tests/Data/UtcClockTest.php | 43 ++++++++++++++++++ 13 files changed, 400 insertions(+), 6 deletions(-) create mode 100644 src/Data/Clock/ClockFactory.php create mode 100644 src/Data/Clock/ClockFactoryImpl.php create mode 100644 src/Data/Clock/ClockInterface.php create mode 100644 src/Data/Clock/LocalClock.php create mode 100644 src/Data/Clock/SystemClock.php create mode 100644 src/Data/Clock/UtcClock.php create mode 100644 src/Data/ROADMAP.md create mode 100644 tests/Data/LocalClockTest.php create mode 100644 tests/Data/SystemClockTest.php create mode 100644 tests/Data/UtcClockTest.php diff --git a/src/DI/Container.php b/src/DI/Container.php index ddf6d6c0e835..ab45f3043c36 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -3,13 +3,10 @@ namespace ILIAS\DI; -use ILIAS\Filesystem\Filesystems; -use ILIAS\FileUpload\FileUpload; -use ILIAS\GlobalScreen\Services; -use ILIAS\Refinery\Factory; -use ILIAS\Skill\Service\SkillService; -use ILIAS\Repository; use ILIAS\BackgroundTasks\BackgroundTaskServices; +use ILIAS\Data\Clock\ClockFactoryImpl; +use ILIAS\Repository; +use ILIAS\Skill\Service\SkillService; /****************************************************************************** * diff --git a/src/Data/Clock/ClockFactory.php b/src/Data/Clock/ClockFactory.php new file mode 100644 index 000000000000..0f773169c0dd --- /dev/null +++ b/src/Data/Clock/ClockFactory.php @@ -0,0 +1,28 @@ +time_zone = $time_zone; + } + + public function now() : DateTimeImmutable + { + return new DateTimeImmutable('now', $this->time_zone); + } +} diff --git a/src/Data/Clock/SystemClock.php b/src/Data/Clock/SystemClock.php new file mode 100644 index 000000000000..0f2d37fe59d8 --- /dev/null +++ b/src/Data/Clock/SystemClock.php @@ -0,0 +1,27 @@ +get() === ['subject1' => 'ASC', 'subject2' => 'DESC']); assert($join === 'sort subject1 ASC, subject2 DESC,'); ?> ``` + +## Clock + +This package provides a fully psr-20 compliant clock handling. + +### Example + +#### System Clock + +The `\ILIAS\Data\Clock\SystemClock` returns a `\DateTimeImmutable` instance always referring to the +current default system timezone. + +```php +clock()->system(); +$now = $clock->now(); +?> +``` + +#### UTC Clock + +The `\ILIAS\Data\Clock\UtcClock` returns a `\DateTimeImmutable` instance always referring to the +`UTC` timezone. + +```php +clock()->utc(); +$now = $clock->now(); +?> +``` + +#### Local Clock + +The `\ILIAS\Data\Clock\UtcClock` returns a `\DateTimeImmutable` instance always referring to the +timezone passed to the factory method. + +```php +clock()->local(new \DateTimeZone('Europe/Berlin')); +$now = $clock->now(); +?> +``` \ No newline at end of file diff --git a/src/Data/ROADMAP.md b/src/Data/ROADMAP.md new file mode 100644 index 000000000000..437766dd8f3d --- /dev/null +++ b/src/Data/ROADMAP.md @@ -0,0 +1 @@ +# Roadmap diff --git a/tests/Data/LocalClockTest.php b/tests/Data/LocalClockTest.php new file mode 100644 index 000000000000..bac69a015c02 --- /dev/null +++ b/tests/Data/LocalClockTest.php @@ -0,0 +1,45 @@ +default_timezone = date_default_timezone_get(); + } + + protected function tearDown() : void + { + date_default_timezone_set($this->default_timezone); + } + + public function testUtcClockIsNotAffectedByGlobalTimezoneChanges() : void + { + date_default_timezone_set('UTC'); + + $clock = new LocalClock(new DateTimeZone('Africa/Windhoek')); + + self::assertSame('Africa/Windhoek', $clock->now()->getTimezone()->getName()); + } +} diff --git a/tests/Data/SystemClockTest.php b/tests/Data/SystemClockTest.php new file mode 100644 index 000000000000..495c7df61931 --- /dev/null +++ b/tests/Data/SystemClockTest.php @@ -0,0 +1,44 @@ +default_timezone = date_default_timezone_get(); + } + + protected function tearDown() : void + { + date_default_timezone_set($this->default_timezone); + } + + public function testUtcClockIsNotAffectedByGlobalTimezoneChanges() : void + { + date_default_timezone_set('Africa/Windhoek'); + + $clock = new SystemClock(); + + self::assertSame('Africa/Windhoek', $clock->now()->getTimezone()->getName()); + } +} diff --git a/tests/Data/UtcClockTest.php b/tests/Data/UtcClockTest.php new file mode 100644 index 000000000000..13f132cb2768 --- /dev/null +++ b/tests/Data/UtcClockTest.php @@ -0,0 +1,43 @@ +default_timezone = date_default_timezone_get(); + } + + protected function tearDown() : void + { + date_default_timezone_set($this->default_timezone); + } + + public function testUtcClockIsNotAffectedByGlobalTimezoneChanges() : void + { + date_default_timezone_set('Europe/Berlin'); + $clock = new UtcClock(); + + self::assertSame('UTC', $clock->now()->getTimezone()->getName()); + } +}