MonotonicClock.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Clock;
  11. /**
  12. * A monotonic clock suitable for performance profiling.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. final class MonotonicClock implements ClockInterface
  17. {
  18. private int $sOffset;
  19. private int $usOffset;
  20. private \DateTimeZone $timezone;
  21. /**
  22. * @throws \DateInvalidTimeZoneException When $timezone is invalid
  23. */
  24. public function __construct(\DateTimeZone|string|null $timezone = null)
  25. {
  26. if (false === $offset = hrtime()) {
  27. throw new \RuntimeException('hrtime() returned false: the runtime environment does not provide access to a monotonic timer.');
  28. }
  29. $time = explode(' ', microtime(), 2);
  30. $this->sOffset = $time[1] - $offset[0];
  31. $this->usOffset = (int) ($time[0] * 1000000) - (int) ($offset[1] / 1000);
  32. $this->timezone = \is_string($timezone ??= date_default_timezone_get()) ? $this->withTimeZone($timezone)->timezone : $timezone;
  33. }
  34. public function now(): DatePoint
  35. {
  36. [$s, $us] = hrtime();
  37. if (1000000 <= $us = (int) ($us / 1000) + $this->usOffset) {
  38. ++$s;
  39. $us -= 1000000;
  40. } elseif (0 > $us) {
  41. --$s;
  42. $us += 1000000;
  43. }
  44. if (6 !== \strlen($now = (string) $us)) {
  45. $now = str_pad($now, 6, '0', \STR_PAD_LEFT);
  46. }
  47. $now = '@'.($s + $this->sOffset).'.'.$now;
  48. return DatePoint::createFromInterface(new \DateTimeImmutable($now, $this->timezone))->setTimezone($this->timezone);
  49. }
  50. public function sleep(float|int $seconds): void
  51. {
  52. if (0 < $s = (int) $seconds) {
  53. sleep($s);
  54. }
  55. if (0 < $us = $seconds - $s) {
  56. usleep((int) ($us * 1E6));
  57. }
  58. }
  59. /**
  60. * @throws \DateInvalidTimeZoneException When $timezone is invalid
  61. */
  62. public function withTimeZone(\DateTimeZone|string $timezone): static
  63. {
  64. if (\PHP_VERSION_ID >= 80300 && \is_string($timezone)) {
  65. $timezone = new \DateTimeZone($timezone);
  66. } elseif (\is_string($timezone)) {
  67. try {
  68. $timezone = new \DateTimeZone($timezone);
  69. } catch (\Exception $e) {
  70. throw new \DateInvalidTimeZoneException($e->getMessage(), $e->getCode(), $e);
  71. }
  72. }
  73. $clone = clone $this;
  74. $clone->timezone = $timezone;
  75. return $clone;
  76. }
  77. }