Browse Source

Using i18nFormat inside timeAgoInWords

Jose Lorenzo Rodriguez 12 years ago
parent
commit
0769f274c0
2 changed files with 11 additions and 9 deletions
  1. 5 3
      src/Utility/Time.php
  2. 6 6
      tests/TestCase/Utility/TimeTest.php

+ 5 - 3
src/Utility/Time.php

@@ -72,7 +72,7 @@ class Time extends Carbon {
  * @var string
  * @see \Cake\Utility\Time::timeAgoInWords()
  */
-	public static $wordFormat = 'j/n/y';
+	public static $wordFormat = 'd/M/YY';
 
 /**
  * The format to use when formatting a time using `Cake\Utility\Time::timeAgoInWords()`
@@ -211,7 +211,8 @@ class Time extends Carbon {
  * - 3 weeks, 4 days ago
  * - 15 seconds ago
  *
- * Default date formatting is d/m/yy e.g: on 18/2/09
+ * Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using
+ * `i18nFormat`, see the method for the valid formatting strings
  *
  * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
  * like 'Posted ' before the function output.
@@ -277,7 +278,8 @@ class Time extends Carbon {
 		}
 
 		if ($diff > abs($now - (new static($end))->format('U'))) {
-			return sprintf($absoluteString, date($format, $inSeconds));
+			$absoluteTime = new static($inSeconds);
+			return sprintf($absoluteString, $absoluteTime->i18nFormat($format));
 		}
 
 		// If more than a week, then take into account the length of months

+ 6 - 6
tests/TestCase/Utility/TimeTest.php

@@ -277,19 +277,19 @@ class TimeTest extends TestCase {
  */
 	public function testTimeAgoInWordsWithFormat() {
 		$time = new Time('2007-9-25');
-		$result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('format' => 'YYYY-MM-dd'));
 		$this->assertEquals('on 2007-09-25', $result);
 
 		$time = new Time('2007-9-25');
-		$result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('format' => 'YYYY-MM-dd'));
 		$this->assertEquals('on 2007-09-25', $result);
 
 		$time = new Time('+2 weeks +2 days');
-		$result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('format' => 'YYYY-MM-dd'));
 		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
 
 		$time = new Time('+2 months +2 days');
-		$result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'YYYY-MM-dd'));
 		$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
 	}
 
@@ -308,7 +308,7 @@ class TimeTest extends TestCase {
 		$this->assertEquals('2 months, 2 days ago', $result);
 
 		$time = new Time('-2 months -2 days');
-		$result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'YYYY-MM-dd'));
 		$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
 
 		$time = new Time('-2 years -5 months -2 days');
@@ -316,7 +316,7 @@ class TimeTest extends TestCase {
 		$this->assertEquals('2 years, 5 months, 2 days ago', $result);
 
 		$time = new Time('-2 weeks -2 days');
-		$result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
+		$result = $time->timeAgoInWords(array('format' => 'YYYY-MM-dd'));
 		$this->assertEquals('2 weeks, 2 days ago', $result);
 
 		$time = new Time('-3 years -12 months');