DatePoint.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * An immmutable DateTime with stricter error handling and return types than the native one.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. final class DatePoint extends \DateTimeImmutable
  17. {
  18. /**
  19. * @throws \DateMalformedStringException When $datetime is invalid
  20. */
  21. public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null, ?parent $reference = null)
  22. {
  23. $now = $reference ?? Clock::get()->now();
  24. if ('now' !== $datetime) {
  25. if (!$now instanceof static) {
  26. $now = static::createFromInterface($now);
  27. }
  28. if (\PHP_VERSION_ID < 80300) {
  29. try {
  30. $builtInDate = new parent($datetime, $timezone ?? $now->getTimezone());
  31. $timezone = $builtInDate->getTimezone();
  32. } catch (\Exception $e) {
  33. throw new \DateMalformedStringException($e->getMessage(), $e->getCode(), $e);
  34. }
  35. } else {
  36. $builtInDate = new parent($datetime, $timezone ?? $now->getTimezone());
  37. $timezone = $builtInDate->getTimezone();
  38. }
  39. $now = $now->setTimezone($timezone)->modify($datetime);
  40. if ('00:00:00.000000' === $builtInDate->format('H:i:s.u')) {
  41. $now = $now->setTime(0, 0);
  42. }
  43. } elseif (null !== $timezone) {
  44. $now = $now->setTimezone($timezone);
  45. }
  46. $this->__unserialize((array) $now);
  47. }
  48. /**
  49. * @throws \DateMalformedStringException When $format or $datetime are invalid
  50. */
  51. public static function createFromFormat(string $format, string $datetime, ?\DateTimeZone $timezone = null): static
  52. {
  53. return parent::createFromFormat($format, $datetime, $timezone) ?: throw new \DateMalformedStringException(static::getLastErrors()['errors'][0] ?? 'Invalid date string or format.');
  54. }
  55. public static function createFromInterface(\DateTimeInterface $object): static
  56. {
  57. return parent::createFromInterface($object);
  58. }
  59. public static function createFromMutable(\DateTime $object): static
  60. {
  61. return parent::createFromMutable($object);
  62. }
  63. public static function createFromTimestamp(int|float $timestamp): static
  64. {
  65. if (\PHP_VERSION_ID >= 80400) {
  66. return parent::createFromTimestamp($timestamp);
  67. }
  68. if (\is_int($timestamp) || !$ms = (int) $timestamp - $timestamp) {
  69. return static::createFromFormat('U', (string) $timestamp);
  70. }
  71. if (!is_finite($timestamp) || \PHP_INT_MAX + 1.0 <= $timestamp || \PHP_INT_MIN > $timestamp) {
  72. throw new \DateRangeError(\sprintf('DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %s and %s.999999, %s given', \PHP_INT_MIN, \PHP_INT_MAX, $timestamp));
  73. }
  74. if ($timestamp < 0) {
  75. $timestamp = (int) $timestamp - 2.0 + $ms;
  76. }
  77. return static::createFromFormat('U.u', \sprintf('%.6F', $timestamp));
  78. }
  79. public function add(\DateInterval $interval): static
  80. {
  81. return parent::add($interval);
  82. }
  83. public function sub(\DateInterval $interval): static
  84. {
  85. return parent::sub($interval);
  86. }
  87. /**
  88. * @throws \DateMalformedStringException When $modifier is invalid
  89. */
  90. public function modify(string $modifier): static
  91. {
  92. if (\PHP_VERSION_ID < 80300) {
  93. return @parent::modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? \sprintf('Invalid modifier: "%s".', $modifier));
  94. }
  95. return parent::modify($modifier);
  96. }
  97. public function setTimestamp(int $value): static
  98. {
  99. return parent::setTimestamp($value);
  100. }
  101. public function setDate(int $year, int $month, int $day): static
  102. {
  103. return parent::setDate($year, $month, $day);
  104. }
  105. public function setISODate(int $year, int $week, int $day = 1): static
  106. {
  107. return parent::setISODate($year, $week, $day);
  108. }
  109. public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static
  110. {
  111. return parent::setTime($hour, $minute, $second, $microsecond);
  112. }
  113. public function setTimezone(\DateTimeZone $timezone): static
  114. {
  115. return parent::setTimezone($timezone);
  116. }
  117. public function getTimezone(): \DateTimeZone
  118. {
  119. return parent::getTimezone() ?: throw new \DateInvalidTimeZoneException('The DatePoint object has no timezone.');
  120. }
  121. public function setMicrosecond(int $microsecond): static
  122. {
  123. if ($microsecond < 0 || $microsecond > 999999) {
  124. throw new \DateRangeError('DatePoint::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, '.$microsecond.' given');
  125. }
  126. if (\PHP_VERSION_ID < 80400) {
  127. return $this->setTime(...explode('.', $this->format('H.i.s.'.$microsecond)));
  128. }
  129. return parent::setMicrosecond($microsecond);
  130. }
  131. public function getMicrosecond(): int
  132. {
  133. if (\PHP_VERSION_ID >= 80400) {
  134. return parent::getMicrosecond();
  135. }
  136. return $this->format('u');
  137. }
  138. }