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()); + } +}