diff --git a/lib/Cake/View/Helper/TimeHelper.php b/lib/Cake/View/Helper/TimeHelper.php index 2b7674e30..d6ddf9477 100644 --- a/lib/Cake/View/Helper/TimeHelper.php +++ b/lib/Cake/View/Helper/TimeHelper.php @@ -29,6 +29,31 @@ App::uses('AppHelper', 'View/Helper'); */ class TimeHelper extends AppHelper { +/** + * The format to use when formatting a time using `TimeHelper::nice()` + * + * The format should use the locale strings as defined in the PHP docs under + * `strftime` (http://php.net/manual/en/function.strftime.php) + * + * @var string + * @see TimeHelper::format() + */ + public $niceFormat = '%a, %b %eS %Y, %H:%M'; + +/** + * Constructor + * + * @param View $View the view object the helper is attached to. + * @param array $settings Settings array Settings array + * @return void + */ + public function __construct(View $View, $settings = array()) { + if (isset($settings['niceFormat'])) { + $this->niceFormat = $settings['niceFormat']; + } + parent::__construct($View, $settings); + } + /** * Converts a string representing the format for the function strftime and returns a * windows safe and i18n aware format. @@ -188,19 +213,26 @@ class TimeHelper extends AppHelper { /** * Returns a nicely formatted date string for given Datetime string. * + * See http://php.net/manual/en/function.strftime.php for information on formatting + * using locale strings. + * * @param string $dateString Datetime string or Unix timestamp * @param int $userOffset User's offset from GMT (in hours) + * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used * @return string Formatted date string * @access public * @link http://book.cakephp.org/view/1471/Formatting */ - public function nice($dateString = null, $userOffset = null) { + public function nice($dateString = null, $userOffset = null, $format = null) { if ($dateString != null) { $date = $this->fromString($dateString, $userOffset); } else { $date = time(); } - $format = $this->convertSpecifiers('%a, %b %eS %Y, %H:%M', $date); + if (!$format) { + $format = $this->niceFormat; + } + $format = $this->convertSpecifiers($format, $date); return strftime($format, $date); } diff --git a/lib/Cake/tests/cases/console/shells/tasks/template.test.php b/lib/Cake/tests/cases/console/shells/tasks/template.test.php index ef2c305e3..e0e4c8ba9 100644 --- a/lib/Cake/tests/cases/console/shells/tasks/template.test.php +++ b/lib/Cake/tests/cases/console/shells/tasks/template.test.php @@ -87,7 +87,7 @@ class TemplateTaskTest extends CakeTestCase { public function testFindingInstalledThemesForBake() { $consoleLibs = CAKE . 'console' . DS; $this->Task->initialize(); - $this->assertEqual($this->Task->templatePaths, array('default' => $consoleLibs . 'templates' . DS . 'default' . DS)); + $this->assertEqual($this->Task->templatePaths['default'], $consoleLibs . 'templates' . DS . 'default' . DS); } /** diff --git a/lib/Cake/tests/cases/libs/view/helpers/time.test.php b/lib/Cake/tests/cases/libs/view/helpers/time.test.php index 2d14479e9..52f78e053 100644 --- a/lib/Cake/tests/cases/libs/view/helpers/time.test.php +++ b/lib/Cake/tests/cases/libs/view/helpers/time.test.php @@ -324,6 +324,13 @@ class TimeHelperTest extends CakeTestCase { $time = null; $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); + + $time = time(); + $this->assertEqual(date('D', $time), $this->Time->nice($time, null, '%a')); + $this->assertEqual(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y')); + + $this->Time->niceFormat = '%Y-%d-%m'; + $this->assertEqual(date('Y-d-m', $time), $this->Time->nice($time)); } /**