Permalink
Browse files

Adds two new time testing methods: 'isFuture' and 'isPast'

TestCases included
  • Loading branch information...
1 parent 3fc627c commit 632c4feb5d671da3f99bf6dbac671b03fed9ed8e @robertofrega robertofrega committed with markstory Mar 20, 2013
Showing with 76 additions and 0 deletions.
  1. +50 −0 lib/Cake/Test/Case/Utility/CakeTimeTest.php
  2. +26 −0 lib/Cake/Utility/CakeTime.php
@@ -596,6 +596,56 @@ public function testIsToday() {
$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);
+ }
+
+/**
+ * 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);
+ }
/**
* testIsThisWeek method
@@ -465,6 +465,32 @@ public static function isToday($dateString, $timezone = null) {
$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.
+ *
+ * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
+ * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
+ * @return boolean True if datetime string is in the future
+ * @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());
+ }
+
+/**
+ * Returns true if given datetime string is in the past.
+ *
+ * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
+ * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
+ * @return boolean True if datetime string is in the past
+ * @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());
+ }
/**
* Returns true if given datetime string is within this week.

0 comments on commit 632c4fe

Please sign in to comment.