Boundaries.php
2 years ago
Cast.php
2 years ago
Comparison.php
1 year ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
1 month ago
DeprecatedProperties.php
2 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
2 years ago
Localization.php
1 month ago
Macro.php
2 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
2 years ago
Mutability.php
2 years ago
ObjectInitialisation.php
2 years ago
Options.php
1 year ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
1 month ago
Timestamp.php
1 month ago
ToStringFormat.php
2 years ago
Units.php
2 years ago
Week.php
2 years ago
Timestamp.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Carbon package. |
| 5 | * |
| 6 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Carbon\Traits; |
| 12 | |
| 13 | /** |
| 14 | * Trait Timestamp. |
| 15 | * @internal |
| 16 | */ |
| 17 | trait Timestamp |
| 18 | { |
| 19 | /** |
| 20 | * Create a Carbon instance from a timestamp and set the timezone (use default one if not specified). |
| 21 | * |
| 22 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 23 | * |
| 24 | * @param float|int|string $timestamp |
| 25 | * @param \DateTimeZone|string|null $tz |
| 26 | * |
| 27 | * @return static |
| 28 | */ |
| 29 | #[\ReturnTypeWillChange] |
| 30 | public static function createFromTimestamp($timestamp, $tz = null) |
| 31 | { |
| 32 | return static::createFromTimestampUTC($timestamp)->setTimezone($tz); |
| 33 | } |
| 34 | /** |
| 35 | * Create a Carbon instance from an timestamp keeping the timezone to UTC. |
| 36 | * |
| 37 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 38 | * |
| 39 | * @param float|int|string $timestamp |
| 40 | * |
| 41 | * @return static |
| 42 | */ |
| 43 | public static function createFromTimestampUTC($timestamp) |
| 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 | return static::rawCreateFromFormat('U u', "{$integer} {$decimal}"); |
| 51 | } |
| 52 | /** |
| 53 | * Create a Carbon instance from a timestamp in milliseconds. |
| 54 | * |
| 55 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 56 | * |
| 57 | * @param float|int|string $timestamp |
| 58 | * |
| 59 | * @return static |
| 60 | */ |
| 61 | public static function createFromTimestampMsUTC($timestamp) |
| 62 | { |
| 63 | [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3); |
| 64 | $sign = $milliseconds < 0 || $milliseconds === 0.0 && $microseconds < 0 ? -1 : 1; |
| 65 | $milliseconds = \abs($milliseconds); |
| 66 | $microseconds = $sign * \abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND); |
| 67 | $seconds = $sign * \floor($milliseconds / static::MILLISECONDS_PER_SECOND); |
| 68 | $delta = \floor($microseconds / static::MICROSECONDS_PER_SECOND); |
| 69 | $seconds += $delta; |
| 70 | $microseconds -= $delta * static::MICROSECONDS_PER_SECOND; |
| 71 | $microseconds = \str_pad($microseconds, 6, '0', \STR_PAD_LEFT); |
| 72 | return static::rawCreateFromFormat('U u', "{$seconds} {$microseconds}"); |
| 73 | } |
| 74 | /** |
| 75 | * Create a Carbon instance from a timestamp in milliseconds. |
| 76 | * |
| 77 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 78 | * |
| 79 | * @param float|int|string $timestamp |
| 80 | * @param \DateTimeZone|string|null $tz |
| 81 | * |
| 82 | * @return static |
| 83 | */ |
| 84 | public static function createFromTimestampMs($timestamp, $tz = null) |
| 85 | { |
| 86 | return static::createFromTimestampMsUTC($timestamp)->setTimezone($tz); |
| 87 | } |
| 88 | /** |
| 89 | * Set the instance's timestamp. |
| 90 | * |
| 91 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 92 | * |
| 93 | * @param float|int|string $unixTimestamp |
| 94 | * |
| 95 | * @return static |
| 96 | */ |
| 97 | public function timestamp($unixTimestamp) |
| 98 | { |
| 99 | return $this->setTimestamp($unixTimestamp); |
| 100 | } |
| 101 | /** |
| 102 | * Returns a timestamp rounded with the given precision (6 by default). |
| 103 | * |
| 104 | * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision) |
| 105 | * @example getPreciseTimestamp(6) 1532087464437474 |
| 106 | * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision) |
| 107 | * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision) |
| 108 | * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision) |
| 109 | * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision) |
| 110 | * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision) |
| 111 | * @example getPreciseTimestamp(0) 1532087464 (second precision) |
| 112 | * @example getPreciseTimestamp(-1) 153208746 (10 second precision) |
| 113 | * @example getPreciseTimestamp(-2) 15320875 (100 second precision) |
| 114 | * |
| 115 | * @param int $precision |
| 116 | * |
| 117 | * @return float |
| 118 | */ |
| 119 | public function getPreciseTimestamp($precision = 6) |
| 120 | { |
| 121 | return \round((float) $this->rawFormat('Uu') / \pow(10, 6 - $precision)); |
| 122 | } |
| 123 | /** |
| 124 | * Returns the milliseconds timestamps used amongst other by Date javascript objects. |
| 125 | * |
| 126 | * @return float |
| 127 | */ |
| 128 | public function valueOf() |
| 129 | { |
| 130 | return $this->getPreciseTimestamp(3); |
| 131 | } |
| 132 | /** |
| 133 | * Returns the timestamp with millisecond precision. |
| 134 | * |
| 135 | * @return int |
| 136 | */ |
| 137 | public function getTimestampMs() |
| 138 | { |
| 139 | return (int) $this->getPreciseTimestamp(3); |
| 140 | } |
| 141 | /** |
| 142 | * @alias getTimestamp |
| 143 | * |
| 144 | * Returns the UNIX timestamp for the current date. |
| 145 | * |
| 146 | * @return int |
| 147 | */ |
| 148 | public function unix() |
| 149 | { |
| 150 | return $this->getTimestamp(); |
| 151 | } |
| 152 | /** |
| 153 | * Return an array with integer part digits and decimals digits split from one or more positive numbers |
| 154 | * (such as timestamps) as string with the given number of decimals (6 by default). |
| 155 | * |
| 156 | * By splitting integer and decimal, this method obtain a better precision than |
| 157 | * number_format when the input is a string. |
| 158 | * |
| 159 | * @param float|int|string $numbers one or more numbers |
| 160 | * @param int $decimals number of decimals precision (6 by default) |
| 161 | * |
| 162 | * @return array 0-index is integer part, 1-index is decimal part digits |
| 163 | */ |
| 164 | private static function getIntegerAndDecimalParts($numbers, $decimals = 6) |
| 165 | { |
| 166 | if (\is_int($numbers) || \is_float($numbers)) { |
| 167 | $numbers = \number_format($numbers, $decimals, '.', ''); |
| 168 | } |
| 169 | $sign = \str_starts_with($numbers, '-') ? -1 : 1; |
| 170 | $integer = 0; |
| 171 | $decimal = 0; |
| 172 | foreach (\preg_split('`[^\\d.]+`', $numbers) as $chunk) { |
| 173 | [$integerPart, $decimalPart] = \explode('.', "{$chunk}."); |
| 174 | $integer += (int) $integerPart; |
| 175 | $decimal += (float) "0.{$decimalPart}"; |
| 176 | } |
| 177 | $overflow = \floor($decimal); |
| 178 | $integer += $overflow; |
| 179 | $decimal -= $overflow; |
| 180 | return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * \round($decimal * \pow(10, $decimals))]; |
| 181 | } |
| 182 | } |
| 183 |