|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Type; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class for all types representing a point in time. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class BaseTime extends Element implements TimeType |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* UTC timezone. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
const TZ_UTC = 'UTC'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Date and time. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \DateTimeImmutable |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_dateTime; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \DateTimeImmutable $dt |
|
32
|
|
|
*/ |
|
33
|
25 |
|
public function __construct(\DateTimeImmutable $dt) |
|
34
|
|
|
{ |
|
35
|
25 |
|
$this->_dateTime = $dt; |
|
36
|
25 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function __toString(): string |
|
42
|
|
|
{ |
|
43
|
2 |
|
return $this->string(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Initialize from datetime string. |
|
48
|
|
|
* |
|
49
|
|
|
* @see http://php.net/manual/en/datetime.formats.php |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $time Time string |
|
52
|
|
|
* @param null|string $tz timezone, if null use default |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \RuntimeException |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
6 |
|
public static function fromString(string $time, ?string $tz = null): self |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
6 |
|
if (!isset($tz)) { |
|
62
|
4 |
|
$tz = date_default_timezone_get(); |
|
63
|
|
|
} |
|
64
|
6 |
|
return new static( |
|
65
|
6 |
|
new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
66
|
2 |
|
} catch (\Exception $e) { |
|
67
|
2 |
|
throw new \RuntimeException( |
|
68
|
|
|
'Failed to create DateTime: ' . |
|
69
|
2 |
|
self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the date and time. |
|
75
|
|
|
* |
|
76
|
|
|
* @return \DateTimeImmutable |
|
77
|
|
|
*/ |
|
78
|
7 |
|
public function dateTime(): \DateTimeImmutable |
|
79
|
|
|
{ |
|
80
|
7 |
|
return $this->_dateTime; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the date and time as a type specific string. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
5 |
|
public function string(): string |
|
89
|
|
|
{ |
|
90
|
5 |
|
return $this->_encodedContentDER(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Create `DateTimeZone` object from string. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $tz |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \UnexpectedValueException If timezone is invalid |
|
99
|
|
|
* |
|
100
|
|
|
* @return \DateTimeZone |
|
101
|
|
|
*/ |
|
102
|
31 |
|
protected static function _createTimeZone(string $tz): \DateTimeZone |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
31 |
|
return new \DateTimeZone($tz); |
|
106
|
1 |
|
} catch (\Exception $e) { |
|
107
|
1 |
|
throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get last error caused by `DateTimeImmutable`. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
3 |
|
protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
117
|
|
|
{ |
|
118
|
3 |
|
$errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
119
|
3 |
|
return implode(', ', $errors); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|