Fix skipping weeks for recurring events that do not occur every other week.
authorGunnar Wrobel <p@rdus.de>
Mon, 9 Nov 2009 16:27:36 +0000 (17:27 +0100)
committerGunnar Wrobel <p@rdus.de>
Mon, 9 Nov 2009 16:27:36 +0000 (17:27 +0100)
Bug: #8546

framework/Date/lib/Horde/Date/Recurrence.php
framework/Date/test/Horde/Date/RecurrenceTest.php

index b6e83ea..3d9ffd9 100644 (file)
@@ -400,11 +400,25 @@ class Horde_Date_Recurrence
             $after_week_end->mday += 7;
 
             $diff = $start_week->diff($after_week);
-            $recur = $diff + ($diff % ($this->recurInterval * 7));
+            $interval = $this->recurInterval * 7;
+            $repeats = floor($diff / $interval);
+            if ($diff % $interval < 7) {
+                $recur = $diff;
+            } else {
+                /**
+                 * If the after_week is not in the first week interval the
+                 * search needs to skip ahead a complete interval. The way it is
+                 * calculated here means that an event that occurs every second
+                 * week on Monday and Wednesday with the event actually starting
+                 * on Tuesday or Wednesday will only have one incidence in the
+                 * first week.
+                 */
+                $recur = $interval * ($repeats + 1);
+            }
 
             if ($this->hasRecurCount()) {
                 $recurrences = 0;
-                if ($recur > 0) {
+                if ($repeats > 0) {
                     $weekdays = $this->recurData;
                     $total_recurrences_per_week = 0;
                     while ($weekdays > 0) {
@@ -413,7 +427,7 @@ class Horde_Date_Recurrence
                         }
                         $weekdays = ($weekdays - ($weekdays % 2)) / 2;
                     }
-                    $recurrences += $total_recurrences_per_week * ($recur / ($this->recurInterval * 7));
+                    $recurrences += $total_recurrences_per_week * $repeats;
                 }
             }
 
index 3cc11fb..267a3e1 100644 (file)
@@ -32,7 +32,7 @@ class Horde_Date_RecurrenceTest extends PHPUnit_Framework_TestCase
         // This is a Thursday
         $next = new Horde_Date('2007-03-01 00:00:00');
         while ($next = $r->nextRecurrence($next)) {
-            if (++$protect > 10) {
+            if (++$protect > 20) {
                 return 'Infinite loop';
             }
             $recurrences[] = (string)$next;
@@ -109,7 +109,7 @@ class Horde_Date_RecurrenceTest extends PHPUnit_Framework_TestCase
                                 '2010-01-06 08:00:00',
                                 '2010-01-07 08:00:00',
                                 '2010-01-08 08:00:00',
-),
+                            ),
                             $this->_getRecurrences($r), 'Test for bug #8546');
     }