Skip to content

Commit 92e5b6d

Browse files
committed
Update YAML to process timestamp string timezone
Parse date strings containing timezone data correctly Default date strings not containing timezone data to UTC
1 parent a43ccb1 commit 92e5b6d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,18 @@ private static function evaluateScalar($scalar, $flags, $references = array())
604604
return self::evaluateBinaryScalar(substr($scalar, 9));
605605
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
606606
return (float) str_replace(',', '', $scalar);
607-
case preg_match(self::getTimestampRegex(), $scalar):
607+
case preg_match(self::getTimestampRegex(), $scalar, $datetimeMatches):
608608
if (Yaml::PARSE_DATETIME & $flags) {
609-
$date = new \DateTime($scalar);
610-
$date->setTimeZone(new \DateTimeZone('UTC'));
611609

610+
// Timestamps that have timezone data should be parsed in the specified timezone first, then
611+
// converted to UTC; if no tz data is supplied, parse as UTC date initially
612+
if (isset($datetimeMatches['tz'])) {
613+
$date = new \DateTime($scalar);
614+
$date->setTimeZone(new \DateTimeZone('UTC'));
615+
return $date;
616+
}
617+
618+
$date = new \DateTime($scalar, new \DateTimeZone('UTC'));
612619
return $date;
613620
}
614621

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,11 @@ public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $mont
535535
$expected = new \DateTime($yaml);
536536
$expected->setTimeZone(new \DateTimeZone('UTC'));
537537
$expected->setDate($year, $month, $day);
538-
@$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
538+
if (PHP_VERSION_ID >= 70100) {
539+
$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
540+
} else {
541+
$expected->setTime($hour, $minute, $second);
542+
}
539543

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

0 commit comments

Comments
 (0)