-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
2 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Clock; | ||
|
||
use Psl\DateTime; | ||
|
||
interface ClockInterface | ||
{ | ||
public function now(): DateTime\DateTimeInterface; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Clock; | ||
|
||
use Psl\DateTime; | ||
|
||
final class FrozenClock implements ClockInterface | ||
{ | ||
public function __construct(private DateTime\DateTime $dateTime) | ||
{ | ||
} | ||
|
||
public function synchronize(DateTime\DateTime $dateTime): void | ||
{ | ||
$this->dateTime = $dateTime; | ||
} | ||
|
||
public function now(): DateTime\DateTimeInterface | ||
{ | ||
return $this->dateTime; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Clock; | ||
|
||
use Psl\DateTime; | ||
use Psr\Clock\ClockInterface as PsrClockInterface; | ||
use function Psl\invariant; | ||
|
||
final class PsrClock implements ClockInterface | ||
{ | ||
public function __construct(private PsrClockInterface $psrClock) | ||
{ | ||
} | ||
|
||
public function now(): DateTime\DateTimeInterface | ||
{ | ||
$nativeDateTime = $this->psrClock->now(); | ||
|
||
$timestamp = DateTime\Timestamp::fromParts($nativeDateTime->getTimestamp()); | ||
|
||
$timezone = $nativeDateTime->getTimezone(); | ||
invariant($timezone !== false, 'Could not get the timezone object.'); | ||
|
||
$timezone = DateTime\Timezone::from($timezone->getName()); | ||
|
||
return DateTime\DateTime::fromTimestamp($timestamp, $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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Clock; | ||
|
||
use Psl\Clock\ClockInterface; | ||
use Psl\DateTime; | ||
|
||
final readonly class SystemClock implements ClockInterface | ||
{ | ||
private function __construct(private DateTime\Timezone $timezone) | ||
{ | ||
} | ||
|
||
public static function fromTimezone(DateTime\Timezone $timezone): self | ||
{ | ||
return new self($timezone); | ||
} | ||
|
||
public static function fromSystemTimezone(): self | ||
{ | ||
return new self(DateTime\Timezone::from(date_default_timezone_get())); | ||
} | ||
|
||
public function now(): DateTime\DateTimeInterface | ||
{ | ||
return DateTime\DateTime::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); | ||
|
||
namespace Psl\Tests\Unit\Clock; | ||
|
||
use Psl\Clock\PsrClock; | ||
use PHPUnit\Framework; | ||
use Psr\Clock\ClockInterface as PsrClockInterface; | ||
use Psl\DateTime; | ||
|
||
final class PsrClockTest extends Framework\TestCase | ||
{ | ||
public function testNow(): void | ||
{ | ||
$clock = new PsrClock( | ||
new FixedPsrClock( | ||
new \DateTimeImmutable('2020-01-01 00:00:00', new \DateTimeZone('Africa/Tunis')) | ||
) | ||
); | ||
|
||
self::assertTrue( | ||
$clock->now()->equalsIncludingTimezone( | ||
DateTime\DateTime::parse( | ||
'2020-01-01 00:00:00', | ||
DateTime\FormatPattern::SqlDateTime, | ||
DateTime\Timezone::AfricaTunis, | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
class FixedPsrClock implements PsrClockInterface | ||
{ | ||
public function __construct(private readonly \DateTimeImmutable $now) | ||
{ | ||
} | ||
|
||
public function now(): \DateTimeImmutable | ||
{ | ||
return $this->now; | ||
} | ||
} |