Browse Source

Merge branch 'relative-sprintf' into 2.6

Refs #4550
mark_story 11 years ago
parent
commit
249dc5650b
2 changed files with 20 additions and 4 deletions
  1. 15 3
      lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. 5 1
      lib/Cake/Utility/CakeTime.php

+ 15 - 3
lib/Cake/Test/Case/Utility/CakeTimeTest.php

@@ -263,7 +263,7 @@ class CakeTimeTest extends CakeTestCase {
 	}
 
 /**
- * Test the format option of timeAgoInWords()
+ * Test the format option of timeAgoInWords() with date() and strftime compatible strings
  *
  * @return void
  */
@@ -271,8 +271,8 @@ class CakeTimeTest extends CakeTestCase {
 		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
 		$this->assertEquals('on 2007-09-25', $result);
 
-		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
-		$this->assertEquals('on 2007-09-25', $result);
+		$result = $this->Time->timeAgoInWords('2007-9-25', '%x');
+		$this->assertEquals('on '. strftime('%x', strtotime('2007-9-25')), $result);
 
 		$result = $this->Time->timeAgoInWords(
 			strtotime('+2 weeks +2 days'),
@@ -281,10 +281,22 @@ class CakeTimeTest extends CakeTestCase {
 		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
 
 		$result = $this->Time->timeAgoInWords(
+			strtotime('+2 weeks +2 days'),
+			'%x'
+		);
+		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
+
+		$result = $this->Time->timeAgoInWords(
 			strtotime('+2 months +2 days'),
 			array('end' => '1 month', 'format' => 'Y-m-d')
 		);
 		$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
+
+		$result = $this->Time->timeAgoInWords(
+			strtotime('+2 months +2 days'),
+			array('end' => '1 month', 'format' => '%x')
+		);
+		$this->assertEquals('on ' . strftime('%x', strtotime('+2 months +2 days')), $result);
 	}
 
 /**

+ 5 - 1
lib/Cake/Utility/CakeTime.php

@@ -796,7 +796,11 @@ class CakeTime {
 		}
 
 		if ($diff > abs($now - self::fromString($end))) {
-			return sprintf($absoluteString, date($format, $inSeconds));
+			return sprintf($absoluteString,
+					(strpos($format, '%') === false) ?
+						date($format, $inSeconds) :
+						self::_strftime($format, $inSeconds)
+			);
 		}
 
 		// If more than a week, then take into account the length of months