Skip to content

Commit

Permalink
added support for DateTimeInterface in FormatDateTimeExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
craue committed Jan 9, 2017
1 parent e16538b commit 482b2d6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Tests/Twig/Extension/FormatDateTimeExtensionIntegrationTest.php
Expand Up @@ -81,6 +81,11 @@ public function dataFormatDate() {
array(44417974000, null, null, 'US/Hawaii', 'Jul 19, 3377'),
);

// TODO remove check as soon as PHP >= 5.5 is required
if (class_exists('DateTimeImmutable')) {
$testdata[] = array(new \DateTimeImmutable('2000-01-01'), 'de-DE', 'medium', null, '01.01.2000');
}

date_default_timezone_set($currentTimezone);

return $testdata;
Expand Down Expand Up @@ -122,6 +127,11 @@ public function dataFormatTime() {
array(new \DateTime('2000-01-01 12:34:56'), 'en-US', 'full', '12:34:56 PM Central European Time'),
);

// TODO remove check as soon as PHP >= 5.5 is required
if (class_exists('DateTimeImmutable')) {
$testdata[] = array(new \DateTimeImmutable('2000-01-01 12:34:56'), 'de-DE', 'medium', '12:34:56');
}

date_default_timezone_set($currentTimezone);

return $testdata;
Expand Down Expand Up @@ -209,6 +219,11 @@ public function dataFormatDateTime() {
array(44417974000, null, 'full', 'full', 'US/Hawaii', 'Saturday, July 19, 3377 12:06:40 PM Hawaii-Aleutian Standard Time'),
);

// TODO remove check as soon as PHP >= 5.5 is required
if (class_exists('DateTimeImmutable')) {
$testdata[] = array(new \DateTimeImmutable('2000-01-01 12:34:56'), 'de-DE', 'medium', 'medium', null, '01.01.2000 12:34:56');
}

date_default_timezone_set($currentTimezone);

return $testdata;
Expand Down
4 changes: 3 additions & 1 deletion Twig/Extension/FormatDateTimeExtension.php
Expand Up @@ -185,7 +185,9 @@ protected function getFormattedDateTime($value, $locale, $dateType, $timeType, $
}

// IntlDateFormatter#format() doesn't support \DateTime objects prior to PHP 5.3.4 (http://php.net/manual/intldateformatter.format.php)
if ($valueToUse instanceof \DateTime) {
// IntlDateFormatter#format() doesn't support \DateTimeInterface at all
// TODO remove additional (fallback) check for DateTime as soon as PHP >= 5.5 is required
if ($valueToUse instanceof \DateTimeInterface || $valueToUse instanceof \DateTime) {
// \DateTime::getTimestamp() would return false for far future dates on 32-bit systems (https://bugs.php.net/bug.php?id=50590)
$valueToUse = floatval($valueToUse->format('U'));
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
"dev-master": "2.2.x-dev"
}
}
}

0 comments on commit 482b2d6

Please sign in to comment.