Skip to content
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

Add test and fix for #149 #150

Merged
merged 2 commits into from
Mar 13, 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
15 changes: 8 additions & 7 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ function convertTimeFormat($time) {
* @return string isolated, normalized TZ offset for implied TZ for other dt- properties
*/
function normalizeTimezoneOffset(&$dtValue) {
preg_match('/Z|[+-]\d{1,2}:?(\d{2})?$/i', $dtValue, $matches);
preg_match('/Z|[+-]\d{1,2}:?(\d{2})?$/i', $dtValue, $matches);

if (empty($matches)) {
return null;
}

$timezoneOffset = null;
$timezoneOffset = null;

if ( $matches[0] != 'Z' ) {
$timezoneString = str_replace(':', '', $matches[0]);
Expand Down Expand Up @@ -845,11 +845,12 @@ public function parseDT(\DOMElement $dt, &$dates = array(), &$impliedTimezone =
$dtValue = $this->textContent($dt);
}

// if the dtValue is not just YYYY-MM-DD, normalize the timezone offset
// if the dtValue is not just YYYY-MM-DD
if (!preg_match('/^(\d{4}-\d{2}-\d{2})$/', $dtValue)) {
$timezoneOffset = normalizeTimezoneOffset($dtValue);
if (!$impliedTimezone && $timezoneOffset) {
$impliedTimezone = $timezoneOffset;
// no implied timezone set and dtValue has a TZ offset, use un-normalized TZ offset
preg_match('/Z|[+-]\d{1,2}:?(\d{2})?$/i', $dtValue, $matches);
if (!$impliedTimezone && !empty($matches[0])) {
$impliedTimezone = $matches[0];
}
}

Expand Down Expand Up @@ -1031,7 +1032,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('/[+-]\d{2}:?(\d{2})?$/i', $dtValue, $matches) == 0 ) {
$dtValue .= $impliedTimezone;
}

Expand Down
32 changes: 29 additions & 3 deletions tests/Mf2/ParseDTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ public function testDTStartDateOnly() {
}

/**
*
* TZ offsets normalized only for VCP.
* This behavior is implied from "However the colons ":" separating the hours and minutes of any timezone offset are optional and discouraged in order to make it less likely that a timezone offset will be confused for a time."
* @see http://microformats.org/wiki/index.php?title=value-class-pattern&oldid=66473##However+the+colons
*/
public function testNormalizeTZOffset() {
public function testNormalizeTZOffsetVCP() {
$input = '<div class="h-event">
<span class="dt-start"> <time class="value" datetime="2017-05-27">May 27</time>, from
<time class="value">20:57-07:00</time> </span>
Expand All @@ -273,6 +275,19 @@ public function testNormalizeTZOffset() {
$this->assertEquals('2017-05-27 20:57-0700', $output['items'][0]['properties']['start'][0]);
}


/**
* TZ offsets *not* normalized for non-VCP dates
*/
public function testNoNormalizeTZOffset() {
$input = '<div class="h-entry"> <time class="dt-start" datetime="2018-03-13 15:30-07:00">March 13, 2018 3:30PM</time> </div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertEquals('2018-03-13 15:30-07:00', $output['items'][0]['properties']['start'][0]);
}


/**
* @see https://github.com/indieweb/php-mf2/issues/115
*/
Expand All @@ -297,7 +312,7 @@ public function testPreserrveTIfAuthored() {
$parser = new Parser($input);
$output = $parser->parse();

$this->assertEquals('2009-06-26T19:01-0800', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2009-06-26T19:01-08:00', $output['items'][0]['properties']['start'][0]);
}

/**
Expand Down Expand Up @@ -469,5 +484,16 @@ public function testDtVCPMultipleDatesAndTimezones() {
$this->assertEquals('2014-06-01 19:30-0600', $output['items'][0]['properties']['end'][0]);
}

/**
* @see https://github.com/indieweb/php-mf2/issues/149
*/
public function testDtWithoutYear() {
$input = '<div class="h-card"> <time class="dt-bday" datetime="--12-28"></time> </div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertEquals('--12-28', $output['items'][0]['properties']['bday'][0]);
}

}