Skip to content

Commit 5a98ead

Browse files
authored
Merge pull request #154 from aaronpk/master
Fix for incorrect timestamp parsing when authored timestamp has a Z offset
2 parents 8a7db43 + 9cdf579 commit 5a98ead

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Mf2/Parser.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ public function parseDT(\DOMElement $dt, &$dates = array(), &$impliedTimezone =
856856

857857
$dtValue = unicodeTrim($dtValue);
858858

859+
// Store the date part so that we can use it when assembling the final timestamp if the next one is missing a date part
859860
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $dtValue, $matches)) {
860861
$dates[] = $matches[0];
861862
}
@@ -1032,7 +1033,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10321033
foreach ($temp_dates as $propName => $data) {
10331034
foreach ( $data as $dtValue ) {
10341035
// var_dump(preg_match('/[+-]\d{2}(\d{2})?$/i', $dtValue));
1035-
if ( $impliedTimezone && preg_match('/[+-]\d{2}:?(\d{2})?$/i', $dtValue, $matches) == 0 ) {
1036+
if ( $impliedTimezone && preg_match('/(Z|[+-]\d{2}:?(\d{2})?)$/i', $dtValue, $matches) == 0 ) {
10361037
$dtValue .= $impliedTimezone;
10371038
}
10381039

tests/Mf2/ParseDTTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ public function testParseDTHandlesTimeDatetimeAttr() {
9090
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
9191
}
9292

93+
/**
94+
* @group parseDT
95+
*/
96+
public function testParseDTHandlesTimeDatetimeAttrWithZ() {
97+
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00Z"></div>';
98+
$parser = new Parser($input);
99+
$output = $parser->parse();
100+
101+
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
102+
$this->assertEquals('2012-08-05T14:50:00Z', $output['items'][0]['properties']['start'][0]);
103+
}
104+
105+
/**
106+
* @group parseDT
107+
*/
108+
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset() {
109+
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00-0700"></div>';
110+
$parser = new Parser($input);
111+
$output = $parser->parse();
112+
113+
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
114+
$this->assertEquals('2012-08-05T14:50:00-0700', $output['items'][0]['properties']['start'][0]);
115+
}
116+
117+
/**
118+
* @group parseDT
119+
*/
120+
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset2() {
121+
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00-07:00"></div>';
122+
$parser = new Parser($input);
123+
$output = $parser->parse();
124+
125+
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
126+
$this->assertEquals('2012-08-05T14:50:00-07:00', $output['items'][0]['properties']['start'][0]);
127+
}
128+
93129
/**
94130
* @group parseDT
95131
*/

0 commit comments

Comments
 (0)