Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for TimeHelper functions not returning a valid utf8 string when L…

…C_TIME file has characters represented by unicode code point higher than 128.

Also harden tests. Fixes #912
(http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/912)
  • Loading branch information...
commit 01750c5d79c9fa59f8f53d2b54a0b1c381647a21 1 parent 169b0a5
@bar authored
View
28 cake/libs/multibyte.php
@@ -1066,6 +1066,34 @@ function mimeEncode($string, $charset = null, $newline = "\r\n") {
}
/**
+ * Format a local time/date according to locale settings, using the app's encoding
+ *
+ * @return string
+ * @access public
+ * @static
+ */
+ function strftime() {
+ $args = func_get_args();
+ $format = call_user_func_array('strftime', $args);
+ if (function_exists('mb_internal_encoding')) {
+ $encoding = mb_internal_encoding();
+ } else {
+ $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;
+ }
+
+/**
* Return the Code points range for Unicode characters
*
* @param interger $decimal
View
13 cake/libs/view/helpers/time.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 Multibyte::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), Multibyte::strftime("%H:%M", $date));
} elseif ($this->wasYesterday($dateString, $userOffset)) {
- $ret = sprintf(__('Yesterday, %s',true), strftime("%H:%M", $date));
+ $ret = sprintf(__('Yesterday, %s',true), Multibyte::strftime("%H:%M", $date));
} else {
$format = $this->convertSpecifiers("%b %eS{$y}, %H:%M", $date);
- $ret = strftime($format, $date);
+ $ret = Multibyte::strftime($format, $date);
}
return $ret;
@@ -739,6 +742,6 @@ function i18nFormat($date, $format = null, $invalid = false, $userOffset = null)
$format = '%x';
}
$format = $this->convertSpecifiers($format, $date);
- return strftime($format, $date);
+ return Multibyte::strftime($format, $date);
}
}
View
15 cake/tests/cases/libs/view/helpers/time.test.php
@@ -770,6 +770,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);
@@ -784,6 +785,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);
Please sign in to comment.
Something went wrong with that request. Please try again.