1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus framework.
7: *
8: * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Clock;
15:
16: /**
17: * A clock that always shows the same time.
18: */
19: final class FrozenClock implements Clock
20: {
21: public function __construct(
22: private \DateTimeImmutable $now,
23: ) {}
24:
25: public function now(): \DateTimeImmutable
26: {
27: return $this->now;
28: }
29:
30: public function sleep(float|int $seconds): void
31: {
32: $now = $this->now->format('U.u') + $seconds;
33: $now = \sprintf('@%0.6F', $now);
34: $timezone = $this->now->getTimezone();
35:
36: $this->now = (new \DateTimeImmutable($now))->setTimezone($timezone);
37: }
38: }
39: