Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Timestamp.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use DateTimeZone;
17
18 /**
19 * Trait Timestamp.
20 */
21 trait Timestamp
22 {
23 /**
24 * Create a Carbon instance from a timestamp and set the timezone (UTC by default).
25 *
26 * Timestamp input can be given as int, float or a string containing one or more numbers.
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 * Create a Carbon instance from a timestamp keeping the timezone to UTC.
40 *
41 * Timestamp input can be given as int, float or a string containing one or more numbers.
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 * Create a Carbon instance from a timestamp in milliseconds.
56 *
57 * Timestamp input can be given as int, float or a string containing one or more numbers.
58 *
59 * @param float|int|string $timestamp
60 *
61 * @return static
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 * Create a Carbon instance from a timestamp in milliseconds.
80 *
81 * Timestamp input can be given as int, float or a string containing one or more numbers.
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 * Set the instance's timestamp.
94 *
95 * Timestamp input can be given as int, float or a string containing one or more numbers.
96 */
97 public function timestamp(float|int|string $timestamp): static
98 {
99 return $this->setTimestamp($timestamp);
100 }
101
102 /**
103 * Returns a timestamp rounded with the given precision (6 by default).
104 *
105 * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision)
106 * @example getPreciseTimestamp(6) 1532087464437474
107 * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision)
108 * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision)
109 * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision)
110 * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision)
111 * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision)
112 * @example getPreciseTimestamp(0) 1532087464 (second precision)
113 * @example getPreciseTimestamp(-1) 153208746 (10 second precision)
114 * @example getPreciseTimestamp(-2) 15320875 (100 second precision)
115 *
116 * @param int $precision
117 *
118 * @return float
119 */
120 public function getPreciseTimestamp($precision = 6): float
121 {
122 return round(((float) $this->rawFormat('Uu')) / pow(10, 6 - $precision));
123 }
124
125 /**
126 * Returns the milliseconds timestamps used amongst other by Date javascript objects.
127 *
128 * @return float
129 */
130 public function valueOf(): float
131 {
132 return $this->getPreciseTimestamp(3);
133 }
134
135 /**
136 * Returns the timestamp with millisecond precision.
137 *
138 * @return int
139 */
140 public function getTimestampMs(): int
141 {
142 return (int) $this->getPreciseTimestamp(3);
143 }
144
145 /**
146 * @alias getTimestamp
147 *
148 * Returns the UNIX timestamp for the current date.
149 *
150 * @return int
151 */
152 public function unix(): int
153 {
154 return $this->getTimestamp();
155 }
156
157 /**
158 * Return an array with integer part digits and decimals digits split from one or more positive numbers
159 * (such as timestamps) as string with the given number of decimals (6 by default).
160 *
161 * By splitting integer and decimal, this method obtain a better precision than
162 * number_format when the input is a string.
163 *
164 * @param float|int|string $numbers one or more numbers
165 * @param int $decimals number of decimals precision (6 by default)
166 *
167 * @return array 0-index is integer part, 1-index is decimal part digits
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 }