Debian Codegraph @

lib/3rdParty/composer/symfony/clock/NativeClock.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Clock;
13
14 /**
15 * A clock that relies the system time.
16 *
17 * @author Nicolas Grekas <[email protected]>
18 */
19 final class NativeClock implements ClockInterface
20 {
21 private \DateTimeZone $timezone;
22
23 /**
24 * @throws \DateInvalidTimeZoneException When $timezone is invalid
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 * @throws \DateInvalidTimeZoneException When $timezone is invalid
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 }