generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Time.php
91 lines (75 loc) · 2 KB
/
Time.php
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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/
namespace Ergebnis\PHPUnit\SlowTestDetector;
/**
* @internal
*/
final class Time
{
private function __construct(
private readonly int $seconds,
private readonly int $nanoseconds,
) {
}
/**
* @throws Exception\InvalidNanoseconds
* @throws Exception\InvalidSeconds
*/
public static function fromSecondsAndNanoseconds(
int $seconds,
int $nanoseconds,
): self {
if (0 > $seconds) {
throw Exception\InvalidSeconds::notGreaterThanOrEqualToZero($seconds);
}
if (0 > $nanoseconds) {
throw Exception\InvalidNanoseconds::notGreaterThanOrEqualToZero($nanoseconds);
}
$maxNanoseconds = 999_999_999;
if ($maxNanoseconds < $nanoseconds) {
throw Exception\InvalidNanoseconds::notLessThanOrEqualTo(
$nanoseconds,
$maxNanoseconds,
);
}
return new self(
$seconds,
$nanoseconds,
);
}
public function seconds(): int
{
return $this->seconds;
}
public function nanoseconds(): int
{
return $this->nanoseconds;
}
/**
* @throws Exception\InvalidStart
*/
public function duration(self $start): Duration
{
$seconds = $this->seconds - $start->seconds;
$nanoseconds = $this->nanoseconds - $start->nanoseconds;
if (0 > $nanoseconds) {
--$seconds;
$nanoseconds += 1_000_000_000;
}
if (0 > $seconds) {
throw Exception\InvalidStart::notLessThanOrEqualToEnd();
}
return Duration::fromSecondsAndNanoseconds(
$seconds,
$nanoseconds,
);
}
}