From d28c2f8e7285e814b7391314167803d60858ee53 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 30 Mar 2013 22:23:47 -0400 Subject: [PATCH] Fix code & tests for isPast() and isFuture() The methods and tests now follow coding standards and work. --- lib/Cake/Test/Case/Utility/CakeTimeTest.php | 56 ++++++++------------- lib/Cake/Utility/CakeTime.php | 12 ++--- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index 56f3e547c..bab883761 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -596,55 +596,39 @@ class CakeTimeTest extends CakeTestCase { $result = $this->Time->isToday('-1 day'); $this->assertFalse($result); } - + /** * testIsFuture method * * @return void */ public function testIsFuture() { - $result = $this->Time->isFuture('+1 month'); - $this->assertTrue($result); - $result = $this->Time->isFuture('+1 days'); - $this->assertTrue($result); - $result = $this->Time->isFuture('+1 minute'); - $this->assertTrue($result); - $result = $this->Time->isFuture('+1 second'); - $this->assertTrue($result); - - $result = $this->Time->isFuture('-1 second'); - $this->assertFalse($result); - $result = $this->Time->isFuture('-1 day'); - $this->assertFalse($result); - $result = $this->Time->isFuture('-1 week'); - $this->assertFalse($result); - $result = $this->Time->isFuture('-1 month'); - $this->assertFalse($result); + $this->assertTrue($this->Time->isFuture('+1 month')); + $this->assertTrue($this->Time->isFuture('+1 days')); + $this->assertTrue($this->Time->isFuture('+1 minute')); + $this->assertTrue($this->Time->isFuture('+1 second')); + + $this->assertFalse($this->Time->isFuture('-1 second')); + $this->assertFalse($this->Time->isFuture('-1 day')); + $this->assertFalse($this->Time->isFuture('-1 week')); + $this->assertFalse($this->Time->isFuture('-1 month')); } - + /** * testIsPast method * * @return void */ public function testIsPast() { - $result = $this->Time->isFuture('+1 month'); - $this->assertFalse($result); - $result = $this->Time->isFuture('+1 days'); - $this->assertFalse($result); - $result = $this->Time->isFuture('+1 minute'); - $this->assertFalse($result); - $result = $this->Time->isFuture('+1 second'); - $this->assertFalse($result); - - $result = $this->Time->isFuture('-1 second'); - $this->assertTrue($result); - $result = $this->Time->isFuture('-1 day'); - $this->assertTrue($result); - $result = $this->Time->isFuture('-1 week'); - $this->assertTrue($result); - $result = $this->Time->isFuture('-1 month'); - $this->assertTrue($result); + $this->assertFalse($this->Time->isPast('+1 month')); + $this->assertFalse($this->Time->isPast('+1 days')); + $this->assertFalse($this->Time->isPast('+1 minute')); + $this->assertFalse($this->Time->isPast('+1 second')); + + $this->assertTrue($this->Time->isPast('-1 second')); + $this->assertTrue($this->Time->isPast('-1 day')); + $this->assertTrue($this->Time->isPast('-1 week')); + $this->assertTrue($this->Time->isPast('-1 month')); } /** diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 30bf81366..15473b898 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -465,7 +465,7 @@ class CakeTime { $timestamp = self::fromString($dateString, $timezone); return date('Y-m-d', $timestamp) == date('Y-m-d', time()); } - + /** * Returns true if given datetime string is in the future. * @@ -475,10 +475,10 @@ class CakeTime { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time */ public static function isFuture($dateString, $timezone = null) { - $date = self::fromString($dateString, $timezone); - return date('Y-m-d H:i:s', $date) < date('Y-m-d H:i:s', time()); + $timestamp = self::fromString($dateString, $timezone); + return $timestamp > time(); } - + /** * Returns true if given datetime string is in the past. * @@ -488,8 +488,8 @@ class CakeTime { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time */ public static function isPast($dateString, $timezone = null) { - $date = self::fromString($dateString, $timezone); - return date('Y-m-d H:i:s', $date) > date('Y-m-d H:i:s', time()); + $timestamp = self::fromString($dateString, $timezone); + return $timestamp < time(); } /**