Skip to content

Commit

Permalink
Add clock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed May 6, 2024
1 parent 653a356 commit a065718
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Clock/Clock.php
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;
}
28 changes: 28 additions & 0 deletions src/Clock/FrozenClock.php
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;
}
}
19 changes: 19 additions & 0 deletions src/Clock/SystemClock.php
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'),
));
}
}

0 comments on commit a065718

Please sign in to comment.