-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathDuration.php
More file actions
641 lines (571 loc) · 20.1 KB
/
Duration.php
File metadata and controls
641 lines (571 loc) · 20.1 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<?php
declare(strict_types=1);
namespace Tempest\DateTime;
use JsonSerializable;
use Override;
use Stringable;
use Tempest\Support\Comparison;
use Tempest\Support\Comparison\Comparable;
use Tempest\Support\Comparison\Equable;
use Tempest\Support\Comparison\Order;
use Tempest\Support\Str;
/**
* Defines a representation of a time duration with specific hours, minutes, seconds, and nanoseconds.
*
* All instances are normalized as follows:
*
* - all non-zero parts (hours, minutes, seconds, nanoseconds) will have the same sign
* - minutes, seconds will be between -59 and 59
* - nanoseconds will be between -999999999 and 999999999 (less than 1 second)
*
* For example, Duration::hours(2, -183) normalizes to "-1 hour(s), -3 minute(s)".
*
* @implements Comparison\Comparable<Duration>
* @implements Comparison\Equable<Duration>
*/
final readonly class Duration implements Comparable, Equable, JsonSerializable, Stringable
{
/**
* Initializes a new instance of Duration with specified hours, minutes, seconds, and nanoseconds.
*
* @param int<-59, 59> $minutes
* @param int<-59, 59> $seconds
* @param int<-999999999, 999999999> $nanoseconds
*/
private function __construct(
private int $hours,
private int $minutes,
private int $seconds,
private int $nanoseconds,
) {}
/**
* Returns an instance representing the specified number of hours (and
* optionally minutes, seconds, nanoseconds). Due to normalization, the
* actual values in the returned instance may differ from the provided ones.
*/
public static function fromParts(int $hours, int $minutes = 0, int $seconds = 0, int $nanoseconds = 0): self
{
// This is where the normalization happens.
$s = (SECONDS_PER_HOUR * $hours) + (SECONDS_PER_MINUTE * $minutes) + $seconds + (int) ($nanoseconds / NANOSECONDS_PER_SECOND);
$ns = $nanoseconds % NANOSECONDS_PER_SECOND;
if ($s < 0 && $ns > 0) {
++$s;
$ns -= NANOSECONDS_PER_SECOND;
} elseif ($s > 0 && $ns < 0) {
--$s;
$ns += NANOSECONDS_PER_SECOND;
}
$m = (int) ($s / 60);
$s %= 60;
$h = (int) ($m / 60);
$m %= 60;
return new self($h, $m, $s, $ns);
}
/**
* Returns an instance representing the specified number of weeks, in hours. For example, `Duration::weeks(1)` is equivalent to `Duration::hours(168)`.
*/
public static function weeks(int $weeks): self
{
return self::fromParts($weeks * HOURS_PER_WEEK);
}
/**
* Returns an instance representing exactly one week.
*/
public static function week(): self
{
return self::weeks(1);
}
/**
* Returns an instance representing the specified number of days, in hours.
*
* For example, `Duration::days(2)` is equivalent to `Duration::hours(48)`.
*/
public static function days(int $days): self
{
return self::fromParts($days * HOURS_PER_DAY);
}
/**
* Returns an instance representing exactly one day.
*/
public static function day(): self
{
return self::days(1);
}
/**
* Returns an instance representing the specified number of hours.
*/
public static function hours(int $hours): self
{
return self::fromParts($hours);
}
/**
* Returns an instance representing exactly one hour.
*/
public static function hour(): self
{
return self::hours(1);
}
/**
* Returns an instance representing the specified number of minutes. Due to
* normalization, the actual value in the returned instance may differ from
* the provided one, and the resulting instance may contain larger units.
*
* For example, `Duration::minutes(63)` normalizes to "1 hour(s), 3 minute(s)".
*/
public static function minutes(int $minutes): self
{
return self::fromParts(0, $minutes);
}
/**
* Returns an instance representing exactly one minute.
*/
public static function minute(): self
{
return self::minutes(1);
}
/**
* Returns an instance representing the specified number of seconds. Due to
* normalization, the actual value in the returned instance may differ from
* the provided one, and the resulting instance may contain larger units.
*
* For example, `Duration::seconds(63)` normalizes to "1 minute(s), 3 second(s)".
*/
public static function seconds(int $seconds): self
{
return self::fromParts(0, 0, $seconds);
}
/**
* Returns an instance representing exactly one second.
*/
public static function second(): self
{
return self::seconds(1);
}
/**
* Returns an instance representing the specified number of milliseconds (ms).
* The value is converted and stored as nanoseconds, since that is the only
* unit smaller than a second that we support. Due to normalization, the
* resulting instance may contain larger units.
*
* For example, `Duration::milliseconds(8042)` normalizes to "8 second(s), 42000000 nanosecond(s)".
*/
public static function milliseconds(int $milliseconds): self
{
return self::fromParts(0, 0, 0, NANOSECONDS_PER_MILLISECOND * $milliseconds);
}
/**
* Returns an instance representing the specified number of microseconds (us).
* The value is converted and stored as nanoseconds, since that is the only
* unit smaller than a second that we support. Due to normalization, the
* resulting instance may contain larger units.
*
* For example, `Duration::microseconds(8000042)` normalizes to "8 second(s), 42000 nanosecond(s)".
*
*/
public static function microseconds(int $microseconds): self
{
return self::fromParts(0, 0, 0, NANOSECONDS_PER_MICROSECOND * $microseconds);
}
/**
* Returns an instance representing the specified number of nanoseconds (ns).
* Due to normalization, the resulting instance may contain larger units.
*
* For example, `Duration::nanoseconds(8000000042)` normalizes to "8 second(s), 42 nanosecond(s)".
*
*/
public static function nanoseconds(int $nanoseconds): self
{
return self::fromParts(0, 0, 0, $nanoseconds);
}
/**
* Returns an instance with all parts equal to 0.
*
*/
public static function zero(): self
{
return new self(0, 0, 0, 0);
}
/**
* Compiles and returns the duration's components (hours, minutes, seconds, nanoseconds) in an
* array, in descending order of significance.
*
* @return array{int, int, int, int}
*/
public function getParts(): array
{
return [$this->hours, $this->minutes, $this->seconds, $this->nanoseconds];
}
/**
* Returns the "hours" part of this time duration.
*/
public function getHours(): int
{
return $this->hours;
}
/**
* Returns the "minutes" part of this time duration.
*/
public function getMinutes(): int
{
return $this->minutes;
}
/**
* Returns the "seconds" part of this time duration.
*/
public function getSeconds(): int
{
return $this->seconds;
}
/**
* Returns the "nanoseconds" part of this time duration.
*/
public function getNanoseconds(): int
{
return $this->nanoseconds;
}
/**
* Computes, and returns the total duration of the instance in hours as a floating-point number,
* including any fractional parts.
*/
public function getTotalHours(): float
{
return $this->hours + ($this->minutes / MINUTES_PER_HOUR) + ($this->seconds / SECONDS_PER_HOUR) + ($this->nanoseconds / (SECONDS_PER_HOUR * NANOSECONDS_PER_SECOND));
}
/**
* Computes, and returns the total duration of the instance in minutes as a floating-point number,
* including any fractional parts.
*/
public function getTotalMinutes(): float
{
return ($this->hours * MINUTES_PER_HOUR) + $this->minutes + ($this->seconds / SECONDS_PER_MINUTE) + ($this->nanoseconds / (SECONDS_PER_MINUTE * NANOSECONDS_PER_SECOND));
}
/**
* Computes, and returns the total duration of the instance in seconds as a floating-point number,
* including any fractional parts.
*/
public function getTotalSeconds(): float
{
return $this->seconds + ($this->minutes * SECONDS_PER_MINUTE) + ($this->hours * SECONDS_PER_HOUR) + ($this->nanoseconds / NANOSECONDS_PER_SECOND);
}
/**
* Computes, and returns the total duration of the instance in milliseconds as a floating-point number,
* including any fractional parts.
*/
public function getTotalMilliseconds(): float
{
return (
($this->hours * SECONDS_PER_HOUR * MILLISECONDS_PER_SECOND)
+ ($this->minutes * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND)
+ ($this->seconds * MILLISECONDS_PER_SECOND)
+ ($this->nanoseconds / NANOSECONDS_PER_MILLISECOND)
);
}
/**
* Computes, and returns the total duration of the instance in microseconds as a floating-point number,
* including any fractional parts.
*/
public function getTotalMicroseconds(): float
{
return (
($this->hours * SECONDS_PER_HOUR * MICROSECONDS_PER_SECOND)
+ ($this->minutes * SECONDS_PER_MINUTE * MICROSECONDS_PER_SECOND)
+ ($this->seconds * MICROSECONDS_PER_SECOND)
+ ($this->nanoseconds / NANOSECONDS_PER_MICROSECOND)
);
}
/**
* Determines whether the instance represents a zero duration.
*/
public function isZero(): bool
{
return $this->hours === 0 && $this->minutes === 0 && $this->seconds === 0 && $this->nanoseconds === 0;
}
/**
* Checks if the duration is positive, implying that all non-zero components are positive.
*
* Due to normalization, it is guaranteed that a positive time duration will
* have all of its parts (hours, minutes, seconds, nanoseconds) positive or
* equal to 0.
*
* Note that this method returns false if all parts are equal to 0.
*/
public function isPositive(): bool
{
return $this->hours > 0 || $this->minutes > 0 || $this->seconds > 0 || $this->nanoseconds > 0;
}
/**
* Checks if the duration is negative, implying that all non-zero components are negative.
*
* Due to normalization, it is guaranteed that a negative time duration will
* have all of its parts (hours, minutes, seconds, nanoseconds) negative or
* equal to 0.
*
* Note that this method returns false if all parts are equal to 0.
*/
public function isNegative(): bool
{
return $this->hours < 0 || $this->minutes < 0 || $this->seconds < 0 || $this->nanoseconds < 0;
}
/**
* Returns a new instance with the "hours" part changed to the specified
* value.
*
* Note that due to normalization, the actual value in the returned
* instance may differ, and this may affect other parts of the returned
* instance too.
*
* For example, `Duration::hours(2, 30)->withHours(-1)` is equivalent to
* `Duration::hours(-1, 30)` which normalizes to "-30 minute(s)".
*/
public function withHours(int $hours): self
{
return self::fromParts($hours, $this->minutes, $this->seconds, $this->nanoseconds);
}
/**
* Returns a new instance with the "minutes" part changed to the specified
* value.
*
* Note that due to normalization, the actual value in the returned
* instance may differ, and this may affect other parts of the returned
* instance too.
*
* For example, `Duration::minutes(2, 30)->withMinutes(-1)` is equivalent to
* `Duration::minutes(-1, 30)` which normalizes to "-30 second(s)".
*/
public function withMinutes(int $minutes): self
{
return self::fromParts($this->hours, $minutes, $this->seconds, $this->nanoseconds);
}
/**
* Returns a new instance with the "seconds" part changed to the specified
* value.
*
* Note that due to normalization, the actual value in the returned
* instance may differ, and this may affect other parts of the returned
* instance too.
*
* For example, `Duration::minutes(2, 30)->withSeconds(-30)` is equivalent
* to `Duration::minutes(2, -30)` which normalizes to "1 minute(s), 30 second(s)".
*/
public function withSeconds(int $seconds): self
{
return self::fromParts($this->hours, $this->minutes, $seconds, $this->nanoseconds);
}
/**
* Returns a new instance with the "nanoseconds" part changed to the specified
* value.
*
* Note that due to normalization, the actual value in the returned
* instance may differ, and this may affect other parts of the returned
* instance too.
*
* For example, `Duration::seconds(2)->withNanoseconds(-1)` is equivalent
* to `Duration::seconds(2, -1)` which normalizes to "1 second(s), 999999999 nanosecond(s)".
*/
public function withNanoseconds(int $nanoseconds): self
{
return self::fromParts($this->hours, $this->minutes, $this->seconds, $nanoseconds);
}
/**
* Implements a comparison between this duration and another, based on their duration.
*
* @param Duration $other
*/
#[Override]
public function compare(mixed $other): Order
{
if ($this->hours !== $other->hours) {
return Order::from($this->hours <=> $other->hours);
}
if ($this->minutes !== $other->minutes) {
return Order::from($this->minutes <=> $other->minutes);
}
if ($this->seconds !== $other->seconds) {
return Order::from($this->seconds <=> $other->seconds);
}
return Order::from($this->nanoseconds <=> $other->nanoseconds);
}
/**
* Evaluates whether this duration is equivalent to another, considering all time components.
*
* @param Duration $other
*/
#[Override]
public function equals(mixed $other): bool
{
return $this->compare($other) === Order::EQUAL;
}
/**
* Determines if this duration is shorter than another.
*/
public function shorter(self $other): bool
{
return $this->compare($other) === Order::LESS;
}
/**
* Determines if this duration is shorter than, or equivalent to another.
*/
public function shorterOrEqual(self $other): bool
{
return $this->compare($other) !== Order::GREATER;
}
/**
* Determines if this duration is longer than another.
*/
public function longer(self $other): bool
{
return $this->compare($other) === Order::GREATER;
}
/**
* Determines if this duration is longer than, or equivalent to another.
*/
public function longerOrEqual(self $other): bool
{
return $this->compare($other) !== Order::LESS;
}
/**
* Returns true if this instance represents a time duration longer than $a but
* shorter than $b, or vice-versa (shorter than $a but longer than $b), or if
* this instance is equal to $a and/or $b. Returns false if this instance is
* shorter/longer than both.
*/
public function betweenInclusive(self $a, self $b): bool
{
$ca = $this->compare($a);
$cb = $this->compare($b);
return $ca === Order::EQUAL || $ca !== $cb;
}
/**
* Returns true if this instance represents a time duration longer than $a but
* shorter than $b, or vice-versa (shorter than $a but longer than $b).
* Returns false if this instance is equal to $a and/or $b, or shorter/longer
* than both.
*/
public function betweenExclusive(self $a, self $b): bool
{
$ca = $this->compare($a);
$cb = $this->compare($b);
return $ca !== Order::EQUAL && $cb !== Order::EQUAL && $ca !== $cb;
}
/**
* Returns a new instance, converting a positive/negative duration to the
* opposite (negative/positive) duration of equal length. The resulting
* instance has all parts equivalent to the current instance's parts
* multiplied by -1.
*/
public function invert(): self
{
if ($this->isZero()) {
return $this;
}
return new self(-$this->hours, -$this->minutes, -$this->seconds, -$this->nanoseconds);
}
/**
* Returns a new instance representing the sum of this instance and the
* provided `$other` instance. Note that time duration can be negative, so
* the resulting instance is not guaranteed to be shorter/longer than either
* of the inputs.
*
* This operation is commutative: `$a->plus($b) === $b->plus($a)`
*/
public function plus(self $other): self
{
if ($other->isZero()) {
return $this;
}
if ($this->isZero()) {
return $other;
}
return self::fromParts(
$this->hours + $other->hours,
$this->minutes + $other->minutes,
$this->seconds + $other->seconds,
$this->nanoseconds + $other->nanoseconds,
);
}
/**
* Returns a new instance representing the difference between this instance
* and the provided `$other` instance (i.e. `$other` subtracted from `$this`).
* Note that time duration can be negative, so the resulting instance is not
* guaranteed to be shorter/longer than either of the inputs.
*
* This operation is not commutative: `$a->minus($b) !== $b->minus($a)`
* But: `$a->minus($b) === $b->minus($a)->invert()`
*/
public function minus(self $other): self
{
if ($other->isZero()) {
return $this;
}
if ($this->isZero()) {
return $other->invert();
}
return self::fromParts(
$this->hours - $other->hours,
$this->minutes - $other->minutes,
$this->seconds - $other->seconds,
$this->nanoseconds - $other->nanoseconds,
);
}
/**
* Returns the time duration as string, useful e.g. for debugging. This is not
* meant to be a comprehensive way to format time durations for user-facing
* output.
*
* @param int<0, max> $max_decimals
*/
public function toString(int $max_decimals = 3): string
{
$decimalPart = '';
if ($max_decimals > 0) {
$decimalPart = (string) abs($this->nanoseconds);
$decimalPart = Str\pad_left($decimalPart, 9, '0');
$decimalPart = Str\slice($decimalPart, 0, $max_decimals);
$decimalPart = mb_rtrim($decimalPart, '0');
}
if ($decimalPart !== '') {
$decimalPart = '.' . $decimalPart;
}
$secSign = $this->seconds < 0 || $this->nanoseconds < 0 ? '-' : '';
$sec = abs($this->seconds);
$containsHours = $this->hours !== 0;
$containsMinutes = $this->minutes !== 0;
$concatenatedSeconds = $secSign . $sec . $decimalPart;
$containsSeconds = $concatenatedSeconds !== '0';
/** @var list<string> $output */
$output = [];
if ($containsHours) {
$output[] = $this->hours . ' hour(s)';
}
if ($containsMinutes || $containsHours && $containsSeconds) {
$output[] = $this->minutes . ' minute(s)';
}
if ($containsSeconds) {
$output[] = $concatenatedSeconds . ' second(s)';
}
return [] === $output ? '0 second(s)' : implode(', ', $output);
}
/**
* Returns a string representation of the time duration.
*/
#[Override]
public function __toString(): string
{
return $this->toString();
}
/**
* Returns data which can be serialized by json_encode().
*
* @return array{hours: int, minutes: int, seconds: int, nanoseconds: int}
*/
#[Override]
public function jsonSerialize(): array
{
return [
'hours' => $this->hours,
'minutes' => $this->minutes,
'seconds' => $this->seconds,
'nanoseconds' => $this->nanoseconds,
];
}
}