Skip to content

Commit

Permalink
Fix issue in TimeHelper with translated values.
Browse files Browse the repository at this point in the history
LC_TIME files using unicode code points would incorrectly display.
Use either the Multibyte class or mbstring to correctly detect
and convert values.

Fixes #912
  • Loading branch information
markstory committed Nov 19, 2011
1 parent f71f8b5 commit b587537
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
39 changes: 34 additions & 5 deletions cake/libs/view/helpers/time.php
Expand Up @@ -17,6 +17,9 @@
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!class_exists('Multibyte')) {
App::import('Core', 'Multibyte');
}

/**
* Time Helper class for easy use of time data.
Expand Down Expand Up @@ -213,7 +216,7 @@ function nice($dateString = null, $userOffset = null) {
$date = time();
}
$format = $this->convertSpecifiers('%a, %b %eS %Y, %H:%M', $date);
return strftime($format, $date);
return $this->_strftime($format, $date);
}

/**
Expand All @@ -236,12 +239,12 @@ function niceShort($dateString = null, $userOffset = null) {
$y = $this->isThisYear($date) ? '' : ' %Y';

if ($this->isToday($dateString, $userOffset)) {
$ret = sprintf(__('Today, %s',true), strftime("%H:%M", $date));
$ret = sprintf(__('Today, %s',true), $this->_strftime("%H:%M", $date));
} elseif ($this->wasYesterday($dateString, $userOffset)) {
$ret = sprintf(__('Yesterday, %s',true), strftime("%H:%M", $date));
$ret = sprintf(__('Yesterday, %s',true), $this->_strftime("%H:%M", $date));
} else {
$format = $this->convertSpecifiers("%b %eS{$y}, %H:%M", $date);
$ret = strftime($format, $date);
$ret = $this->_strftime($format, $date);
}

return $ret;
Expand Down Expand Up @@ -750,6 +753,32 @@ function i18nFormat($date, $format = null, $invalid = false, $userOffset = null)
$format = '%x';
}
$format = $this->convertSpecifiers($format, $date);
return strftime($format, $date);
return $this->_strftime($format, $date);
}

/**
* Multibyte wrapper for strftime.
*
* Handles utf8_encoding the result of strftime when necessary.
*
* @param string $format Format string.
* @param int $date Timestamp to format.
* @return string formatted string with correct encoding.
*/
function _strftime($format, $date) {
$format = strftime($format, $date);
$encoding = Configure::read('App.encoding');

if (!empty($encoding) && $encoding === 'UTF-8') {
if (function_exists('mb_check_encoding')) {
$valid = mb_check_encoding($format, $encoding);
} else {
$valid = !Multibyte::checkMultibyte($format);
}
if (!$valid) {
$format = utf8_encode($format);
}
}
return $format;
}
}
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/view/helpers/time.test.php
Expand Up @@ -777,6 +777,7 @@ function testI18nFormat() {
'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS)
), true);
Configure::write('Config.language', 'time_test');

$time = strtotime('Thu Jan 14 13:59:28 2010');

$result = $this->Time->i18nFormat($time);
Expand All @@ -791,6 +792,20 @@ function testI18nFormat() {
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
$this->assertEqual($result, $expected);

$time = strtotime('Wed Jan 13 13:59:28 2010');

$result = $this->Time->i18nFormat($time);
$expected = '13/01/10';
$this->assertEqual($result, $expected);

$result = $this->Time->i18nFormat($time, '%c');
$expected = 'mié 13 ene 2010 13:59:28 ' . strftime('%Z', $time);
$this->assertEqual($result, $expected);

$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
$this->assertEqual($result, $expected);

$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
$expected = 'Date invalid';
$this->assertEqual($result, $expected);
Expand Down

1 comment on commit b587537

@bar
Copy link
Contributor

@bar bar commented on b587537 Jan 3, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to see this hits master :)

Please sign in to comment.