Skip to content

Commit

Permalink
[jan] Change Horde_View_Helper_Date#distanceOfTimeInWords() to not ro…
Browse files Browse the repository at this point in the history
…und up the distance.
  • Loading branch information
yunosh committed May 6, 2014
1 parent df7b73b commit ad162e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions framework/View/lib/Horde/View/Helper/Date.php
Expand Up @@ -31,8 +31,8 @@ class Horde_View_Helper_Date extends Horde_View_Helper_Base
public function distanceOfTimeInWords($fromTime, $toTime = 0,
$includeSeconds = false)
{
$distanceInMinutes = round(abs($toTime - $fromTime) / 60);
$distanceInSeconds = round(abs($toTime - $fromTime));
$distanceInMinutes = floor(abs($toTime - $fromTime) / 60);
$distanceInSeconds = floor(abs($toTime - $fromTime));

if ($distanceInMinutes >= 0 && $distanceInMinutes <= 1) {
if (!$includeSeconds) {
Expand Down
2 changes: 2 additions & 0 deletions framework/View/package.xml
Expand Up @@ -29,6 +29,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Change Horde_View_Helper_Date#distanceOfTimeInWords() to not round up the distance.
* [jan] Make Horde_View_Helper_Text#truncate() and truncateMiddle() multibyte-safe (Bug #13163).
</notes>
<contents>
Expand Down Expand Up @@ -439,6 +440,7 @@
<date>2013-08-22</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Change Horde_View_Helper_Date#distanceOfTimeInWords() to not round up the distance.
* [jan] Make Horde_View_Helper_Text#truncate() and truncateMiddle() multibyte-safe (Bug #13163).
</notes>
</release>
Expand Down
24 changes: 10 additions & 14 deletions framework/View/test/Horde/View/Helper/DateTest.php
Expand Up @@ -64,57 +64,57 @@ public function testDistanceInWords()
$this->helper->distanceOfTimeInWords($from, $from + 0));
$this->assertEquals('less than a minute',
$this->helper->distanceOfTimeInWords($from, $from + 29));
$this->assertEquals('1 minute',
$this->assertEquals('less than a minute',
$this->helper->distanceOfTimeInWords($from, $from + 30));
$this->assertEquals('1 minute',
$this->helper->distanceOfTimeInWords($from, $from + (1*60) + 29));

// 2..44
$this->assertEquals('2 minutes',
$this->helper->distanceOfTimeInWords($from, $from + (1*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (2*60) + 30));
$this->assertEquals('44 minutes',
$this->helper->distanceOfTimeInWords($from, $from + (44*60) + 29));

// 45..89
$this->assertEquals('about 1 hour',
$this->helper->distanceOfTimeInWords($from, $from + (44*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (45*60)));
$this->assertEquals('about 1 hour',
$this->helper->distanceOfTimeInWords($from, $from + (89*60) + 29));

// 90..1439
$this->assertEquals('about 2 hours',
$this->helper->distanceOfTimeInWords($from, $from + (89*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (90*60)));
$this->assertEquals('about 24 hours',
$this->helper->distanceOfTimeInWords($from, $from + (23*3600) + (59*60) + 29));

// 2880..43199
$this->assertEquals('2 days',
$this->helper->distanceOfTimeInWords($from, $from + (47*3600) + (59*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (47*3600) + (60*60)));
$this->assertEquals('29 days',
$this->helper->distanceOfTimeInWords($from, $from + (29*86400) + (23*3600) + (59*60) + 29));

// 43200..86399
$this->assertEquals('about 1 month',
$this->helper->distanceOfTimeInWords($from, $from + (29*86400) + (23*3600) + (59*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (29*86400) + (23*3600) + (60*60)));
$this->assertEquals('about 1 month',
$this->helper->distanceOfTimeInWords($from, $from + (59*86400) + (23*3600) + (59*60) + 29));

// 86400..525599
$this->assertEquals('2 months',
$this->helper->distanceOfTimeInWords($from, $from + (59*86400) + (23*3600) + (59*60) + 30));
$this->helper->distanceOfTimeInWords($from, $from + (59*86400) + (23*3600) + (60*60)));

$this->assertEquals('12 months',
$this->helper->distanceOfTimeInWords($from, $from + (1*31557600) - 31));

// 525960..1051919
$this->assertEquals('about 1 year',
$this->helper->distanceOfTimeInWords($from, $from + (1*31557600) - 30));
$this->helper->distanceOfTimeInWords($from, $from + (1*31557600)));
$this->assertEquals('about 1 year',
$this->helper->distanceOfTimeInWords($from, $from + (2*31557600) - 31));

// > 1051920
$this->assertEquals('over 2 years',
$this->helper->distanceOfTimeInWords($from, $from + (2*31557600) - 30));
$this->helper->distanceOfTimeInWords($from, $from + (2*31557600)));
$this->assertEquals('over 10 years',
$this->helper->distanceOfTimeInWords($from, $from + (10*31557600)));

Expand All @@ -127,11 +127,7 @@ public function testDistanceInWords()

public function testDistanceInWordsWithIntegers()
{
$this->markTestIncomplete('not yet passing');

$from = mktime(21, 45, 0, 6, 6, 2004);

// test with integers (not yet passing)
// test with integers
$this->assertEquals('less than a minute',
$this->helper->distanceOfTimeInWords(59));
$this->assertEquals('about 1 hour',
Expand Down

0 comments on commit ad162e7

Please sign in to comment.