Fix issue in TimeHelper with translated values.

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

Conflicts:

	lib/Cake/View/Helper/TimeHelper.php
This commit is contained in:
mark_story 2011-11-19 10:08:04 -05:00
parent 2957a33b23
commit c8ab4ad05b
2 changed files with 49 additions and 5 deletions

View file

@ -751,6 +751,7 @@ class TimeHelperTest extends CakeTestCase {
'locales' => array(CAKE . 'Test' . 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);
@ -765,6 +766,20 @@ class TimeHelperTest extends CakeTestCase {
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
$this->assertEquals($expected, $result);
$time = strtotime('Wed Jan 13 13:59:28 2010');
$result = $this->Time->i18nFormat($time);
$expected = '13/01/10';
$this->assertEquals($expected, $result);
$result = $this->Time->i18nFormat($time, '%c');
$expected = 'mié 13 ene 2010 13:59:28 ' . strftime('%Z', $time);
$this->assertEquals($expected, $result);
$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->assertEquals($expected, $result);
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
$expected = 'Date invalid';
$this->assertEquals($expected, $result);

View file

@ -16,6 +16,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');
}
App::uses('AppHelper', 'View/Helper');
@ -241,7 +244,7 @@ class TimeHelper extends AppHelper {
$format = $this->niceFormat;
}
$format = $this->convertSpecifiers($format, $date);
return strftime($format, $date);
return $this->_strftime($format, $date);
}
/**
@ -263,12 +266,12 @@ class TimeHelper extends AppHelper {
$y = $this->isThisYear($date) ? '' : ' %Y';
if ($this->isToday($dateString, $userOffset)) {
$ret = __d('cake', 'Today, %s', strftime("%H:%M", $date));
$ret = __d('cake', 'Today, %s', $this->_strftime("%H:%M", $date));
} elseif ($this->wasYesterday($dateString, $userOffset)) {
$ret = __d('cake', 'Yesterday, %s', strftime("%H:%M", $date));
$ret = __d('cake', 'Yesterday, %s', $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;
@ -748,6 +751,32 @@ class TimeHelper extends AppHelper {
$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;
}
}