all repeater tests pass
authorChuck Hagenbuch <chuck@horde.org>
Sat, 7 Feb 2009 04:59:50 +0000 (23:59 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 7 Feb 2009 04:59:50 +0000 (23:59 -0500)
framework/Date_Parser/lib/Horde/Date/Repeater/Hour.php
framework/Date_Parser/lib/Horde/Date/Repeater/MonthName.php
framework/Date_Parser/lib/Horde/Date/Repeater/Time.php
framework/Date_Parser/lib/Horde/Date/Repeater/Week.php
framework/Date_Parser/lib/Horde/Date/Repeater/Weekend.php
framework/Date_Parser/lib/Horde/Date/Repeater/Year.php
framework/Date_Parser/lib/Horde/Date/Span.php
framework/Date_Parser/test/Horde/Date/Repeater/HourTest.php
framework/Date_Parser/test/Horde/Date/Repeater/TimeTest.php
framework/Date_Parser/test/Horde/Date/Repeater/YearTest.php

index 7472c07..e9ea26b 100644 (file)
@@ -30,13 +30,12 @@ class Horde_Date_Repeater_Hour extends Horde_Date_Repeater
 
         case 'past':
             $hourStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour));
-            $hourEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour + 1, 'min' => $this->now->min));
+            $hourEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour, 'min' => $this->now->min));
             break;
 
         case 'none':
             $hourStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour));
-            $hourEnd = clone($hourStart);
-            $hourEnd->hour++;
+            $hourEnd = $hourStart->add(array('hour' => 1));
             break;
         }
 
index e406f99..d7b355e 100644 (file)
@@ -17,26 +17,26 @@ class Horde_Date_Repeater_MonthName extends Horde_Date_Repeater
             $targetMonth = $this->_monthNumber($this->type);
             switch ($pointer) {
             case 'future':
-                if ($this->now->month > $targetMonth) {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth));
+                if ($this->now->month < $targetMonth) {
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth, 'day' => 1));
                 } else {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year + 1, 'month' => $targetMonth));
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year + 1, 'month' => $targetMonth, 'day' => 1));
                 }
                 break;
 
             case 'none':
                 if ($this->now->month <= $targetMonth) {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth));
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth, 'day' => 1));
                 } else {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year + 1, 'month' => $targetMonth));
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year + 1, 'month' => $targetMonth, 'day' => 1));
                 }
                 break;
 
             case 'past':
                 if ($this->now->month > $targetMonth) {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth));
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year, 'month' => $targetMonth, 'day' => 1));
                 } else {
-                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year - 1, 'month' => $targetMonth));
+                    $this->currentMonthStart = new Horde_Date(array('year' => $this->now->year - 1, 'month' => $targetMonth, 'day' => 1));
                 }
                 break;
             }
@@ -52,7 +52,7 @@ class Horde_Date_Repeater_MonthName extends Horde_Date_Repeater
             }
         }
 
-        return new Horde_Date_Span($this->currentMonthStart, new Horde_Date(array('year' => $this->currentMonthStart->year, 'month' => $this->currentMonthStart->month + 1)));
+        return new Horde_Date_Span($this->currentMonthStart, $this->currentMonthStart->add(array('month' => 1)));
     }
 
     public function this($pointer = 'future')
