Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'blaugueux/master'
Browse files Browse the repository at this point in the history
* blaugueux/master:
  Improved localizeddate extension with dynamic DateTimeZone.

Conflicts:
	lib/Twig/Extensions/Extension/Intl.php
  • Loading branch information
fabpot committed Dec 15, 2012
2 parents dcdff02 + 0a8496c commit 91336a6
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/Twig/Extensions/Extension/Intl.php
Expand Up @@ -26,7 +26,7 @@ public function __construct()
public function getFilters() public function getFilters()
{ {
return array( return array(
'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter'), 'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter', array('needs_environment' => true)),
); );
} }


Expand All @@ -41,8 +41,10 @@ public function getName()
} }
} }


function twig_localized_date_filter($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null) function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null)
{ {
$date = twig_date_converter($env, $date, $timezone);

$formatValues = array( $formatValues = array(
'none' => IntlDateFormatter::NONE, 'none' => IntlDateFormatter::NONE,
'short' => IntlDateFormatter::SHORT, 'short' => IntlDateFormatter::SHORT,
Expand All @@ -55,17 +57,8 @@ function twig_localized_date_filter($date, $dateFormat = 'medium', $timeFormat =
$locale !== null ? $locale : Locale::getDefault(), $locale !== null ? $locale : Locale::getDefault(),
$formatValues[$dateFormat], $formatValues[$dateFormat],
$formatValues[$timeFormat], $formatValues[$timeFormat],
date_default_timezone_get() $date->getTimezone()->getName()
); );


if (!$date instanceof DateTime) {
if (ctype_digit((string) $date)) {
$date = new DateTime('@'.$date);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
} else {
$date = new DateTime($date);
}
}

return $formatter->format($date->getTimestamp()); return $formatter->format($date->getTimestamp());
} }

0 comments on commit 91336a6

Please sign in to comment.