8000 Support DateTimeInterface in IntlDateFormatter::format · symfony/symfony@73044d6 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 73044d6

Browse files
committed
Support DateTimeInterface in IntlDateFormatter::format
1 parent 1fc080b commit 73044d6

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function create($locale, $datetype, $timetype, $timezone = null, $
176176
/**
177177
* Format the date/time value (timestamp) as a string.
178178
*
179-
* @param int|\DateTime $timestamp The timestamp to format
179+
* @param int|\DateTimeInterface $timestamp The timestamp to format
180180
*
181181
* @return string|bool The formatted value or false if formatting failed
182182
*
@@ -195,7 +195,7 @@ public function format($timestamp)
195195

196196
// behave like the intl extension
197197
$argumentError = null;
198-
if (!\is_int($timestamp) && !$timestamp instanceof \DateTime) {
198+
if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) {
199199
$argumentError = sprintf('datefmt_format: string \'%s\' is not numeric, which would be required for it to be a valid date', $timestamp);
200200
}
201201

@@ -207,7 +207,7 @@ public function format($timestamp)
207207
return false;
208208
}
209209

210-
if ($timestamp instanceof \DateTime) {
210+
if ($timestamp instanceof \DateTimeInterface) {
211211
$timestamp = $timestamp->getTimestamp();
212212
}
213213

src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function testFormat($pattern, $timestamp, $expected)
7676
public function formatProvider()
7777
{
7878
$dateTime = new \DateTime('@0');
79+
$dateTimeImmutable = new \DateTimeImmutable('@0');
7980

8081
$formatData = [
8182
/* general */
@@ -250,6 +251,12 @@ public function formatProvider()
250251
$formatData[] = ['h:mm a', $dateTime, '12:00 AM'];
251252
$formatData[] = ['yyyyy.MMMM.dd hh:mm aaa', $dateTime, '01970.January.01 12:00 AM'];
252253

254+
/* general, DateTimeImmutable */
255+
$formatData[] = ['y-M-d', $dateTimeImmutable, '1970-1-1'];
256+
$formatData[] = ["EEE, MMM d, ''yy", $dateTimeImmutable, "Thu, Jan 1, '70"];
257+
$formatData[] = ['h:mm a', $dateTimeImmutable, '12:00 AM'];
258+
$formatData[] = ['yyyyy.MMMM.dd hh:mm aaa', $dateTimeImmutable, '01970.January.01 12:00 AM'];
259+
253260
if (IcuVersion::compare(Intl::getIcuVersion(), '59.1', '>=', 1)) {
254261
// Before ICU 59.1 GMT was used instead of UTC
255262
$formatData[] = ["yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 UTC'];
@@ -269,6 +276,8 @@ public function testFormatUtcAndGmtAreSplit()
269276

270277
$this->assertSame('1970.01.01 at 00:00:00 GMT', $gmtFormatter->format(new \DateTime('@0')));
271278
$this->assertSame('1970.01.01 at 00:00:00 UTC', $utcFormatter->format(new \DateTime('@0')));
279+
$this->assertSame('1970.01.01 at 00:00:00 GMT', $gmtFormatter->format(new \DateTimeImmutable('@0')));
280+
$this->assertSame('1970.01.01 at 00:00:00 UTC', $utcFormatter->format(new \DateTimeImmutable('@0')));
272281
}
273282

274283
/**

0 commit comments

Comments
 (0)
0