8000 [Form] fix support for years outside of the 32b range on x86 arch · symfony/symfony@2353f19 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2353f19

Browse files
[Form] fix support for years outside of the 32b range on x86 arch
1 parent 53d9b10 commit 2353f19

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,7 @@ private function listYears(array $years)
381381
$result = [];
382382

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

389387
return $result;

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -927,19 +927,15 @@ public function testDayErrorsBubbleUp($widget)
927927
$this->assertSame([$error], iterator_to_array($form->getErrors()));
928928
}
929929

930-
public function testYearsFor32BitsMachines()
930+
public function testYears()
931931
{
932-
if (4 !== \PHP_INT_SIZE) {
933-
$this->markTestSkipped('PHP 32 bit is required.');
934-
}
935-
936932
$view = $this->factory->create(static::TESTED_TYPE, null, [
937-
'years' => range(1900, 2040),
933+
'years' => [1900, 2000, 2040],
938934
])
939935
->createView();
940936

941937
$listChoices = [];
942-
foreach (range(1902, 2037) as $y) {
938+
foreach ([1900, 2000, 2040] as $y) {
943939
$listChoices[] = new ChoiceView($y, $y, $y);
944940
}
945941

src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static function create($locale, $datetype, $timetype, $timezone = null, $
180180
/**
181181
* Format the date/time value (timestamp) as a string.
182182
*
183-
* @param int|\DateTimeInterface $timestamp The timestamp to format
183+
* @param int|string|\DateTimeInterface $timestamp The timestamp to format
184184
*
185185
* @return string|bool The formatted value or false if formatting failed
186186
*
@@ -192,11 +192,15 @@ public function format($timestamp)
192192
{
193193
// intl allows timestamps to be passed as arrays - we don't
194194
if (\is_array($timestamp)) {
195-
$message = 'Only integer Unix timestamps and DateTime objects are supported';
195+
$message = 'Only Unix timestamps and DateTime objects are supported';
196196

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

200+
if (\is_string($timestamp) && $dt = \DateTime::createFromFormat('U', $timestamp)) {
201+
$timestamp = $dt;
202+
}
203+
200204
// behave like the intl extension
201205
$argumentError = null;
202206
if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) {
@@ -212,7 +216,7 @@ public function format($timestamp)
212216
}
213217

214218
if ($timestamp instanceof \DateTimeInterface) {
215-
$timestamp = $timestamp->getTimestamp();
219+
$timestamp = $timestamp->format('U');
216220
}
217221

218222
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
@@ -586,8 +590,7 @@ public function setTimeZone($timeZone)
586590
*/
587591
protected function createDateTime($timestamp)
588592
{
589-
$dateTime = new \DateTime();
590-
$dateTime->setTimestamp($timestamp);
593+
$dateTime = \DateTime::createFromFormat('U', $timestamp);
591594
$dateTime->setTimezone($this->dateTimeZone);
592595

593596
return $dateTime;

src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testFormatWithUnsupportedTimestampArgument()
7272
} catch (\Exception $e) {
7373
$this->assertInstanceOf(MethodArgumentValueNotImplementedException::class, $e);
7474

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

src/Symfony/Component/Yaml/Inline.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -674,17 +674,22 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
674674
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
675675
return (float) str_replace('_', '', $scalar);
676676
case Parser::preg_match(self::getTimestampRegex(), $scalar):
677+
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
678+
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
679+
677680
if (Yaml::PARSE_DATETIME & $flags) {
678-
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
679-
return new \DateTime($scalar, new \DateTimeZone('UTC'));
681+
return $time;
680682
}
681683

682-
$timeZone = date_default_timezone_get();
683-
date_default_timezone_set('UTC');
684-
$time = strtotime($scalar);
685-
date_default_timezone_set($timeZone);
684+
try {
685+
if (false !== $scalar = $time->getTimestamp()) {
686+
return $scalar;
687+
}
688+
} catch (\ValueError $e) {
689+
// no-op
690+
}
686691

687-
return $time;
692+
return $time->format('U');
688693
}
689694
}
690695

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public function getTestsForParse()
323323
['2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)],
324324
['2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)],
325325
['1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)],
326-
['1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)],
326+
['1730-10-30T02:59:43Z', \PHP_INT_SIZE === 4 ? '-7547547617' : gmmktime(2, 59, 43, 10, 30, 1730)],
327327

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

399399
['"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''],
400400
["'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''],

0 commit comments

Comments
 (0)
0