8000 bug #20550 [YAML] Fix processing timestamp strings with timezone (mye… · src-run/symfony@c01a1a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c01a1a9

Browse files
bug symfony#20550 [YAML] Fix processing timestamp strings with timezone (myesain)
This PR was merged into the 3.1 branch. Discussion ---------- [YAML] Fix processing timestamp strings with timezone | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#20399 | License | MIT | Doc PR | - Parse date strings containing timezone data correctly Default date strings not containing timezone data to UTC Commits ------- cdb11a7 [YAML] Fix processing timestamp with timezone
2 parents 2e6618b + cdb11a7 commit c01a1a9

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
606606
return (float) str_replace(',', '', $scalar);
607607
case preg_match(self::getTimestampRegex(), $scalar):
608608
if (Yaml::PARSE_DATETIME & $flags) {
609-
$date = new \DateTime($scalar);
610-
$date->setTimeZone(new \DateTimeZone('UTC'));
611-
612-
return $date;
609+
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
610+
return new \DateTime($scalar, new \DateTimeZone('UTC'));
613611
}
614612

615613
$timeZone = date_default_timezone_get();

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public function testParseTimestampAsUnixTimestampByDefault($yaml, $year, $month,
502502
/**
503503
* @dataProvider getTimestampTests
504504
*/
505-
public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second)
505+
public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second, $timezone)
506506
{
507507
$expected = new \DateTime($yaml);
508508
$expected->setTimeZone(new \DateTimeZone('UTC'));
@@ -514,16 +514,18 @@ public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $
514514
$expected->setTime($hour, $minute, $second);
515515
}
516516

517-
$this->assertEquals($expected, Inline::parse($yaml, Yaml::PARSE_DATETIME));
517+
$date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
518+
$this->assertEquals($expected, $date);
519+
$this->assertSame($timezone, $date->format('O'));
518520
}
519521

520522
public function getTimestampTests()
521523
{
522524
return array(
523-
'canonical' => array('2001-12-15T02:59:43.1Z', 2001, 12, 15, 2, 59, 43.1),
524-
'ISO-8601' => array('2001-12-15t21:59:43.10-05:00', 2001, 12, 16, 2, 59, 43.1),
525-
'spaced' => array('2001-12-15 21:59:43.10 -5', 2001, 12, 16, 2, 59, 43.1),
526-
'date' => array('2001-12-15', 2001, 12, 15, 0, 0, 0),
525+
'canonical' => array('2001-12-15T02:59:43.1Z', 2001, 12, 15, 2, 59, 43.1, '+0000'),
526+
'ISO-8601' => array('2001-12-15t21:59:43.10-05:00', 2001, 12, 16, 2, 59, 43.1, '-0500'),
527+
'spaced' => array('2001-12-15 21:59:43.10 -5', 2001, 12, 16, 2, 59, 43.1, '-0500'),
528+
'date' => array('2001-12-15', 2001, 12, 15, 0, 0, 0, '+0000'),
527529
);
528530
}
529531

@@ -535,7 +537,11 @@ public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $mont
535537
$expected = new \DateTime($yaml);
536538
$expected->setTimeZone(new \DateTimeZone('UTC'));
537539
$expected->setDate($year, $month, $day);
538-
@$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
540+
if (PHP_VERSION_ID >= 70100) {
541+
$expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
542+
} else {
543+
$expected->setTime($hour, $minute, $second);
544+
}
539545

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

0 commit comments

Comments
 (0)
0