index cb1cc45..2afd38f 100644 (file)
@@ -68,31 +68,31 @@ class Horde_Date_Repeater_Time extends Horde_Date_Repeater
 
             if ($pointer == 'future') {
                 if ($this->type->ambiguous) {
-                    foreach (array($midnight + $this->type, $midnight + $halfDay + $this->type, $tomorrowMidnight + $this->type) as $t) {
-                        if ($t >= $this->now) {
+                    foreach (array($midnight->add($this->type->time), $midnight->add($halfDay + $this->type->time), $tomorrowMidnight->add($this->type->time)) as $t) {
+                        if ($t->compareDateTime($this->now) >= 0) {
                             $this->currentTime = $t;
                             break;
                         }
                     }
                 } else {
-                    foreach (array($midnight + $this->type, $tomorrowMidnight + $this->type) as $t) {
-                        if ($t >= $this->now) {
+                    foreach (array($midnight->add($this->type->time), $tomorrowMidnight->add($this->type->time)) as $t) {
+                        if ($t->compareDateTime($this->now) >= 0) {
                             $this->currentTime = $t;
                             break;
                         }
                     }
                 }
-            } else {
+            } elseif ($pointer == 'past') {
                 if ($this->type->ambiguous) {
-                    foreach (array($midnight + $halfDay + $this->type, $midnight + $this->type, $yesterdayMidnight + $this->type * 2) as $t) {
-                        if ($t <= $this->now) {
+                    foreach (array($midnight->add($halfDay + $this->type->time), $midnight->add($this->type->time), $yesterdayMidnight->add($this->type->time * 2)) as $t) {
+                        if ($t->compareDateTime($this->now) <= 0) {
                             $this->currentTime = $t;
                             break;
                         }
                     }
                 } else {
-                    foreach (array($midnight + $this->type, $yesterdayMidnight + $this->type) as $t) {
-                        if ($t <= $this->now) {
+                    foreach (array($midnight->add($this->type->time), $yesterdayMidnight->add($this->type->time)) as $t) {
+                        if ($t->compareDateTime($this->now) <= 0) {
                             $this->currentTime = $t;
                             break;
                         }
@@ -106,11 +106,11 @@ class Horde_Date_Repeater_Time extends Horde_Date_Repeater
         }
 
         if (!$first) {
-            $increment = $this->type->ambiguous ? $halfday : $fullDay;
-            $this->currentTime += ($pointer == 'future') ? $increment : -$increment;
+            $increment = $this->type->ambiguous ? $halfDay : $fullDay;
+            $this->currentTime->sec += ($pointer == 'future') ? $increment : -$increment;
         }
 
-        return new Horde_Date_Span($this->currentTime, $this->currentTime + $this->width());
+        return new Horde_Date_Span($this->currentTime, $this->currentTime->add(1));
     }
 
     public function this($context = 'future')
@@ -149,12 +149,6 @@ class Horde_Date_Tick
         return new Horde_Date_Tick($this->time * $other, $this->ambiguous);
     }
 
-    /*
-    def to_f
-      @time.to_f
-    end
-    */
-
     public function __toString()
     {
         return $this->time . ($this->ambiguous ? '?' : '');
index 9b80b78..a88623e 100644 (file)
@@ -15,19 +15,19 @@ class Horde_Date_Repeater_Week extends Horde_Date_Repeater
         if (!$this->currentWeekStart) {
             switch ($pointer) {
             case 'future':
-                $sundayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SUNDAY);
+                $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
                 $sundayRepeater->now = $this->now;
                 $nextSundaySpan = $sundayRepeater->next('future');
                 $this->currentWeekStart = $nextSundaySpan->begin;
                 break;
 
             case 'past':
-                $sundayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SUNDAY);
+                $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
                 $sundayRepeater->now = clone($this->now);
                 $sundayRepeater->now->day++;
                 $sundayRepeater->next('past');
                 $lastSundaySpan = $sundayRepeater->next('past');
-                $currentWeekStart = $lastSundaySpan->begin;
+                $this->currentWeekStart = $lastSundaySpan->begin;
                 break;
             }
         } else {
@@ -35,9 +35,7 @@ class Horde_Date_Repeater_Week extends Horde_Date_Repeater
             $this->currentWeekStart->day += $direction * 7;
         }
 
-        $end = clone($this->currentWeekStart);
-        $end->day += 7;
-        return new Horde_Date_Span($this->currentWeekStart, $end);
+        return new Horde_Date_Span($this->currentWeekStart, $this->currentWeekStart->add(array('day' => 7)));
     }
 
     public function this($pointer = 'future')
@@ -46,23 +44,23 @@ class Horde_Date_Repeater_Week extends Horde_Date_Repeater
 
         switch ($pointer) {
         case 'future':
-            $thisWeekStart = new Horde_Date(array('year' => $now->year, 'month' => $now->month, 'day' => $now->day, 'hour' => $now->hour + 1));
-            $sundayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SUNDAY);
+            $thisWeekStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour + 1));
+            $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
             $sundayRepeater->now = $this->now;
             $thisSundaySpan = $sundayRepeater->this('future');
             $thisWeekEnd = $thisSundaySpan->begin;
             return new Horde_Date_Span($thisWeekStart, $thisWeekEnd);
 
         case 'past':
-            $thisWeekEnd = new Horde_Date(array('year' => $now->year, 'month' => $now->month, 'day' => $now->day, 'hour' => $now->hour));
-            $sundayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SUNDAY);
+            $thisWeekEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'hour' => $this->now->hour));
+            $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
             $sundayRepeater->now = $this->now;
             $lastSundaySpan = $sundayRepeater->next('past');
             $thisWeekStart = $lastSundaySpan->begin;
             return new Horde_Date_Span($thisWeekStart, $thisWeekEnd);
 
         case 'none':
-            $sundayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SUNDAY);
+            $sundayRepeater = new Horde_Date_Repeater_DayName('sunday');
             $sundayRepeater->now = $this->now;
             $lastSundaySpan = $sundayRepeater->next('past');
             $thisWeekStart = $lastSundaySpan->begin;
