|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>. |
|
4
|
|
|
* Copyright (c) 2017–2019 Contributors. |
|
5
|
|
|
* |
|
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SimplePie\Parser; |
|
12
|
|
|
|
|
13
|
|
|
use DateTime; |
|
14
|
|
|
use DateTimeZone; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The core parser for all Date content. |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#33-date-constructs |
|
20
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#429-the-atompublished-element |
|
21
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#4215-the-atomupdated-element |
|
22
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#elements-of-item |
|
23
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#items |
|
24
|
|
|
*/ |
|
25
|
|
|
class Date |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The input datestamp. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $datestamp; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The desired output timezone. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string|null |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $outputTimezone; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The format used to assist date string parsing. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string|null |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $createFromFormat; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The resulting `DateTime` object. |
|
50
|
|
|
* |
|
51
|
|
|
* @var bool|DateTime |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $dateTime; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Constructs a new instance of this class. |
|
57
|
|
|
* |
|
58
|
|
|
* Timezone calculation is performed on a _best-effort_ basis and is not guaranteed. Factors which may affect the |
|
59
|
|
|
* calculation include: |
|
60
|
|
|
* |
|
61
|
|
|
* * the version of glibc/musl that your OS relies on. |
|
62
|
|
|
* * the freshness of the timestamp data your OS relies on. |
|
63
|
|
|
* * the format of the datestamp inside of the feed and PHP's ability to parse it. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string|null $datestamp The datestamp to handle, as a string. The default value is `null`. |
|
66
|
|
|
* @param string|null $outputTimezone The timezone identifier to use. Must be compatible with `DateTimeZone`. |
|
67
|
|
|
* The default value is `UTC`. |
|
68
|
|
|
* @param string|null $createFromFormat Allows the user to assist the date parser by providing the input format of |
|
69
|
|
|
* the datestamp. This will be passed into `DateTime::createFromFormat()` |
|
70
|
|
|
* at parse-time. |
|
71
|
|
|
* |
|
72
|
|
|
* @see http://php.net/manual/en/datetime.createfromformat.php |
|
73
|
|
|
* |
|
74
|
|
|
* @phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine |
|
75
|
|
|
*/ |
|
76
|
6 |
|
public function __construct( |
|
77
|
|
|
?string $datestamp = null, |
|
78
|
|
|
?string $outputTimezone = 'UTC', |
|
79
|
|
|
?string $createFromFormat = null |
|
80
|
|
|
) { |
|
81
|
|
|
// @phpcs:enable |
|
82
|
|
|
|
|
83
|
6 |
|
$this->datestamp = $datestamp ?? null; |
|
84
|
6 |
|
$this->createFromFormat = $createFromFormat; |
|
85
|
|
|
|
|
86
|
|
|
// Convert null or `Z` to UTC. |
|
87
|
6 |
|
if (null === $outputTimezone) { |
|
88
|
5 |
|
$this->outputTimezone = 'UTC'; |
|
89
|
1 |
|
} elseif ('Z' === \mb_strtoupper($outputTimezone)) { |
|
90
|
|
|
$this->outputTimezone = 'UTC'; |
|
91
|
|
|
} else { |
|
92
|
1 |
|
$this->outputTimezone = $outputTimezone; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
if (null !== $this->datestamp) { |
|
96
|
|
|
// Use the custom formatter, if available |
|
97
|
6 |
|
if (null !== $this->createFromFormat) { |
|
98
|
1 |
|
$this->dateTime = DateTime::createFromFormat( |
|
99
|
1 |
|
$this->createFromFormat, |
|
100
|
1 |
|
$this->datestamp, |
|
101
|
1 |
|
new DateTimeZone($this->outputTimezone) |
|
102
|
|
|
); |
|
103
|
|
|
} else { |
|
104
|
5 |
|
$this->dateTime = new DateTime( |
|
105
|
5 |
|
$this->datestamp, |
|
106
|
5 |
|
new DateTimeZone($this->outputTimezone) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Sometimes, `createFromFormat()` doesn't set this correctly. |
|
111
|
6 |
|
if (false !== $this->dateTime) { |
|
112
|
6 |
|
$this->dateTime->setTimezone(new DateTimeZone($this->outputTimezone)); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
6 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the input datestamp. |
|
119
|
|
|
*/ |
|
120
|
9 |
|
public function getDatestamp(): string |
|
121
|
|
|
{ |
|
122
|
9 |
|
return $this->datestamp; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the requested output timezone. |
|
127
|
|
|
*/ |
|
128
|
9 |
|
public function getOutputTimezone(): string |
|
129
|
|
|
{ |
|
130
|
9 |
|
return $this->outputTimezone; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the format used to assist date string parsing. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getCreateFromFormat(): ?string |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->createFromFormat; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the resulting `DateTime` object. If the date string could not be parsed, `false` will be returned. |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool|DateTime |
|
145
|
|
|
*/ |
|
146
|
15 |
|
public function getDateTime(): ?DateTime |
|
147
|
|
|
{ |
|
148
|
15 |
|
return $this->dateTime; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|