-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathInstant.php
More file actions
378 lines (317 loc) · 9.63 KB
/
Instant.php
File metadata and controls
378 lines (317 loc) · 9.63 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
declare(strict_types=1);
namespace Brick\DateTime;
use JsonSerializable;
use Stringable;
use function assert;
use function is_int;
use function rtrim;
use function str_pad;
use const PHP_INT_MAX;
use const PHP_INT_MIN;
use const STR_PAD_LEFT;
/**
* Represents a point in time, with a nanosecond precision.
*
* Instant represents the computer view of the timeline. It unambiguously represents a point in time,
* without any calendar concept of date, time or time zone. It is not very meaningful to humans,
* but can be converted to a `ZonedDateTime` by providing a time zone.
*/
final class Instant implements JsonSerializable, Stringable
{
/**
* Private constructor. Use of() to obtain an Instant.
*
* @param int $epochSecond The number of seconds since the epoch of 1970-01-01T00:00:00Z.
* @param int $nano The nanosecond adjustment to the epoch second, validated in the range 0 to 999,999,999.
*/
private function __construct(
private readonly int $epochSecond,
private readonly int $nano,
) {
}
/**
* Returns an Instant representing a number of seconds and an adjustment in nanoseconds.
*
* This method allows an arbitrary number of nanoseconds to be passed in.
* The factory will alter the values of the second and nanosecond in order
* to ensure that the stored nanosecond is in the range 0 to 999,999,999.
* For example, the following will result in exactly the same instant:
*
* * Instant::of(3, 1);
* * Instant::of(4, -999_999_999);
* * Instant::of(2, 1_000_000_001);
*
* @param int $epochSecond The number of seconds since the UNIX epoch of 1970-01-01T00:00:00Z.
* @param int $nanoAdjustment The adjustment to the epoch second in nanoseconds.
*/
public static function of(int $epochSecond, int $nanoAdjustment = 0): Instant
{
$nanos = $nanoAdjustment % LocalTime::NANOS_PER_SECOND;
$epochSecond += ($nanoAdjustment - $nanos) / LocalTime::NANOS_PER_SECOND;
assert(is_int($epochSecond));
if ($nanos < 0) {
$nanos += LocalTime::NANOS_PER_SECOND;
$epochSecond--;
}
return new Instant($epochSecond, $nanos);
}
public static function epoch(): Instant
{
/** @var Instant|null $epoch */
static $epoch = null;
return $epoch ??= new Instant(0, 0);
}
public static function now(?Clock $clock = null): Instant
{
if ($clock === null) {
$clock = DefaultClock::get();
}
return $clock->getTime();
}
/**
* Returns the minimum supported instant.
*
* This could be used by an application as a "far past" instant.
*/
public static function min(): Instant
{
/** @var Instant|null $min */
static $min = null;
return $min ??= new Instant(PHP_INT_MIN, 0);
}
/**
* Returns the maximum supported instant.
*
* This could be used by an application as a "far future" instant.
*/
public static function max(): Instant
{
/** @var Instant|null $max */
static $max = null;
return $max ??= new Instant(PHP_INT_MAX, 999_999_999);
}
public function plus(Duration $duration): Instant
{
if ($duration->isZero()) {
return $this;
}
$seconds = $this->epochSecond + $duration->getSeconds();
$nanos = $this->nano + $duration->getNanos();
return Instant::of($seconds, $nanos);
}
public function minus(Duration $duration): Instant
{
if ($duration->isZero()) {
return $this;
}
return $this->plus($duration->negated());
}
public function plusSeconds(int $seconds): Instant
{
if ($seconds === 0) {
return $this;
}
return new Instant($this->epochSecond + $seconds, $this->nano);
}
public function minusSeconds(int $seconds): Instant
{
return $this->plusSeconds(-$seconds);
}
public function plusMinutes(int $minutes): Instant
{
return $this->plusSeconds($minutes * LocalTime::SECONDS_PER_MINUTE);
}
public function minusMinutes(int $minutes): Instant
{
return $this->plusMinutes(-$minutes);
}
public function plusHours(int $hours): Instant
{
return $this->plusSeconds($hours * LocalTime::SECONDS_PER_HOUR);
}
public function minusHours(int $hours): Instant
{
return $this->plusHours(-$hours);
}
public function plusDays(int $days): Instant
{
return $this->plusSeconds($days * LocalTime::SECONDS_PER_DAY);
}
/**
* Returns a copy of this Instant with the epoch second altered.
*/
public function withEpochSecond(int $epochSecond): Instant
{
if ($epochSecond === $this->epochSecond) {
return $this;
}
return new Instant($epochSecond, $this->nano);
}
/**
* Returns a copy of this Instant with the nano-of-second altered.
*
* @throws DateTimeException If the nano-of-second if not valid.
*/
public function withNano(int $nano): Instant
{
if ($nano === $this->nano) {
return $this;
}
Field\NanoOfSecond::check($nano);
return new Instant($this->epochSecond, $nano);
}
public function minusDays(int $days): Instant
{
return $this->plusDays(-$days);
}
public function getEpochSecond(): int
{
return $this->epochSecond;
}
public function getNano(): int
{
return $this->nano;
}
/**
* Compares this instant with another.
*
* @return int [-1,0,1] If this instant is before, on, or after the given instant.
*
* @psalm-return -1|0|1
*/
public function compareTo(Instant $that): int
{
$seconds = $this->getEpochSecond() - $that->getEpochSecond();
if ($seconds !== 0) {
return $seconds > 0 ? 1 : -1;
}
$nanos = $this->getNano() - $that->getNano();
if ($nanos !== 0) {
return $nanos > 0 ? 1 : -1;
}
return 0;
}
/**
* Returns whether this instant equals another.
*/
public function isEqualTo(Instant $that): bool
{
return $this->compareTo($that) === 0;
}
/**
* Returns whether this instant is after another.
*/
public function isAfter(Instant $that): bool
{
return $this->compareTo($that) === 1;
}
/**
* Returns whether this instant is after or equal to another.
*/
public function isAfterOrEqualTo(Instant $that): bool
{
return $this->compareTo($that) >= 0;
}
/**
* Returns whether this instant is before another.
*/
public function isBefore(Instant $that): bool
{
return $this->compareTo($that) === -1;
}
/**
* Returns whether this instant is before or equal to another.
*/
public function isBeforeOrEqualTo(Instant $that): bool
{
return $this->compareTo($that) <= 0;
}
public function isBetweenInclusive(Instant $from, Instant $to): bool
{
return $this->isAfterOrEqualTo($from) && $this->isBeforeOrEqualTo($to);
}
public function isBetweenExclusive(Instant $from, Instant $to): bool
{
return $this->isAfter($from) && $this->isBefore($to);
}
/**
* Returns whether this instant is in the future, according to the given clock.
*
* If no clock is provided, the system clock is used.
*/
public function isFuture(?Clock $clock = null): bool
{
return $this->isAfter(Instant::now($clock));
}
/**
* Returns whether this instant is in the past, according to the given clock.
*
* If no clock is provided, the system clock is used.
*/
public function isPast(?Clock $clock = null): bool
{
return $this->isBefore(Instant::now($clock));
}
/**
* Returns a ZonedDateTime formed from this instant and the specified time-zone.
*/
public function atTimeZone(TimeZone $timeZone): ZonedDateTime
{
return ZonedDateTime::ofInstant($this, $timeZone);
}
/**
* Returns an Interval from this Instant (inclusive) to the given one (exclusive).
*
* @throws DateTimeException If the given Instant is before this Instant.
*/
public function getIntervalTo(Instant $that): Interval
{
return Interval::of($this, $that);
}
/**
* Returns a decimal representation of the timestamp represented by this instant.
*
* The output does not have trailing decimal zeros.
*
* Examples: `123456789`, `123456789.5`, `123456789.000000001`.
*/
public function toDecimal(): string
{
$result = (string) $this->epochSecond;
if ($this->nano !== 0) {
$nano = (string) $this->nano;
$nano = str_pad($nano, 9, '0', STR_PAD_LEFT);
$nano = rtrim($nano, '0');
$result .= '.' . $nano;
}
return $result;
}
/**
* Serializes as a string using {@see Instant::toISOString()}.
*
* @psalm-return non-empty-string
*/
public function jsonSerialize(): string
{
return $this->toISOString();
}
/**
* Returns the ISO 8601 representation of this instant.
*
* @psalm-return non-empty-string
*/
public function toISOString(): string
{
return (string) ZonedDateTime::ofInstant($this, TimeZoneOffset::utc());
}
/**
* {@see Instant::toISOString()}.
*
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->toISOString();
}
}