From 3bd4cc07a6af7d102cfafc90ae3195d58dbd0836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Fri, 15 Jan 2010 17:40:49 -0430 Subject: [PATCH] Fixes to make TimeHelper and test cases more compatible with PHP 4 --- cake/libs/view/helpers/time.php | 3 + .../cases/libs/view/helpers/time.test.php | 65 +++++++++---------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/cake/libs/view/helpers/time.php b/cake/libs/view/helpers/time.php index e4b9f7cb632..1431fd52c26 100644 --- a/cake/libs/view/helpers/time.php +++ b/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; } diff --git a/cake/tests/cases/libs/view/helpers/time.test.php b/cake/tests/cases/libs/view/helpers/time.test.php index 7b70d4ed72c..d90fd456212 100644 --- a/cake/tests/cases/libs/view/helpers/time.test.php +++ b/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."
"; - $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'));