Skip to content

Commit

Permalink
refactored date filter code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 30, 2011
1 parent bd08072 commit 519b680
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
* 1.5.0-RC2 * 1.5.0-RC2


* fixed the date filter for DateInterval instances (setTimezone() does not exist for them)
* refactored Twig_Template::display() to ease its extension * refactored Twig_Template::display() to ease its extension
* added a number_format filter * added a number_format filter


Expand Down
32 changes: 17 additions & 15 deletions lib/Twig/Extension/Core.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -313,24 +313,26 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
$format = $env->getExtension('core')->getDateFormat(); $format = $env->getExtension('core')->getDateFormat();
} }


if (!$date instanceof DateInterval) { if ($date instanceof DateInterval || $date instanceof DateTime) {
if (!$date instanceof DateTime) { return $date->format($format);
$asString = (string) $date; }
if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
$date = new DateTime('@'.$date);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
} else {
$date = new DateTime($date);
}
}


if (null !== $timezone) { // convert to a DateTime
if (!$timezone instanceof DateTimeZone) { $asString = (string) $date;
$timezone = new DateTimeZone($timezone); if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
} $date = new DateTime('@'.$date);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
} else {
$date = new DateTime($date);
}


$date->setTimezone($timezone); // set Timezone
if (null !== $timezone) {
if (!$timezone instanceof DateTimeZone) {
$timezone = new DateTimeZone($timezone);
} }

$date->setTimezone($timezone);
} }


return $date->format($format); return $date->format($format);
Expand Down

0 comments on commit 519b680

Please sign in to comment.