-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
109 additions
and
1 deletion.
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
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
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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\Clock\Adapter; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Orisai\Clock\Clock; | ||
use Symfony\Component\Clock\ClockInterface; | ||
use function is_string; | ||
use function round; | ||
|
||
final class OrisaiToSymfonyClockAdapter implements ClockInterface | ||
{ | ||
|
||
private Clock $clock; | ||
|
||
private ?DateTimeZone $timeZone = null; | ||
|
||
public function __construct(Clock $clock) | ||
{ | ||
$this->clock = $clock; | ||
} | ||
|
||
public function now(): DateTimeImmutable | ||
{ | ||
$dt = $this->clock->now(); | ||
|
||
if ($this->timeZone !== null) { | ||
$dt = $dt->setTimezone($this->timeZone); | ||
} | ||
|
||
return $dt; | ||
} | ||
|
||
public function sleep(float|int $seconds): void | ||
{ | ||
$this->clock->sleep(0, 0, (int) round($seconds * 1E6)); | ||
Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php GitHub Actions / Test for mutants (ubuntu-latest, 8.1)
Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php GitHub Actions / Test for mutants (ubuntu-latest, 8.1)
Check warning on line 37 in src/Adapter/OrisaiToSymfonyClockAdapter.php GitHub Actions / Test for mutants (ubuntu-latest, 8.1)
|
||
} | ||
|
||
public function withTimeZone(DateTimeZone|string $timezone): static | ||
{ | ||
$clone = clone $this; | ||
Check warning on line 42 in src/Adapter/OrisaiToSymfonyClockAdapter.php GitHub Actions / Test for mutants (ubuntu-latest, 8.1)
|
||
$clone->timeZone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone; | ||
|
||
return $clone; | ||
} | ||
|
||
} |
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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Clock\Unit\Adapter; | ||
|
||
use DateTimeZone; | ||
use Orisai\Clock\Adapter\OrisaiToSymfonyClockAdapter; | ||
use Orisai\Clock\FrozenClock; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Clock\ClockInterface; | ||
use function interface_exists; | ||
|
||
final class OrisaiToSymfonyClockAdapterTest extends TestCase | ||
{ | ||
|
||
public function test(): void | ||
{ | ||
if (!interface_exists(ClockInterface::class)) { | ||
self::markTestSkipped('symfony/clock is required.'); | ||
} | ||
|
||
$innerClock = new FrozenClock(0, new DateTimeZone('UTC')); | ||
$clock = new OrisaiToSymfonyClockAdapter($innerClock); | ||
|
||
self::assertSame( | ||
'0.000000', | ||
$clock->now()->format('U.u'), | ||
); | ||
|
||
$clock->sleep(1.234_567_8); | ||
self::assertSame( | ||
'1.234568', | ||
$clock->now()->format('U.u'), | ||
); | ||
|
||
self::assertEquals( | ||
$clock->now()->getTimezone(), | ||
new DateTimeZone('UTC'), | ||
); | ||
|
||
$clock = $clock->withTimeZone(new DateTimeZone('Europe/Prague')); | ||
self::assertEquals( | ||
$clock->now()->getTimezone(), | ||
new DateTimeZone('Europe/Prague'), | ||
); | ||
} | ||
|
||
} |
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