From b06b81809709acf62abefe3805bc907ba06fd58e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 14 Feb 2023 14:19:26 +0100 Subject: [PATCH] [Yaml] Fix parsing sub-second dates on x86 --- src/Symfony/Component/Yaml/CHANGELOG.md | 2 +- src/Symfony/Component/Yaml/Inline.php | 36 ++++++++----------- .../Component/Yaml/Tests/InlineTest.php | 2 +- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index beb6c3ccd6255..0c2021f48b2ef 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 6.3 --- - * Add support to dump int keys as strings by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag. + * Add support to dump int keys as strings by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag 6.2 --- diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 586ae2ce86a4b..c2a93bab6c5da 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -110,16 +110,11 @@ public static function dump(mixed $value, int $flags = 0): string return self::dumpNull($flags); case $value instanceof \DateTimeInterface: - $length = \strlen(rtrim($value->format('u'), '0')); - if (0 === $length) { - $format = 'c'; - } elseif ($length < 4) { - $format = 'Y-m-d\TH:i:s.vP'; - } else { - $format = 'Y-m-d\TH:i:s.uP'; - } - - return $value->format($format); + return $value->format(match (true) { + !$length = \strlen(rtrim($value->format('u'), '0')) => 'c', + $length < 4 => 'Y-m-d\TH:i:s.vP', + default => 'Y-m-d\TH:i:s.uP', + }); case $value instanceof \UnitEnum: return sprintf('!php/const %s::%s', $value::class, $value->name); case \is_object($value): @@ -721,20 +716,19 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer return $time; } - $length = \strlen(rtrim($time->format('u'), '0')); - if (0 === $length) { - try { - if (false !== $scalar = $time->getTimestamp()) { - return $scalar; - } - } catch (\ValueError) { - // no-op - } + if ('' !== rtrim($time->format('u'), '0')) { + return (float) $time->format('U.u'); + } - return (int) $time->format('U'); + try { + if (false !== $scalar = $time->getTimestamp()) { + return $scalar; + } + } catch (\ValueError) { + // no-op } - return (float) $time->format('U.u'); + return $time->format('U'); } } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index c49e4e9c7db76..2f9f58350f0a0 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -565,7 +565,7 @@ public static function getTestsForDump() */ public function testParseTimestampAsUnixTimestampByDefault(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond) { - $expectedDate = (new \DateTimeImmutable($yaml))->format('U'); + $expectedDate = (new \DateTimeImmutable($yaml, new \DateTimeZone('UTC')))->format('U'); $this->assertSame($microsecond ? (float) "$expectedDate.$microsecond" : (int) $expectedDate, Inline::parse($yaml)); }