Support DateTimeInterface in IntlDateFormatter::format

This commit is contained in:
Pierre du Plessis 2019-08-05 12:19:56 +02:00
parent 2f97ab1643
commit b1e160c41e
No known key found for this signature in database
GPG Key ID: DCB9DD926044955D
2 changed files with 12 additions and 3 deletions

View File

@ -176,7 +176,7 @@ class IntlDateFormatter
/**
* 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 @@ class IntlDateFormatter
// 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 @@ class IntlDateFormatter
return false;
}
if ($timestamp instanceof \DateTime) {
if ($timestamp instanceof \DateTimeInterface) {
$timestamp = $timestamp->getTimestamp();
}

View File

@ -76,6 +76,7 @@ abstract class AbstractIntlDateFormatterTest extends TestCase
public function formatProvider()
{
$dateTime = new \DateTime('@0');
$dateTimeImmutable = new \DateTimeImmutable('@0');
$formatData = [
/* general */
@ -250,6 +251,12 @@ abstract class AbstractIntlDateFormatterTest extends TestCase
$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 @@ abstract class AbstractIntlDateFormatterTest extends TestCase
$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')));
}
/**