| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use DateTimeZone; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
trait Timestamp |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#[\ReturnTypeWillChange] |
| 29 |
public static function createFromTimestamp( |
| 30 |
float|int|string $timestamp, |
| 31 |
DateTimeZone|string|int|null $timezone = null, |
| 32 |
): static { |
| 33 |
$date = static::createFromTimestampUTC($timestamp); |
| 34 |
|
| 35 |
return $timezone === null ? $date : $date->setTimezone($timezone); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static function createFromTimestampUTC(float|int|string $timestamp): static |
| 44 |
{ |
| 45 |
[$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp); |
| 46 |
$delta = floor($decimal / static::MICROSECONDS_PER_SECOND); |
| 47 |
$integer += $delta; |
| 48 |
$decimal -= $delta * static::MICROSECONDS_PER_SECOND; |
| 49 |
$decimal = str_pad((string) $decimal, 6, '0', STR_PAD_LEFT); |
| 50 |
|
| 51 |
return static::rawCreateFromFormat('U u', "$integer $decimal"); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function createFromTimestampMsUTC($timestamp): static |
| 64 |
{ |
| 65 |
[$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3); |
| 66 |
$sign = $milliseconds < 0 || ($milliseconds === 0.0 && $microseconds < 0) ? -1 : 1; |
| 67 |
$milliseconds = abs($milliseconds); |
| 68 |
$microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND); |
| 69 |
$seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND); |
| 70 |
$delta = floor($microseconds / static::MICROSECONDS_PER_SECOND); |
| 71 |
$seconds = (int) ($seconds + $delta); |
| 72 |
$microseconds -= $delta * static::MICROSECONDS_PER_SECOND; |
| 73 |
$microseconds = str_pad((string) (int) $microseconds, 6, '0', STR_PAD_LEFT); |
| 74 |
|
| 75 |
return static::rawCreateFromFormat('U u', "$seconds $microseconds"); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function createFromTimestampMs( |
| 84 |
float|int|string $timestamp, |
| 85 |
DateTimeZone|string|int|null $timezone = null, |
| 86 |
): static { |
| 87 |
$date = static::createFromTimestampMsUTC($timestamp); |
| 88 |
|
| 89 |
return $timezone === null ? $date : $date->setTimezone($timezone); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function timestamp(float|int|string $timestamp): static |
| 98 |
{ |
| 99 |
return $this->setTimestamp($timestamp); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function getPreciseTimestamp($precision = 6): float |
| 121 |
{ |
| 122 |
return round(((float) $this->rawFormat('Uu')) / pow(10, 6 - $precision)); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function valueOf(): float |
| 131 |
{ |
| 132 |
return $this->getPreciseTimestamp(3); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function getTimestampMs(): int |
| 141 |
{ |
| 142 |
return (int) $this->getPreciseTimestamp(3); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function unix(): int |
| 153 |
{ |
| 154 |
return $this->getTimestamp(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private static function getIntegerAndDecimalParts($numbers, $decimals = 6): array |
| 170 |
{ |
| 171 |
if (\is_int($numbers) || \is_float($numbers)) { |
| 172 |
$numbers = number_format($numbers, $decimals, '.', ''); |
| 173 |
} |
| 174 |
|
| 175 |
$sign = str_starts_with($numbers, '-') ? -1 : 1; |
| 176 |
$integer = 0; |
| 177 |
$decimal = 0; |
| 178 |
|
| 179 |
foreach (preg_split('`[^\d.]+`', $numbers) as $chunk) { |
| 180 |
[$integerPart, $decimalPart] = explode('.', "$chunk."); |
| 181 |
|
| 182 |
$integer += (int) $integerPart; |
| 183 |
$decimal += (float) ("0.$decimalPart"); |
| 184 |
} |
| 185 |
|
| 186 |
$overflow = floor($decimal); |
| 187 |
$integer += $overflow; |
| 188 |
$decimal -= $overflow; |
| 189 |
|
| 190 |
return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))]; |
| 191 |
} |
| 192 |
} |