From 13ee5b7338ae421fb7cc83a4ea1eb590a4b234ad Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 16 Jul 2013 15:16:30 +0200 Subject: [PATCH] ability to provide custom strings for timeAgoInWords() --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 21 +++++++++++++++++++++ lib/Cake/Utility/CakeTime.php | 18 +++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 97bba39f7..3911fd856 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -186,6 +186,27 @@ class CakeTimeTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * test the custom string options for timeAgoInWords + * + * @return void + */ + public function testTimeAgoInWordsCustomStrings() { + $result = $this->Time->timeAgoInWords( + strtotime('-8 years -4 months -2 weeks -3 days'), + array('relativeString' => 'at least %s ago', 'accuracy' => array('year' => 'year'), 'end' => '+10 years') + ); + $expected = 'at least 8 years ago'; + $this->assertEquals($expected, $result); + + $result = $this->Time->timeAgoInWords( + strtotime('+4 months +2 weeks +3 days'), + array('absoluteString' => 'exactly on %s', 'accuracy' => array('year' => 'year'), 'end' => '+2 months') + ); + $expected = 'exactly on 3/12/13'; + $this->assertEquals($expected, $result); + } + /** * Test the accuracy option for timeAgoInWords() * diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 422161a58..bae1273a3 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -692,7 +692,7 @@ class CakeTime { } /** - * Returns either a relative date or a formatted date depending + * Returns either a relative or an absolute formatted date depending * on the difference between the current time and given datetime. * $datetime should be in a *strtotime* - parsable format, like MySQL's datetime datatype. * @@ -708,6 +708,8 @@ class CakeTime { * - minute => The format if minutes > 0 (default "minute") * - second => The format if seconds > 0 (default "second") * - `end` => The end of relative time telling + * - `relativeString` => The printf compatible string when outputting relative time + * - `absoluteString` => The printf compatible string when outputting absolute time * - `userOffset` => Users offset from GMT (in hours) *Deprecated* use timezone intead. * - `timezone` => The user timezone the timestamp should be formatted in. * @@ -732,6 +734,8 @@ class CakeTime { $timezone = null; $format = self::$wordFormat; $end = self::$wordEnd; + $relativeString = __d('cake', '%s ago'); + $absoluteString = __d('cake', 'on %s'); $accuracy = self::$wordAccuracy; if (is_array($options)) { @@ -757,6 +761,14 @@ class CakeTime { if (isset($options['end'])) { $end = $options['end']; } + if (isset($options['relativeString'])) { + $relativeString = $options['relativeString']; + unset($options['relativeString']); + } + if (isset($options['absoluteString'])) { + $absoluteString = $options['absoluteString']; + unset($options['absoluteString']); + } unset($options['end'], $options['format']); } else { $format = $options; @@ -843,7 +855,7 @@ class CakeTime { } if ($diff > abs($now - self::fromString($end))) { - return __d('cake', 'on %s', date($format, $inSeconds)); + return sprintf($absoluteString, date($format, $inSeconds)); } $f = $accuracy['second']; @@ -887,7 +899,7 @@ class CakeTime { } if (!$backwards) { - return __d('cake', '%s ago', $relativeDate); + return sprintf($relativeString, $relativeDate); } return $relativeDate;