Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b06b818

Browse files
[Yaml] Fix parsing sub-second dates on x86
1 parent 46f3879 commit b06b818

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
6.3
55
---
66

7-
* Add support to dump int keys as strings by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag.
7+
* Add support to dump int keys as strings by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag
88

99
6.2
1010
---

src/Symfony/Component/Yaml/Inline.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,11 @@ public static function dump(mixed $value, int $flags = 0): string
110110

111111
return self::dumpNull($flags);
112112
case $value instanceof \DateTimeInterface:
113-
$length = \strlen(rtrim($value->format('u'), '0'));
114-
if (0 === $length) {
115-
$format = 'c';
116-
} elseif ($length < 4) {
117-
$format = 'Y-m-d\TH:i:s.vP';
118-
} else {
119-
$format = 'Y-m-d\TH:i:s.uP';
120-
}
121-
122-
return $value->format($format);
113+
return $value->format(match (true) {
114+
!$length = \strlen(rtrim($value->format('u'), '0')) => 'c',
115+
$length < 4 => 'Y-m-d\TH:i:s.vP',
116+
default => 'Y-m-d\TH:i:s.uP',
117+
});
123118
case $value instanceof \UnitEnum:
124119
return sprintf('!php/const %s::%s', $value::class, $value->name);
125120
case \is_object($value):
@@ -721,20 +716,19 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
721716
return $time;
722717
}
723718

724-
$length = \strlen(rtrim($time->format('u'), '0'));
725-
if (0 === $length) {
726-
try {
727-
if (false !== $scalar = $time->getTimestamp()) {
728-
return $scalar;
729-
}
730-
} catch (\ValueError) {
731-
// no-op
732-
}
719+
if ('' !== rtrim($time->format('u'), '0')) {
720+
return (float) $time->format('U.u');
721+
}
733722

734-
return (int) $time->format('U');
723+
try {
724+
if (false !== $scalar = $time->getTimestamp()) {
725+
return $scalar;
726+
}
727+
} catch (\ValueError) {
728+
// no-op
735729
}
736730

737-
return (float) $time->format('U.u');
731+
return $time->format('U');
738732
}
739733
}
740734

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ public static function getTestsForDump()
565565
*/
566566
public function testParseTimestampAsUnixTimestampByDefault(string $yaml, int $year, int $month, int $day, int $hour, int $minute, int $second, int $microsecond)
567567
{
568-
$expectedDate = (new \DateTimeImmutable($yaml))->format('U');
568+
$expectedDate = (new \DateTimeImmutable($yaml, new \DateTimeZone('UTC')))->format('U');
569569
$this->assertSame($microsecond ? (float) "$expectedDate.$microsecond" : (int) $expectedDate, Inline::parse($yaml));
570570
}
571571

0 commit comments

Comments
 (0)