From b1e160c41e62aec2c496092505763e09e0bc8f15 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 5 Aug 2019 12:19:56 +0200 Subject: [PATCH] Support DateTimeInterface in IntlDateFormatter::format --- .../Component/Intl/DateFormatter/IntlDateFormatter.php | 6 +++--- .../DateFormatter/AbstractIntlDateFormatterTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php index 0f19310f22bf..197a2e3db06f 100644 --- a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php +++ b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php @@ -176,7 +176,7 @@ public static function create($locale, $datetype, $timetype, $timezone = null, $ /** * Format the date/time value (timestamp) as a string. * - * @param int|\DateTime $timestamp The timestamp to format + * @param int|\DateTimeInterface $timestamp The timestamp to format * * @return string|bool The formatted value or false if formatting failed * @@ -195,7 +195,7 @@ public function format($timestamp) // behave like the intl extension $argumentError = null; - if (!\is_int($timestamp) && !$timestamp instanceof \DateTime) { + if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) { $argumentError = sprintf('datefmt_format: string \'%s\' is not numeric, which would be required for it to be a valid date', $timestamp); } @@ -207,7 +207,7 @@ public function format($timestamp) return false; } - if ($timestamp instanceof \DateTime) { + if ($timestamp instanceof \DateTimeInterface) { $timestamp = $timestamp->getTimestamp(); } diff --git a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php index e472000974a6..682380bf5415 100644 --- a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php @@ -76,6 +76,7 @@ public function testFormat($pattern, $timestamp, $expected) public function formatProvider() { $dateTime = new \DateTime('@0'); + $dateTimeImmutable = new \DateTimeImmutable('@0'); $formatData = [ /* general */ @@ -250,6 +251,12 @@ public function formatProvider() $formatData[] = ['h:mm a', $dateTime, '12:00 AM']; $formatData[] = ['yyyyy.MMMM.dd hh:mm aaa', $dateTime, '01970.January.01 12:00 AM']; + /* general, DateTimeImmutable */ + $formatData[] = ['y-M-d', $dateTimeImmutable, '1970-1-1']; + $formatData[] = ["EEE, MMM d, ''yy", $dateTimeImmutable, "Thu, Jan 1, '70"]; + $formatData[] = ['h:mm a', $dateTimeImmutable, '12:00 AM']; + $formatData[] = ['yyyyy.MMMM.dd hh:mm aaa', $dateTimeImmutable, '01970.January.01 12:00 AM']; + if (IcuVersion::compare(Intl::getIcuVersion(), '59.1', '>=', 1)) { // Before ICU 59.1 GMT was used instead of UTC $formatData[] = ["yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 UTC']; @@ -272,6 +279,8 @@ public function testFormatUtcAndGmtAreSplit() $this->assertSame('1970.01.01 at 00:00:00 GMT', $gmtFormatter->format(new \DateTime('@0'))); $this->assertSame('1970.01.01 at 00:00:00 UTC', $utcFormatter->format(new \DateTime('@0'))); + $this->assertSame('1970.01.01 at 00:00:00 GMT', $gmtFormatter->format(new \DateTimeImmutable('@0'))); + $this->assertSame('1970.01.01 at 00:00:00 UTC', $utcFormatter->format(new \DateTimeImmutable('@0'))); } /**