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