8000 [Form] fix support for years outside of the 32b range on x86 arch by nicolas-grekas · Pull Request #41394 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] fix support for years outside of the 32b range on x86 arch #41394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,7 @@ private function listYears(array $years)
$result = [];

foreach ($years as $year) {
if (false !== $y = gmmktime(0, 0, 0, 6, 15, $year)) {
$result[$y] = $year;
}
$result[\PHP_INT_SIZE === 4 ? \DateTime::createFromFormat('Y e', $year.' UTC')->format('U') : gmmktime(0, 0, 0, 6, 15, $year)] = $year;
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,19 +927,15 @@ public function testDayErrorsBubbleUp($widget)
$this->assertSame([$error], iterator_to_array($form->getErrors()));
}

public function testYearsFor32BitsMachines()
public function testYears()
{
if (4 !== \PHP_INT_SIZE) {
$this->markTestSkipped('PHP 32 bit is required.');
}

$view = $this->factory->create(static::TESTED_TYPE, null, [
'years' => range(1900, 2040),
'years' => [1900, 2000, 2040],
])
->createView();

$listChoices = [];
foreach (range(1902, 2037) as $y) {
foreach ([1900, 2000, 2040] as $y) {
$listChoices[] = new ChoiceView($y, $y, $y);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function create($locale, $datetype, $timetype, $timezone = null, $
/**
* Format the date/time value (timestamp) as a string.
*
* @param int|\DateTimeInterface $timestamp The timestamp to format
* @param int|string|\DateTimeInterface $timestamp The timestamp to format
*
* @return string|bool The formatted value or false if formatting failed
*
Expand All @@ -192,11 +192,15 @@ public function format($timestamp)
{
// intl allows timestamps to be passed as arrays - we don't
if (\is_array($timestamp)) {
$message = 'Only integer Unix timestamps and DateTime objects are supported';
$message = 'Only Unix timestamps and DateTime objects are supported';

throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message);
}

if (\is_string($timestamp) && $dt = \DateTime::createFromFormat('U', $timestamp)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be applied to the polyfills repo, right ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$timestamp = $dt;
}

// behave like the intl extension
$argumentError = null;
if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) {
Expand All @@ -212,7 +216,7 @@ public function format($timestamp)
}

if ($timestamp instanceof \DateTimeInterface) {
$timestamp = $timestamp->getTimestamp();
$timestamp = $timestamp->format('U');
}

$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
Expand Down Expand Up @@ -586,8 +590,7 @@ public function setTimeZone($timeZone)
*/
protected function createDateTime($timestamp)
{
$dateTime = new \DateTime();
$dateTime->setTimestamp($timestamp);
$dateTime = \DateTime::createFromFormat('U', $timestamp);
$dateTime->setTimezone($this->dateTimeZone);

return $dateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testFormatWithUnsupportedTimestampArgument()
} catch (\Exception $e) {
$this->assertInstanceOf(MethodArgumentValueNotImplementedException::class, $e);

$this->assertStringEndsWith('Only integer Unix timestamps and DateTime objects are supported. Please install the "intl" extension for full localization capabilities.', $e->getMessage());
$this->assertStringEndsWith('Only Unix timestamps and DateTime objects are supported. Please install the "intl" extension for full localization capabilities.', $e->getMessage());
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,17 +674,22 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
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'));

if (Yaml::PARSE_DATETIME & $flags) {
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
return new \DateTime($scalar, new \DateTimeZone('UTC'));
return $time;
}

$timeZone = date_default_timezone_get();
date_default_timezone_set('UTC');
$time = strtotime($scalar);
date_default_timezone_set($timeZone);
try {
if (false !== $scalar = $time->getTimestamp()) {
return $scalar;
}
} catch (\ValueError $e) {
// no-op
}

return $time;
return $time->format('U');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function getTestsForParse()
['2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)],
['2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)],
['1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)],
['1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)],
['1730-10-30T02:59:43Z', \PHP_INT_SIZE === 4 ? '-7547547617' : gmmktime(2, 59, 43, 10, 30, 1730)],

['"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''],
["'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''],
Expand Down Expand Up @@ -394,7 +394,7 @@ public function getTestsForParseWithMapObjects()
['2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)],
['2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)],
['1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)],
['1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)],
['1730-10-30T02:59:43Z', \PHP_INT_SIZE === 4 ? '-7547547617' : gmmktime(2, 59, 43, 10, 30, 1730)],

['"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''],
["'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''],
Expand Down
0