| 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 DatePoint extends \DateTimeImmutable |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null, ?parent $reference = null) |
| 25 |
{ |
| 26 |
$now = $reference ?? Clock::get()->now(); |
| 27 |
|
| 28 |
if ('now' !== $datetime) { |
| 29 |
if (!$now instanceof static) { |
| 30 |
$now = static::createFromInterface($now); |
| 31 |
} |
| 32 |
|
| 33 |
if (\PHP_VERSION_ID < 80300) { |
| 34 |
try { |
| 35 |
$builtInDate = new parent($datetime, $timezone ?? $now->getTimezone()); |
| 36 |
$timezone = $builtInDate->getTimezone(); |
| 37 |
} catch (\Exception $e) { |
| 38 |
throw new \DateMalformedStringException($e->getMessage(), $e->getCode(), $e); |
| 39 |
} |
| 40 |
} else { |
| 41 |
$builtInDate = new parent($datetime, $timezone ?? $now->getTimezone()); |
| 42 |
$timezone = $builtInDate->getTimezone(); |
| 43 |
} |
| 44 |
|
| 45 |
$now = $now->setTimezone($timezone)->modify($datetime); |
| 46 |
|
| 47 |
if ('00:00:00.000000' === $builtInDate->format('H:i:s.u')) { |
| 48 |
$now = $now->setTime(0, 0); |
| 49 |
} |
| 50 |
} elseif (null !== $timezone) { |
| 51 |
$now = $now->setTimezone($timezone); |
| 52 |
} |
| 53 |
|
| 54 |
$this->__unserialize((array) $now); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static function createFromFormat(string $format, string $datetime, ?\DateTimeZone $timezone = null): static |
| 61 |
{ |
| 62 |
return parent::createFromFormat($format, $datetime, $timezone) ?: throw new \DateMalformedStringException(static::getLastErrors()['errors'][0] ?? 'Invalid date string or format.'); |
| 63 |
} |
| 64 |
|
| 65 |
public static function createFromInterface(\DateTimeInterface $object): static |
| 66 |
{ |
| 67 |
return parent::createFromInterface($object); |
| 68 |
} |
| 69 |
|
| 70 |
public static function createFromMutable(\DateTime $object): static |
| 71 |
{ |
| 72 |
return parent::createFromMutable($object); |
| 73 |
} |
| 74 |
|
| 75 |
public static function createFromTimestamp(int|float $timestamp): static |
| 76 |
{ |
| 77 |
if (\PHP_VERSION_ID >= 80400) { |
| 78 |
return parent::createFromTimestamp($timestamp); |
| 79 |
} |
| 80 |
|
| 81 |
if (\is_int($timestamp) || !$ms = (int) $timestamp - $timestamp) { |
| 82 |
return static::createFromFormat('U', (string) $timestamp); |
| 83 |
} |
| 84 |
|
| 85 |
if (!is_finite($timestamp) || \PHP_INT_MAX + 1.0 <= $timestamp || \PHP_INT_MIN > $timestamp) { |
| 86 |
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)); |
| 87 |
} |
| 88 |
|
| 89 |
if ($timestamp < 0) { |
| 90 |
$timestamp = (int) $timestamp - 2.0 + $ms; |
| 91 |
} |
| 92 |
|
| 93 |
return static::createFromFormat('U.u', \sprintf('%.6F', $timestamp)); |
| 94 |
} |
| 95 |
|
| 96 |
public function add(\DateInterval $interval): static |
| 97 |
{ |
| 98 |
return parent::add($interval); |
| 99 |
} |
| 100 |
|
| 101 |
public function sub(\DateInterval $interval): static |
| 102 |
{ |
| 103 |
return parent::sub($interval); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function modify(string $modifier): static |
| 110 |
{ |
| 111 |
if (\PHP_VERSION_ID < 80300) { |
| 112 |
return @parent::modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? \sprintf('Invalid modifier: "%s".', $modifier)); |
| 113 |
} |
| 114 |
|
| 115 |
return parent::modify($modifier); |
| 116 |
} |
| 117 |
|
| 118 |
public function setTimestamp(int $value): static |
| 119 |
{ |
| 120 |
return parent::setTimestamp($value); |
| 121 |
} |
| 122 |
|
| 123 |
public function setDate(int $year, int $month, int $day): static |
| 124 |
{ |
| 125 |
return parent::setDate($year, $month, $day); |
| 126 |
} |
| 127 |
|
| 128 |
public function setISODate(int $year, int $week, int $day = 1): static |
| 129 |
{ |
| 130 |
return parent::setISODate($year, $week, $day); |
| 131 |
} |
| 132 |
|
| 133 |
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static |
| 134 |
{ |
| 135 |
return parent::setTime($hour, $minute, $second, $microsecond); |
| 136 |
} |
| 137 |
|
| 138 |
public function setTimezone(\DateTimeZone $timezone): static |
| 139 |
{ |
| 140 |
return parent::setTimezone($timezone); |
| 141 |
} |
| 142 |
|
| 143 |
public function getTimezone(): \DateTimeZone |
| 144 |
{ |
| 145 |
return parent::getTimezone() ?: throw new \DateInvalidTimeZoneException('The DatePoint object has no timezone.'); |
| 146 |
} |
| 147 |
|
| 148 |
public function setMicrosecond(int $microsecond): static |
| 149 |
{ |
| 150 |
if ($microsecond < 0 || $microsecond > 999999) { |
| 151 |
throw new \DateRangeError('DatePoint::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, '.$microsecond.' given'); |
| 152 |
} |
| 153 |
|
| 154 |
if (\PHP_VERSION_ID < 80400) { |
| 155 |
return $this->setTime(...explode('.', $this->format('H.i.s.'.$microsecond))); |
| 156 |
} |
| 157 |
|
| 158 |
return parent::setMicrosecond($microsecond); |
| 159 |
} |
| 160 |
|
| 161 |
public function getMicrosecond(): int |
| 162 |
{ |
| 163 |
if (\PHP_VERSION_ID >= 80400) { |
| 164 |
return parent::getMicrosecond(); |
| 165 |
} |
| 166 |
|
| 167 |
return $this->format('u'); |
| 168 |
} |
| 169 |
} |