index edc48fa..41024b6 100644 (file)
@@ -12,17 +12,17 @@ class Horde_Date_Repeater_Weekend extends Horde_Date_Repeater
     {
         parent::next($pointer);
 
-        if ($this->currentWeekStart) {
+        if (!$this->currentWeekStart) {
             switch ($pointer) {
             case 'future':
-                $saturdayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SATURDAY);
+                $saturdayRepeater = new Horde_Date_Repeater_DayName('saturday');
                 $saturdayRepeater->now = $this->now;
                 $nextSaturdaySpan = $saturdayRepeater->next('future');
                 $this->currentWeekStart = $nextSaturdaySpan->begin;
                 break;
 
             case 'past':
-                $saturdayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SATURDAY);
+                $saturdayRepeater = new Horde_Date_Repeater_DayName('saturday');
                 $saturdayRepeater->now = $this->now;
                 $saturdayRepeater->now->day++;
                 $lastSaturdaySpan = $saturdayRepeater->next('past');
@@ -34,9 +34,7 @@ class Horde_Date_Repeater_Weekend extends Horde_Date_Repeater
             $this->currentWeekStart->day += $direction * 7;
         }
 
-        $currentWeekEnd = clone($this->currentWeekStart);
-        $currentWeekEnd->day += 2;
-        return new Horde_Date_Span($this->currentWeekStart, $currentWeekEnd);
+        return new Horde_Date_Span($this->currentWeekStart, $this->currentWeekStart->add(array('day' => 2)));
     }
 
     public function this($pointer = 'future')
@@ -46,32 +44,27 @@ class Horde_Date_Repeater_Weekend extends Horde_Date_Repeater
         switch ($pointer) {
         case 'future':
         case 'none':
-            $saturdayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SATURDAY);
+            $saturdayRepeater = new Horde_Date_Repeater_DayName('saturday');
             $saturdayRepeater->now = $this->now;
             $thisSaturdaySpan = $saturdayRepeater->this('future');
-            $thisSaturdaySpanEnd = $thisSaturdaySpan->begin;
-            $thisSaturdaySpanEnd->day += 2;
-            return new Horde_Date_Span($thisSaturdaySpan->begin, $thisSaturdaySpanEnd);
+            return new Horde_Date_Span($thisSaturdaySpan->begin, $thisSaturdaySpan->begin->add(array('day' => 2)));
 
         case 'past':
-            $saturdayRepeater = new Horde_Date_Repeater_DayName(Horde_Date::DATE_SATURDAY);
+            $saturdayRepeater = new Horde_Date_Repeater_DayName('saturday');
             $saturdayRepeater->now = $this->now;
             $lastSaturdaySpan = $saturdayRepeater->this('past');
