| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Clock; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class NativeClock implements ClockInterface |
| 20 |
{ |
| 21 |
private \DateTimeZone $timezone; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct(\DateTimeZone|string|null $timezone = null) |
| 27 |
{ |
| 28 |
$this->timezone = \is_string($timezone ??= date_default_timezone_get()) ? $this->withTimeZone($timezone)->timezone : $timezone; |
| 29 |
} |
| 30 |
|
| 31 |
public function now(): DatePoint |
| 32 |
{ |
| 33 |
return DatePoint::createFromInterface(new \DateTimeImmutable('now', $this->timezone)); |
| 34 |
} |
| 35 |
|
| 36 |
public function sleep(float|int $seconds): void |
| 37 |
{ |
| 38 |
if (0 < $s = (int) $seconds) { |
| 39 |
sleep($s); |
| 40 |
} |
| 41 |
|
| 42 |
if (0 < $us = $seconds - $s) { |
| 43 |
usleep((int) ($us * 1E6)); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function withTimeZone(\DateTimeZone|string $timezone): static |
| 51 |
{ |
| 52 |
if (\PHP_VERSION_ID >= 80300 && \is_string($timezone)) { |
| 53 |
$timezone = new \DateTimeZone($timezone); |
| 54 |
} elseif (\is_string($timezone)) { |
| 55 |
try { |
| 56 |
$timezone = new \DateTimeZone($timezone); |
| 57 |
} catch (\Exception $e) { |
| 58 |
throw new \DateInvalidTimeZoneException($e->getMessage(), $e->getCode(), $e); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$clone = clone $this; |
| 63 |
$clone->timezone = $timezone; |
| 64 |
|
| 65 |
return $clone; |
| 66 |
} |
| 67 |
} |