Skip to content

Commit

Permalink
[Yaml] Fix parsing sub-second dates on x86
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Feb 14, 2023
1 parent 46f3879 commit b06b818
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
36 changes: 15 additions & 21 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit b06b818

Please sign in to comment.