Skip to content

Commit

Permalink
Merge pull request #33 from mremi/fix_datetime_helper
Browse files Browse the repository at this point in the history
Fixed date helper to be compliant with a specific php version
  • Loading branch information
rande committed Oct 22, 2012
2 parents d21d913 + d498f3b commit 0208ca4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
23 changes: 19 additions & 4 deletions Templating/Helper/BaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,25 @@ public static function getUCIDataVersion()

$info = explode("\n", $content);

foreach($info as $line) {
$results = array();
if (preg_match('/(ICU Data version|ICU version) => (.*)/', $line, $results)) {
return $results[1];
if ('cli' == php_sapi_name()) {
foreach ($info as $line) {
$results = array();

if (preg_match('/(ICU Data version|ICU version) => (.*)/', $line, $results)) {
return $results[2];
}
}
} else {
foreach ($info as $line) {
$results = array();

if (preg_match('/(ICU Data version).*/', $line, $results)) {
return trim(strtolower(strip_tags($results[0])), 'ICU Data version');
}

if (preg_match('/(ICU version).*/', $line, $results)) {
return trim(strtolower(strip_tags($results[0])), 'icu version');
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions Templating/Helper/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,22 @@ public function format($datetime, $pattern, $locale = null, $timezone = null)
*/
public function process(\IntlDateFormatter $formatter, \Datetime $date)
{
return $this->fixCharset($formatter->format($date->getTimestamp()));
// strange bug with PHP 5.3.3-7+squeeze14 with Suhosin-Patch
// getTimestamp() method alters the object...
return $this->fixCharset($formatter->format((int) $date->format('U')));
}

/**
* @param \Datetime|string|integer $data
* @param null|string timezone
* Gets a date time instance by a given data and timezone
*
* @param \Datetime|string|integer $data Value representing date
* @param null|string $timezone Timezone of the date
*
* @return \Datetime
*/
public function getDatetime($data, $timezone = null)
{
if($data instanceof \DateTime) {
if ($data instanceof \DateTime) {
return $data;
}

Expand Down
8 changes: 7 additions & 1 deletion Tests/Helper/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public function testLocale()

// custom format
$this->assertEquals('30 novembre 1981', $helper->formatDate($datetimeParis, null, null, \IntlDateFormatter::LONG));
$this->assertEquals('02:00:00 HNEC', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG));

if (version_compare(DateTimeHelper::getUCIDataVersion(), '4.8.0', '>=')) {
$this->assertEquals('02:00:00 UTC+01:00', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG));
} else {
$this->assertEquals('02:00:00 HNEC', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG));
}

$this->assertEquals('30 novembre 1981 02:00', $helper->formatDateTime($datetimeParis, null, null, \IntlDateFormatter::LONG, \IntlDateFormatter::SHORT));
$this->assertEquals('30 nov. 1981 ap. J.-C.', $helper->format($datetimeParis, 'dd MMM Y G'));
}
Expand Down

0 comments on commit 0208ca4

Please sign in to comment.