-            $lastSaturdaySpanEnd = $lastSaturdaySpan->begin;
-            $lastSaturdaySpanEnd->day += 2;
-            return new Horde_Date_Span($lastSaturdaySpan->begin, $lastSaturdaySpanEnd);
+            return new Horde_Date_Span($lastSaturdaySpan->begin, $lastSaturdaySpan->begin->add(array('day' => 2)));
         }
     }
 
     public function offset($span, $amount, $pointer)
     {
         $direction = ($pointer == 'future') ? 1 : -1;
-        $weekend = new Horde_Date_Repeater_Weekend('weekend');
-        $weekend->now = $span->begin;
+        $weekend = new self();
+        $weekend->now = clone($span->begin);
         $start = $weekend->next($pointer)->begin;
         $start->day += ($amount - 1) * $direction * 7;
-        // @FIXME
-        return new Horde_Date_Span($start, $start + ($span->end - $span->begin));
+        return new Horde_Date_Span($start, $start->add($span->width()));
     }
 
     public function width()
index ace5cdc..e31207d 100644 (file)
@@ -8,21 +8,13 @@ class Horde_Date_Repeater_Year extends Horde_Date_Repeater
         parent::next($pointer);
 
         if (!$this->currentYearStart) {
-            switch ($pointer) {
-            case 'future':
-                $this->currentYearStart = new Horde_Date(array('year' => $this->now->year + 1));
-                break;
-
-            case 'past':
-                $this->currentYearStart = new Horde_Date(array('year' => $this->now->year - 1));
-                break;
-            }
-        } else {
-            $diff = ($pointer == 'future') ? 1 : -1;
-            $this->currentYearStart->year += $diff;
+            $this->currentYearStart = new Horde_Date(array('year' => $this->now->year, 'month' => 1, 'day' => 1));
         }
 
-        return new Horde_Date_Span($this->currentYearStart, new Horde_Date(array('year' => $this->currentYearStart->year + 1)));
+        $diff = ($pointer == 'future') ? 1 : -1;
+        $this->currentYearStart->year += $diff;
+
+        return new Horde_Date_Span($this->currentYearStart, $this->currentYearStart->add(array('year' => 1)));
     }
 
     public function this($pointer = 'future')
@@ -32,17 +24,17 @@ class Horde_Date_Repeater_Year extends Horde_Date_Repeater
         switch ($pointer) {
         case 'future':
             $thisYearStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1));
-            $thisYearEnd = new Horde_Date(array('year' => $this->now->year + 1));
+            $thisYearEnd = new Horde_Date(array('year' => $this->now->year + 1, 'month' => 1, 'day' => 1));
             break;
 
         case 'past':
-            $thisYearStart = new Horde_Date(array('year' => $this->now->year));
+            $thisYearStart = new Horde_Date(array('year' => $this->now->year, 'month' => 1, 'day' => 1));
             $thisYearEnd = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day));
             break;
 
         case 'none':
-            $thisYearStart = new Horde_Date(array('year' => $this->now->year));
-            $thisYearEnd = new Horde_Date(array('year' => $this->now->year + 1));
+            $thisYearStart = new Horde_Date(array('year' => $this->now->year, 'month' => 1, 'day' => 1));
+            $thisYearEnd = new Horde_Date(array('year' => $this->now->year + 1, 'month' => 1, 'day' => 1));
             break;
         }
 
@@ -52,14 +44,7 @@ class Horde_Date_Repeater_Year extends Horde_Date_Repeater
     public function offset($span, $amount, $pointer)
     {
         $direction = ($pointer == 'future') ? 1 : -1;
-
-        $sb = clone($span->begin);
-        $sb->year += ($amount * $direction);
-
-        $se = clone($span->end);
-        $se->year += ($amount * $direction);
-
-        return new Horde_Date_Span($se, $sb);
+        return $span->add(array('year' => ($amount * $direction)));
     }
 
     public function width()
