diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index 0b9a4ffab3..88f5457625 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -381,9 +381,7 @@ class DateType extends AbstractType $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; diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php index 5891cc08aa..a4f93cdee2 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php @@ -927,19 +927,15 @@ class DateTypeTest extends BaseTypeTest $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); } diff --git a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php index 317510c236..5011ee3321 100644 --- a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php +++ b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php @@ -180,7 +180,7 @@ abstract class IntlDateFormatter /** * 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 * @@ -192,11 +192,15 @@ abstract class IntlDateFormatter { // 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)) { + $timestamp = $dt; + } + // behave like the intl extension $argumentError = null; if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) { @@ -212,7 +216,7 @@ abstract class IntlDateFormatter } if ($timestamp instanceof \DateTimeInterface) { - $timestamp = $timestamp->getTimestamp(); + $timestamp = $timestamp->format('U'); } $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); @@ -586,8 +590,7 @@ abstract class IntlDateFormatter */ protected function createDateTime($timestamp) { - $dateTime = new \DateTime(); - $dateTime->setTimestamp($timestamp); + $dateTime = \DateTime::createFromFormat('U', $timestamp); $dateTime->setTimezone($this->dateTimeZone); return $dateTime; diff --git a/src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php b/src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php index de61e7fb84..fd562b4d48 100644 --- a/src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php @@ -72,7 +72,7 @@ class IntlDateFormatterTest extends AbstractIntlDateFormatterTest } 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()); } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 935a7b5fc1..a317bb0438 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -674,17 +674,22 @@ class Inline 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'); } } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index ec0e182174..b1f0206d54 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -323,7 +323,7 @@ class InlineTest extends TestCase ['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\''], @@ -394,7 +394,7 @@ class InlineTest extends TestCase ['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\''],