1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | namespace Nexus\Clock; |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | final readonly class SystemClock implements Clock |
22: | { |
23: | private \DateTimeZone $timezone; |
24: | |
25: | public function __construct(\DateTimeZone|string $timezone) |
26: | { |
27: | $this->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone; |
28: | } |
29: | |
30: | public function now(): \DateTimeImmutable |
31: | { |
32: | return new \DateTimeImmutable('now', $this->timezone); |
33: | } |
34: | |
35: | public function sleep(float|int $seconds): void |
36: | { |
37: | if ($seconds <= 0) { |
38: | return; |
39: | } |
40: | |
41: | $microseconds = (int) ($seconds * 1_000_000); |
42: | $seconds = (int) floor($microseconds / 1_000_000); |
43: | $microseconds %= 1_000_000; |
44: | |
45: | sleep($seconds); |
46: | usleep($microseconds); |
47: | } |
48: | } |
49: | |