Skip to content

Commit

Permalink
Merge pull request #4004 from mjansenDatabay/feature/8/clock-service
Browse files Browse the repository at this point in the history
Clock: Introduce `PSR-20 Clock Service` as `src/Data` type
  • Loading branch information
mjansenDatabay authored Mar 8, 2022
2 parents 83bd95a + 8904ae2 commit d0b2575
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/******************************************************************************
*
Expand Down
28 changes: 28 additions & 0 deletions src/Data/Clock/ClockFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeZone;

interface ClockFactory
{
public function system() : ClockInterface;

public function utc() : ClockInterface;

public function local(DateTimeZone $time_zone) : ClockInterface;
}
46 changes: 46 additions & 0 deletions src/Data/Clock/ClockFactoryImpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeZone;

class ClockFactoryImpl implements ClockFactory
{
/**
* @inheritDoc
*/
public function system() : ClockInterface
{
return new SystemClock();
}

/**
* @inheritDoc
*/
public function utc() : ClockInterface
{
return new UtcClock();
}

/**
* @inheritDoc
*/
public function local(DateTimeZone $time_zone) : ClockInterface
{
return new LocalClock($time_zone);
}
}
24 changes: 24 additions & 0 deletions src/Data/Clock/ClockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeImmutable;

interface ClockInterface
{
public function now() : DateTimeImmutable;
}
35 changes: 35 additions & 0 deletions src/Data/Clock/LocalClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeImmutable;
use DateTimeZone;

class LocalClock implements ClockInterface
{
private DateTimeZone $time_zone;

public function __construct(DateTimeZone $time_zone)
{
$this->time_zone = $time_zone;
}

public function now() : DateTimeImmutable
{
return new DateTimeImmutable('now', $this->time_zone);
}
}
27 changes: 27 additions & 0 deletions src/Data/Clock/SystemClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeImmutable;

class SystemClock implements ClockInterface
{
public function now() : DateTimeImmutable
{
return new DateTimeImmutable('now');
}
}
28 changes: 28 additions & 0 deletions src/Data/Clock/UtcClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeImmutable;
use DateTimeZone;

class UtcClock implements ClockInterface
{
public function now() : DateTimeImmutable
{
return new DateTimeImmutable('now', new DateTimeZone('UTC'));
}
}
8 changes: 8 additions & 0 deletions src/Data/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace ILIAS\Data;

use ILIAS\Data\Clock\ClockFactory;
use ILIAS\Data\Clock\ClockFactoryImpl;

/**
* Builds data types.
*
Expand Down Expand Up @@ -154,4 +157,9 @@ public function link(string $label, URI $url) : Link
{
return new Link($label, $url);
}

public function clock() : ClockFactory
{
return new ClockFactoryImpl();
}
}
68 changes: 68 additions & 0 deletions src/Data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
This service should contain standard datatypes for ILIAS that are used in many
locations in the system and do not belong to a certain service.

The keywords “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”,
“SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be
interpreted as described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt).

**Table of Contents**
* [Result](#result)
* [Color](#color)
* [URI](#uri)
* [DataSize](#datasize)
* [Password](#password)
* [ClientId](#clientid)
* [ReferenceId](#referenceid)
* [ObjectId](#objectid)
* [Alphanumeric](#alphanumeric)
* [PositiveInteger](#positiveinteger)
* [DateFormat](#dateformat)
* [Range](#Range)
* [Order](#order)
* [Clock](#clock)

Other examples for data types that could (and maybe should) be added here:

* Option (akin to rusts type)
Expand Down Expand Up @@ -387,3 +407,51 @@ assert($order2->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
<?php
$f = new \ILIAS\Data\Factory;

$clock = $f->clock()->system();
$now = $clock->now();
?>
```

#### UTC Clock

The `\ILIAS\Data\Clock\UtcClock` returns a `\DateTimeImmutable` instance always referring to the
`UTC` timezone.

```php
<?php
$f = new \ILIAS\Data\Factory;

$clock = $f->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
<?php
$f = new \ILIAS\Data\Factory;

$clock = $f->clock()->local(new \DateTimeZone('Europe/Berlin'));
$now = $clock->now();
?>
```
1 change: 1 addition & 0 deletions src/Data/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Roadmap
45 changes: 45 additions & 0 deletions tests/Data/LocalClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/

namespace ILIAS\Data\Clock;

use DateTimeZone;
use ILIAS\Data\Clock\LocalClock;
use PHPUnit\Framework\TestCase;

class LocalClockTest extends TestCase
{
private string $default_timezone;

protected function setUp() : void
{
$this->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());
}
}
Loading

0 comments on commit d0b2575

Please sign in to comment.