-
Notifications
You must be signed in to change notification settings - Fork 0
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
Christian Kolb
committed
May 6, 2024
1 parent
653a356
commit a065718
Showing
3 changed files
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
interface Clock | ||
{ | ||
public function now(): Moment; | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
final class FrozenClock implements Clock | ||
{ | ||
private Moment $moment; | ||
|
||
public function __construct(?Moment $moment = null) | ||
{ | ||
$this->moment = $moment ?? Moment::fromDateTime(new \DateTimeImmutable('now')); | ||
} | ||
|
||
public function freeze(Moment $moment): void | ||
{ | ||
$this->moment = $moment; | ||
} | ||
|
||
#[\Override] | ||
public function now(): Moment | ||
{ | ||
return $this->moment; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
final readonly class SystemClock implements Clock | ||
{ | ||
#[\Override] | ||
public function now(): Moment | ||
{ | ||
return Moment::fromDateTime(new \DateTimeImmutable( | ||
'now', | ||
new \DateTimeZone('UTC'), | ||
)); | ||
} | ||
} |