Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Intl] Support DateTimeInterface in IntlDateFormatter::format #32947

Merged
merged 1 commit into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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')));
}

/**
Expand Down