Skip to content

Commit

Permalink
[YAML] Fix processing timestamp with timezone
Browse files Browse the repository at this point in the history
Parse date strings containing timezone data correctly
Default date strings not containing timezone data to UTC
  • Loading branch information
noahheck committed Nov 17, 2016
1 parent a43ccb1 commit 1d050d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -604,10 +604,19 @@ private static function evaluateScalar($scalar, $flags, $references = array())
return self::evaluateBinaryScalar(substr($scalar, 9));
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
return (float) str_replace(',', '', $scalar);
case preg_match(self::getTimestampRegex(), $scalar):
case preg_match(self::getTimestampRegex(), $scalar, $datetimeMatches):
if (Yaml::PARSE_DATETIME & $flags) {
$date = new \DateTime($scalar);
$date->setTimeZone(new \DateTimeZone('UTC'));

// Timestamps that have timezone data should be parsed in the specified timezone first, then
// converted to UTC; if no tz data is supplied, parse as UTC date initially
if (isset($datetimeMatches['tz'])) {
$date = new \DateTime($scalar);
$date->setTimeZone(new \DateTimeZone('UTC'));

return $date;
}

$date = new \DateTime($scalar, new \DateTimeZone('UTC'));

return $date;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Yaml/Tests/InlineTest.php
Expand Up @@ -535,7 +535,11 @@ public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $mont
$expected = new \DateTime($yaml);
$expected->setTimeZone(new \DateTimeZone('UTC'));
$expected->setDate($year, $month, $day);
@$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
if (PHP_VERSION_ID >= 70100) {
$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
} else {
$expected->setTime($hour, $minute, $second);
}

$expectedNested = array('nested' => array($expected));
$yamlNested = "{nested: [$yaml]}";
Expand Down

0 comments on commit 1d050d5

Please sign in to comment.