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