|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Esi\Clock. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Eric Sizemore <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This source file is subject to the MIT license. For the full copyright and |
|
11
|
|
|
* license information, please view the LICENSE file that was distributed with |
|
12
|
|
|
* this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Esi\Clock; |
|
16
|
|
|
|
|
17
|
|
|
use DateInvalidTimeZoneException; |
|
18
|
|
|
use DateTimeImmutable; |
|
19
|
|
|
use DateTimeInterface; |
|
20
|
|
|
use DateTimeZone; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
|
|
23
|
|
|
use function date_default_timezone_get; |
|
24
|
|
|
use function sprintf; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A clock that relies on system time. |
|
28
|
|
|
* |
|
29
|
|
|
* @immutable |
|
30
|
|
|
*/ |
|
31
|
|
|
final readonly class SystemClock implements ClockInterface |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
private DateTimeZone $timezone; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws DateInvalidTimeZoneException If $timezone is passed as string and is invalid. |
|
37
|
|
|
*/ |
|
38
|
9 |
|
public function __construct(DateTimeZone | string | null $timezone = null) |
|
39
|
|
|
{ |
|
40
|
9 |
|
if (!$timezone instanceof DateTimeZone) { |
|
41
|
4 |
|
$timezone ??= 'UTC'; |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
4 |
|
$this->timezone = new DateTimeZone($timezone === '' ? 'UTC' : $timezone); |
|
45
|
|
|
// \Exception < PHP 8.3, \DateInvalidTimeZoneException >= PHP 8.3 |
|
46
|
1 |
|
} catch (Throwable $throwable) { |
|
47
|
1 |
|
throw new DateInvalidTimeZoneException($throwable->getMessage(), \intval($throwable->getCode()), $throwable); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
$this->timezone = $timezone; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
1 |
|
return sprintf( |
|
62
|
1 |
|
'[SystemClock("%s"): unixtime: %s; iso8601: %s;]', |
|
63
|
1 |
|
$this->timezone->getName(), |
|
64
|
1 |
|
$this->now()->format('U'), |
|
65
|
1 |
|
$this->now()->format(DateTimeInterface::ISO8601_EXPANDED) |
|
66
|
1 |
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create a FrozenClock from the current SystemClock now. |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function freeze(): FrozenClock |
|
73
|
|
|
{ |
|
74
|
1 |
|
return new FrozenClock($this->now()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
8 |
|
public function now(): DateTimeImmutable |
|
81
|
|
|
{ |
|
82
|
8 |
|
return new DateTimeImmutable('now', $this->timezone); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns a new SystemClock at current system time using the system's timezone. |
|
87
|
|
|
* |
|
88
|
|
|
* @throws DateInvalidTimeZoneException |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public static function fromSystemTimezone(): SystemClock |
|
91
|
|
|
{ |
|
92
|
1 |
|
return new SystemClock(new DateTimeZone(date_default_timezone_get())); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public static function fromUtc(): SystemClock |
|
99
|
|
|
{ |
|
100
|
1 |
|
return new SystemClock(new DateTimeZone('UTC')); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|