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
  • Loading branch information...
markstory committed Nov 19, 2011
1 parent f71f8b5 commit b58753779dfca298cd85df3ffc6911c64ae31b1a
Showing with 49 additions and 5 deletions.
  1. +34 −5 cake/libs/view/helpers/time.php
  2. +15 −0 cake/tests/cases/libs/view/helpers/time.test.php
@@ -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.
@@ -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);
}
/**
@@ -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;
@@ -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;
}
}
@@ -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);
@@ -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);

1 comment on commit b587537

@bar

This comment has been minimized.

Show comment
Hide comment
@bar

bar Jan 3, 2012

Contributor

nice to see this hits master :)

Contributor

bar commented on b587537 Jan 3, 2012

nice to see this hits master :)

Please sign in to comment.