Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
8 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
8 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
8 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Timestamp.php
76 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | trait Timestamp |
| 5 | { |
| 6 | #[\ReturnTypeWillChange] |
| 7 | public static function createFromTimestamp($timestamp, $tz = null) |
| 8 | { |
| 9 | return static::createFromTimestampUTC($timestamp)->setTimezone($tz); |
| 10 | } |
| 11 | public static function createFromTimestampUTC($timestamp) |
| 12 | { |
| 13 | [$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp); |
| 14 | $delta = \floor($decimal / static::MICROSECONDS_PER_SECOND); |
| 15 | $integer += $delta; |
| 16 | $decimal -= $delta * static::MICROSECONDS_PER_SECOND; |
| 17 | $decimal = \str_pad((string) $decimal, 6, '0', \STR_PAD_LEFT); |
| 18 | return static::rawCreateFromFormat('U u', "{$integer} {$decimal}"); |
| 19 | } |
| 20 | public static function createFromTimestampMsUTC($timestamp) |
| 21 | { |
| 22 | [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3); |
| 23 | $sign = $milliseconds < 0 || $milliseconds === 0.0 && $microseconds < 0 ? -1 : 1; |
| 24 | $milliseconds = \abs($milliseconds); |
| 25 | $microseconds = $sign * \abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND); |
| 26 | $seconds = $sign * \floor($milliseconds / static::MILLISECONDS_PER_SECOND); |
| 27 | $delta = \floor($microseconds / static::MICROSECONDS_PER_SECOND); |
| 28 | $seconds += $delta; |
| 29 | $microseconds -= $delta * static::MICROSECONDS_PER_SECOND; |
| 30 | $microseconds = \str_pad($microseconds, 6, '0', \STR_PAD_LEFT); |
| 31 | return static::rawCreateFromFormat('U u', "{$seconds} {$microseconds}"); |
| 32 | } |
| 33 | public static function createFromTimestampMs($timestamp, $tz = null) |
| 34 | { |
| 35 | return static::createFromTimestampMsUTC($timestamp)->setTimezone($tz); |
| 36 | } |
| 37 | public function timestamp($unixTimestamp) |
| 38 | { |
| 39 | return $this->setTimestamp($unixTimestamp); |
| 40 | } |
| 41 | public function getPreciseTimestamp($precision = 6) |
| 42 | { |
| 43 | return \round((float) $this->rawFormat('Uu') / \pow(10, 6 - $precision)); |
| 44 | } |
| 45 | public function valueOf() |
| 46 | { |
| 47 | return $this->getPreciseTimestamp(3); |
| 48 | } |
| 49 | public function getTimestampMs() |
| 50 | { |
| 51 | return (int) $this->getPreciseTimestamp(3); |
| 52 | } |
| 53 | public function unix() |
| 54 | { |
| 55 | return $this->getTimestamp(); |
| 56 | } |
| 57 | private static function getIntegerAndDecimalParts($numbers, $decimals = 6) |
| 58 | { |
| 59 | if (\is_int($numbers) || \is_float($numbers)) { |
| 60 | $numbers = \number_format($numbers, $decimals, '.', ''); |
| 61 | } |
| 62 | $sign = \str_starts_with($numbers, '-') ? -1 : 1; |
| 63 | $integer = 0; |
| 64 | $decimal = 0; |
| 65 | foreach (\preg_split('`[^\\d.]+`', $numbers) as $chunk) { |
| 66 | [$integerPart, $decimalPart] = \explode('.', "{$chunk}."); |
| 67 | $integer += (int) $integerPart; |
| 68 | $decimal += (float) "0.{$decimalPart}"; |
| 69 | } |
| 70 | $overflow = \floor($decimal); |
| 71 | $integer += $overflow; |
| 72 | $decimal -= $overflow; |
| 73 | return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * \round($decimal * \pow(10, $decimals))]; |
| 74 | } |
| 75 | } |
| 76 |