Skip to content

Commit

Permalink
Support DateTimeInterface in IntlDateFormatter::format
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Aug 5, 2019
1 parent 1fc080b commit 73044d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Expand Up @@ -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
*
Expand All @@ -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);
}

Expand All @@ -207,7 +207,7 @@ public function format($timestamp)
return false;
}

if ($timestamp instanceof \DateTime) {
if ($timestamp instanceof \DateTimeInterface) {
$timestamp = $timestamp->getTimestamp();
}

Expand Down
Expand Up @@ -76,6 +76,7 @@ public function testFormat($pattern, $timestamp, $expected)
public function formatProvider()
{
$dateTime = new \DateTime('@0');
$dateTimeImmutable = new \DateTimeImmutable('@0');

$formatData = [
/* general */
Expand Down Expand Up @@ -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'];
Expand All @@ -269,6 +276,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')));
}

/**
Expand Down

0 comments on commit 73044d6

Please sign in to comment.