Skip to content

Commit b5fbe4f

Browse files
committed
feature #47730 Ban DateTime from the codebase (WebMamba)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- Ban DateTime from the codebase | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | #47580 | License | MIT | Doc PR | symfony As discuss in this issue, the purpose of this PR is to remove DateTime from the code base in favor to DateTimeImmutable. I will process it component by component. Feel free to discuss! Commits ------- 689385a83f Ban DateTime from the codebase
2 parents a2eda75 + 75e5ec8 commit b5fbe4f

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
699699
return (float) str_replace('_', '', $scalar);
700700
case Parser::preg_match(self::getTimestampRegex(), $scalar):
701701
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
702-
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
702+
$time = new \DateTimeImmutable($scalar, new \DateTimeZone('UTC'));
703703

704704
if (Yaml::PARSE_DATETIME & $flags) {
705705
return $time;

Tests/InlineTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,10 @@ public function testParseTimestampAsUnixTimestampByDefault(string $yaml, int $ye
573573
*/
574574
public function testParseTimestampAsDateTimeObject(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond, string $timezone)
575575
{
576-
$expected = new \DateTime($yaml);
577-
$expected->setTimeZone(new \DateTimeZone('UTC'));
578-
$expected->setDate($year, $month, $day);
579-
$expected->setTime($hour, $minute, $second, $microsecond);
576+
$expected = (new \DateTime($yaml))
577+
->setTimeZone(new \DateTimeZone('UTC'))
578+
->setDate($year, $month, $day)
579+
->setTime($hour, $minute, $second, $microsecond);
580580

581581
$date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
582582
$this->assertEquals($expected, $date);
@@ -598,10 +598,10 @@ public function getTimestampTests(): array
598598
*/
599599
public function testParseNestedTimestampListAsDateTimeObject(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond)
600600
{
601-
$expected = new \DateTime($yaml);
602-
$expected->setTimeZone(new \DateTimeZone('UTC'));
603-
$expected->setDate($year, $month, $day);
604-
$expected->setTime($hour, $minute, $second, $microsecond);
601+
$expected = (new \DateTime($yaml))
602+
->setTimeZone(new \DateTimeZone('UTC'))
603+
->setDate($year, $month, $day)
604+
->setTime($hour, $minute, $second, $microsecond);
605605

606606
$expectedNested = ['nested' => [$expected]];
607607
$yamlNested = "{nested: [$yaml]}";
@@ -636,7 +636,7 @@ public function getDateTimeDumpTests()
636636
{
637637
$tests = [];
638638

639-
$dateTime = new \DateTime('2001-12-15 21:59:43', new \DateTimeZone('UTC'));
639+
$dateTime = new \DateTimeImmutable('2001-12-15 21:59:43', new \DateTimeZone('UTC'));
640640
$tests['date-time-utc'] = [$dateTime, '2001-12-15T21:59:43+00:00'];
641641

642642
$dateTime = new \DateTimeImmutable('2001-07-15 21:59:43', new \DateTimeZone('Europe/Berlin'));

Tests/ParserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,10 +1546,10 @@ public function testParseDateAsMappingValue()
15461546
$yaml = <<<'EOT'
15471547
date: 2002-12-14
15481548
EOT;
1549-
$expectedDate = new \DateTime();
1550-
$expectedDate->setTimeZone(new \DateTimeZone('UTC'));
1551-
$expectedDate->setDate(2002, 12, 14);
1552-
$expectedDate->setTime(0, 0, 0);
1549+
$expectedDate = (new \DateTimeImmutable())
1550+
->setTimeZone(new \DateTimeZone('UTC'))
1551+
->setDate(2002, 12, 14)
1552+
->setTime(0, 0, 0);
15531553

15541554
$this->assertSameData(['date' => $expectedDate], $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
15551555
}

0 commit comments

Comments
 (0)