Skip to content

Fix for incorrect timestamp parsing when authored timestamp has a Z offset #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ public function parseDT(\DOMElement $dt, &$dates = array(), &$impliedTimezone =

$dtValue = unicodeTrim($dtValue);

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

Expand Down
36 changes: 36 additions & 0 deletions tests/Mf2/ParseDTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ public function testParseDTHandlesTimeDatetimeAttr() {
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}

/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithZ() {
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00Z"></div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00Z', $output['items'][0]['properties']['start'][0]);
}

/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset() {
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00-0700"></div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00-0700', $output['items'][0]['properties']['start'][0]);
}

/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset2() {
$input = '<div class="h-card"><time class="dt-start" datetime="2012-08-05T14:50:00-07:00"></div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00-07:00', $output['items'][0]['properties']['start'][0]);
}

/**
* @group parseDT
*/
Expand Down