Permalink
Browse files

Fixes to make TimeHelper and test cases more compatible with PHP 4

  • Loading branch information...
1 parent 4c0a6d9 commit 3bd4cc07a6af7d102cfafc90ae3195d58dbd0836 @lorenzo lorenzo committed Jan 15, 2010
Showing with 35 additions and 33 deletions.
  1. +3 −0 cake/libs/view/helpers/time.php
  2. +32 −33 cake/tests/cases/libs/view/helpers/time.test.php
View
3 cake/libs/view/helpers/time.php
@@ -174,6 +174,9 @@ function fromString($dateString, $userOffset = null) {
if ($userOffset !== null) {
return $this->convert($date, $userOffset);
}
+ if ($date === -1) {
+ return false;
+ }
return $date;
}
View
65 cake/tests/cases/libs/view/helpers/time.test.php
@@ -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 ++) {
@@ -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';
}
@@ -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';
}
@@ -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');
@@ -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'));

0 comments on commit 3bd4cc0

Please sign in to comment.