index 6acc5fe..11bea96 100644 (file)
@@ -31,7 +31,7 @@ class Horde_Date_Span
      */
     public function width()
     {
-        return $this->end->timestamp() - $this->begin->timestamp();
+        return abs($this->end->timestamp() - $this->begin->timestamp());
     }
 
     /**
index 85de9fb..aed7910 100644 (file)
@@ -65,8 +65,8 @@ class Horde_Date_Repeater_HourTest extends PHPUnit_Framework_TestCase
         $hours = new Horde_Date_Repeater_Hour();
 
         $offsetSpan = $hours->offset($span, 3, 'future');
-        $this->assertEquals('2006-08-16 16:00:00', (string)$offsetSpan->begin);
-        $this->assertEquals('2006-08-16 17:00:00', (string)$offsetSpan->end);
+        $this->assertEquals('2006-08-16 17:00:00', (string)$offsetSpan->begin);
+        $this->assertEquals('2006-08-16 17:00:01', (string)$offsetSpan->end);
 
         $offsetSpan = $hours->offset($span, 24, 'past');
         $this->assertEquals('2006-08-15 14:00:00', (string)$offsetSpan->begin);
index 17e26f6..4fd84ea 100644 (file)
@@ -22,35 +22,33 @@ class Horde_Date_Repeater_TimeTest extends PHPUnit_Framework_TestCase
         $t = new Horde_Date_Repeater_Time('4:00');
         $t->now = $this->now;
 
-        $this->assertEquals('2006-08-16 16:00:00', $t->next('future')->begin);
-        $this->assertEquals('2006-08-17 04:00:00', $t->next('future')->begin);
+        $this->assertEquals('2006-08-16 16:00:00', (string)$t->next('future')->begin);
+        $this->assertEquals('2006-08-17 04:00:00', (string)$t->next('future')->begin);
 
         $t = new Horde_Date_Repeater_Time('13:00');
         $t->now = $this->now;
 
-        $this->assertEquals('2006-08-17 13:00:00', $t->next('future')->begin);
-        $this->assertEquals('2006-08-18 13:00:00', $t->next('future')->begin);
+        $this->assertEquals('2006-08-17 13:00:00', (string)$t->next('future')->begin);
+        $this->assertEquals('2006-08-18 13:00:00', (string)$t->next('future')->begin);
 
         $t = new Horde_Date_Repeater_Time('0400');
         $t->now = $this->now;
 
-        $this->assertEquals('2006-08-17 04:00:00', $t->next('future')->begin);
-        $this->assertEquals('2006-08-18 04:00:00', $t->next('future')->begin);
+        $this->assertEquals('2006-08-17 04:00:00', (string)$t->next('future')->begin);
+        $this->assertEquals('2006-08-18 04:00:00', (string)$t->next('future')->begin);
     }
 
     public function testNextPast()
     {
         $t = new Horde_Date_Repeater_Time('4:00');
         $t->now = $this->now;
-
-        $this->assertEquals('2006-08-16 04:00:00', $t->next('past')->begin);
-        $this->assertEquals('2006-08-15 16:00:00', $t->next('past')->begin);
+        $this->assertEquals('2006-08-16 04:00:00', (string)$t->next('past')->begin);
+        $this->assertEquals('2006-08-15 16:00:00', (string)$t->next('past')->begin);
 
         $t = new Horde_Date_Repeater_Time('13:00');
         $t->now = $this->now;
-
-        $this->assertEquals('2006-08-16 13:00:00', $t->next('past')->begin);
-        $this->assertEquals('2006-08-15 13:00:00', $t->next('past')->begin);
+        $this->assertEquals('2006-08-16 13:00:00', (string)$t->next('past')->begin);
+        $this->assertEquals('2006-08-15 13:00:00', (string)$t->next('past')->begin);
     }
 
     public function testType()
index 776e04f..0cedf78 100644 (file)
@@ -40,7 +40,7 @@ class Horde_Date_Repeater_YearTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('2005-01-01', $lastYear->begin->format('Y-m-d'));
         $this->assertEquals('2006-01-01', $lastYear->end->format('Y-m-d'));
 
-        $lastLastYear = $years->last('future');
+        $lastLastYear = $years->next('past');
         $this->assertEquals('2004-01-01', $lastLastYear->begin->format('Y-m-d'));
         $this->assertEquals('2005-01-01', $lastLastYear->end->format('Y-m-d'));
     }