Permalink
Browse files

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
  • Loading branch information...
markstory committed Nov 19, 2011
1 parent 2957a33 commit c8ab4ad05bd2edc9417b864838e38bfbe01eae7b
Showing with 49 additions and 5 deletions.
  1. +15 −0 lib/Cake/Test/Case/View/Helper/TimeHelperTest.php
  2. +34 −5 lib/Cake/View/Helper/TimeHelper.php
@@ -751,6 +751,7 @@ public function testI18nFormat() {
'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 @@ public function testI18nFormat() {
$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);
@@ -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 @@ public function nice($dateString = null, $userOffset = null, $format = null) {
$format = $this->niceFormat;
}
$format = $this->convertSpecifiers($format, $date);
return strftime($format, $date);
return $this->_strftime($format, $date);
}
/**
@@ -263,12 +266,12 @@ public function niceShort($dateString = null, $userOffset = null) {
$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 @@ public function i18nFormat($date, $format = null, $invalid = false, $userOffset
$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;
}
}

0 comments on commit c8ab4ad

Please sign in to comment.