Skip to content

Commit

Permalink
FIx WEEKLY interval recurrances
Browse files Browse the repository at this point in the history
  • Loading branch information
christiaangoossens committed Feb 8, 2023
1 parent 272251a commit 854ad71
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/IcalParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ public function parseRecurrences($event): array {
date_default_timezone_set($event['DTSTART']->getTimezone()->getName());
$frequency = new Freq($recurring->rrule, $event['DTSTART']->getTimestamp(), $exclusions, $additions);
$recurrenceTimestamps = $frequency->getAllOccurrences();

// This should be fixed in the Freq class, but it's too messy to make sense of
// This guard only works on WEEKLY, because the others have no fixed time interval
// There may still be a bug with the others
if (isset($event['RRULE']['INTERVAL']) && $recurring->getFreq() === "WEEKLY") {
$replacementList = [];

foreach($recurrenceTimestamps as $timestamp) {
$tmp = new DateTime('now', $event['DTSTART']->getTimezone());
$tmp->setTimestamp($timestamp);

$dayCount = $event['DTSTART']->diff($tmp)->format('%a');

if ($dayCount % ($event['RRULE']['INTERVAL'] * 7) == 0) {
$replacementList[] = $timestamp;
}
}

$recurrenceTimestamps = $replacementList;
}

$recurrences = [];
foreach ($recurrenceTimestamps as $recurrenceTimestamp) {
$tmp = new DateTime('now', $event['DTSTART']->getTimezone());
Expand Down
6 changes: 3 additions & 3 deletions tests/events.recurring.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ test('Recurring instances bi-weekly', function () {
// RRULE:FREQ=WEEKLY;WKST=MO;UNTIL=20230228T090000;INTERVAL=2;BYDAY=TU
Assert::equal(3, count($events[0]['RECURRENCES']));
Assert::equal(3, $events->count());
Assert::equal('1.31.2023 5:00:00', $events[0]['DTSTART']->format('j.n.Y H:i:s'));
Assert::equal('2.14.2023 5:00:00', $events[1]['DTSTART']->format('j.n.Y H:i:s'));
Assert::equal('2.28.2023 5:00:00', $events[2]['DTSTART']->format('j.n.Y H:i:s'));
Assert::equal('31.1.2023 05:00:00', $events[0]['DTSTART']->format('j.n.Y H:i:s'));
Assert::equal('14.2.2023 05:00:00', $events[1]['DTSTART']->format('j.n.Y H:i:s'));
Assert::equal('28.2.2023 05:00:00', $events[2]['DTSTART']->format('j.n.Y H:i:s'));
});

0 comments on commit 854ad71

Please sign in to comment.