Skip to content

Commit 763dd5d

Browse files
authored
Merge pull request #150 from gRegorLove/issue149
Add test and fix for #149
2 parents acd29b8 + 1a3c2f0 commit 763dd5d

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Mf2/Parser.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ function convertTimeFormat($time) {
244244
* @return string isolated, normalized TZ offset for implied TZ for other dt- properties
245245
*/
246246
function normalizeTimezoneOffset(&$dtValue) {
247-
preg_match('/Z|[+-]\d{1,2}:?(\d{2})?$/i', $dtValue, $matches);
247+
preg_match('/Z|[+-]\d{1,2}:?(\d{2})?$/i', $dtValue, $matches);
248248

249249
if (empty($matches)) {
250250
return null;
251251
}
252252

253-
$timezoneOffset = null;
253+
$timezoneOffset = null;
254254

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

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

@@ -1031,7 +1032,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10311032
foreach ($temp_dates as $propName => $data) {
10321033
foreach ( $data as $dtValue ) {
10331034
// var_dump(preg_match('/[+-]\d{2}(\d{2})?$/i', $dtValue));
1034-
if ( $impliedTimezone && preg_match('/[+-]\d{2}(\d{2})?$/i', $dtValue, $matches) == 0 ) {
1035+
if ( $impliedTimezone && preg_match('/[+-]\d{2}:?(\d{2})?$/i', $dtValue, $matches) == 0 ) {
10351036
$dtValue .= $impliedTimezone;
10361037
}
10371038

tests/Mf2/ParseDTTest.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ public function testDTStartDateOnly() {
260260
}
261261

262262
/**
263-
*
263+
* TZ offsets normalized only for VCP.
264+
* 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."
265+
* @see http://microformats.org/wiki/index.php?title=value-class-pattern&oldid=66473##However+the+colons
264266
*/
265-
public function testNormalizeTZOffset() {
267+
public function testNormalizeTZOffsetVCP() {
266268
$input = '<div class="h-event">
267269
<span class="dt-start"> <time class="value" datetime="2017-05-27">May 27</time>, from
268270
<time class="value">20:57-07:00</time> </span>
@@ -273,6 +275,19 @@ public function testNormalizeTZOffset() {
273275
$this->assertEquals('2017-05-27 20:57-0700', $output['items'][0]['properties']['start'][0]);
274276
}
275277

278+
279+
/**
280+
* TZ offsets *not* normalized for non-VCP dates
281+
*/
282+
public function testNoNormalizeTZOffset() {
283+
$input = '<div class="h-entry"> <time class="dt-start" datetime="2018-03-13 15:30-07:00">March 13, 2018 3:30PM</time> </div>';
284+
$parser = new Parser($input);
285+
$output = $parser->parse();
286+
287+
$this->assertEquals('2018-03-13 15:30-07:00', $output['items'][0]['properties']['start'][0]);
288+
}
289+
290+
276291
/**
277292
* @see https://github.com/indieweb/php-mf2/issues/115
278293
*/
@@ -297,7 +312,7 @@ public function testPreserrveTIfAuthored() {
297312
$parser = new Parser($input);
298313
$output = $parser->parse();
299314

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

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

487+
/**
488+
* @see https://github.com/indieweb/php-mf2/issues/149
489+
*/
490+
public function testDtWithoutYear() {
491+
$input = '<div class="h-card"> <time class="dt-bday" datetime="--12-28"></time> </div>';
492+
$parser = new Parser($input);
493+
$output = $parser->parse();
494+
495+
$this->assertEquals('--12-28', $output['items'][0]['properties']['bday'][0]);
496+
}
497+
472498
}
473499

0 commit comments

Comments
 (0)