Skip to content

Commit

Permalink
bug #49372 [Yaml] Fix parsing sub-second dates on x86 (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

[Yaml] Fix parsing sub-second dates on x86

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Fix #49164

Commits
-------

b06b818 [Yaml] Fix parsing sub-second dates on x86
  • Loading branch information
nicolas-grekas committed Feb 14, 2023
2 parents 46f3879 + b06b818 commit e16aea4
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 e16aea4

Please sign in to comment.