Skip to content

Commit

Permalink
feature #47730 Ban DateTime from the codebase (WebMamba)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Oct 9, 2022
2 parents a2eda75 + 75e5ec8 commit b5fbe4f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
return (float) str_replace('_', '', $scalar);
case Parser::preg_match(self::getTimestampRegex(), $scalar):
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
$time = new \DateTimeImmutable($scalar, new \DateTimeZone('UTC'));

if (Yaml::PARSE_DATETIME & $flags) {
return $time;
Expand Down
18 changes: 9 additions & 9 deletions Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,10 @@ public function testParseTimestampAsUnixTimestampByDefault(string $yaml, int $ye
*/
public function testParseTimestampAsDateTimeObject(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond, string $timezone)
{
$expected = new \DateTime($yaml);
$expected->setTimeZone(new \DateTimeZone('UTC'));
$expected->setDate($year, $month, $day);
$expected->setTime($hour, $minute, $second, $microsecond);
$expected = (new \DateTime($yaml))
->setTimeZone(new \DateTimeZone('UTC'))
->setDate($year, $month, $day)
->setTime($hour, $minute, $second, $microsecond);

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

$expectedNested = ['nested' => [$expected]];
$yamlNested = "{nested: [$yaml]}";
Expand Down Expand Up @@ -636,7 +636,7 @@ public function getDateTimeDumpTests()
{
$tests = [];

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

$dateTime = new \DateTimeImmutable('2001-07-15 21:59:43', new \DateTimeZone('Europe/Berlin'));
Expand Down
8 changes: 4 additions & 4 deletions Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1546,10 +1546,10 @@ public function testParseDateAsMappingValue()
$yaml = <<<'EOT'
date: 2002-12-14
EOT;
$expectedDate = new \DateTime();
$expectedDate->setTimeZone(new \DateTimeZone('UTC'));
$expectedDate->setDate(2002, 12, 14);
$expectedDate->setTime(0, 0, 0);
$expectedDate = (new \DateTimeImmutable())
->setTimeZone(new \DateTimeZone('UTC'))
->setDate(2002, 12, 14)
->setTime(0, 0, 0);

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

0 comments on commit b5fbe4f

Please sign in to comment.