Skip to content

Commit

Permalink
Fixes to make TimeHelper and test cases more compatible with PHP 4
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 15, 2010
1 parent 4c0a6d9 commit 3bd4cc0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
3 changes: 3 additions & 0 deletions cake/libs/view/helpers/time.php
Expand Up @@ -174,6 +174,9 @@ function fromString($dateString, $userOffset = null) {
if ($userOffset !== null) {
return $this->convert($date, $userOffset);
}
if ($date === -1) {
return false;
}
return $date;
}

Expand Down
65 changes: 32 additions & 33 deletions cake/tests/cases/libs/view/helpers/time.test.php
Expand Up @@ -86,34 +86,34 @@ function testToQuarter() {
* @return void
*/
function testTimeAgoInWords() {
$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 3 days'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +3 days'), array('end' => '8 years'), true);
$this->assertEqual($result, '4 months, 2 weeks, 3 days');

$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 2 days'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +2 days'), array('end' => '8 years'), true);
$this->assertEqual($result, '4 months, 2 weeks, 2 days');

$result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +1 day'), array('end' => '8 years'), true);
$this->assertEqual($result, '4 months, 2 weeks, 1 day');

$result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks +1 day'), array('end' => '8 years'), true);
$this->assertEqual($result, '3 months, 2 weeks, 1 day');

$result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks'), array('end' => '8 years'), true);
$this->assertEqual($result, '3 months, 2 weeks');

$result = $this->Time->timeAgoInWords(strtotime('3 months, 1 week, 6 days'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+3 months +1 week +6 days'), array('end' => '8 years'), true);
$this->assertEqual($result, '3 months, 1 week, 6 days');

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks +1 day'), array('end' => '8 years'), true);
$this->assertEqual($result, '2 months, 2 weeks, 1 day');

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks'), array('end' => '8 years'), true);
$this->assertEqual($result, '2 months, 2 weeks');

$result = $this->Time->timeAgoInWords(strtotime('2 months, 1 week, 6 days'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+2 months +1 week +6 days'), array('end' => '8 years'), true);
$this->assertEqual($result, '2 months, 1 week, 6 days');

$result = $this->Time->timeAgoInWords(strtotime('1 month, 1 week, 6 days'), array('end' => '8 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('+1 month +1 week +6 days'), array('end' => '8 years'), true);
$this->assertEqual($result, '1 month, 1 week, 6 days');

for($i = 0; $i < 200; $i ++) {
Expand Down Expand Up @@ -157,8 +157,8 @@ function testTimeAgoInWords() {
$relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
}

if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
$result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
$result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
if ($relative_date == '0 seconds') {
$relative_date = '0 seconds ago';
}
Expand Down Expand Up @@ -211,9 +211,8 @@ function testTimeAgoInWords() {
$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
}

if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
// echo $relative_date."<br />";
$result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') {
$result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true);
if ($relative_date == '0 seconds') {
$relative_date = '0 seconds ago';
}
Expand All @@ -223,7 +222,7 @@ function testTimeAgoInWords() {
}
}

$result = $this->Time->timeAgoInWords(strtotime('-2 years, -5 months, -2 days'), array('end' => '3 years'), true);
$result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true);
$this->assertEqual($result, '2 years, 5 months, 2 days ago');

$result = $this->Time->timeAgoInWords('2007-9-25');
Expand All @@ -235,44 +234,44 @@ function testTimeAgoInWords() {
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
$this->assertEqual($result, 'on 2007-09-25');

$result = $this->Time->timeAgoInWords(strtotime('-2 weeks, -2 days'), 'Y-m-d', false);
$result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false);
$this->assertEqual($result, '2 weeks, 2 days ago');

$result = $this->Time->timeAgoInWords(strtotime('2 weeks, 2 days'), 'Y-m-d', true);
$result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true);
$this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month'));
$this->assertEqual($result, 'on ' . date('j/n/y', strtotime('2 months, 2 days')));
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month'));
$this->assertEqual($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days')));

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
$this->assertPattern('/2 months/', $result);

$result = $this->Time->timeAgoInWords(strtotime('2 months, 12 days'), array('end' => '3 month'));
$result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month'));
$this->assertPattern('/2 months, 1 week/', $result);

$result = $this->Time->timeAgoInWords(strtotime('3 months, 5 days'), array('end' => '4 month'));
$result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month'));
$this->assertEqual($result, '3 months, 5 days');

$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
$this->assertEqual($result, '2 months, 2 days ago');

$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month'));
$this->assertEqual($result, '2 months, 2 days ago');

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month'));
$this->assertPattern('/2 months/', $result);

$result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('2 months, 2 days')));
$result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('+2 months +2 days')));

$result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months, -2 days')));
$result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
$this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days')));

$result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years'));
$result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years'));
$this->assertEqual($result, '1 year, 1 month, 5 days ago');

$fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4));
$result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4));
$fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4));
$result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4));
$this->assertEqual($fourHours, $result);

$result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
Expand Down

0 comments on commit 3bd4cc0

Please sign in to comment.