2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "iCalRecurrenceCalculator.h"
24 @interface iCalMonthlyRecurrenceCalculator : iCalRecurrenceCalculator
27 #include <NGExtensions/NGCalendarDateRange.h>
28 #include "iCalRecurrenceRule.h"
29 #include "NSCalendarDate+ICal.h"
32 @interface iCalRecurrenceCalculator(PrivateAPI)
33 - (NSCalendarDate *)lastInstanceStartDate;
36 @implementation iCalMonthlyRecurrenceCalculator
38 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
40 NSMutableArray *ranges;
42 NSCalendarDate *firStart, *rStart, *rEnd, *until;
43 unsigned i, count, interval;
46 firStart = [self->firstRange startDate];
47 timeZone = [firStart timeZone];
48 rStart = [_r startDate];
50 interval = [self->rrule repeatInterval];
51 until = [self lastInstanceStartDate]; // TODO: maybe replace
53 if ([self->rrule byDayMask] != 0) {
54 [self errorWithFormat:@"cannot process byday part of rrule: %@",
59 /* check whether the range to be processed is beyond the 'until' date */
62 if ([until compare:rStart] == NSOrderedAscending) /* until before start */
64 if ([until compare:rEnd] == NSOrderedDescending) /* end before until */
65 rEnd = until; // TODO: why is that? end is _before_ until?
68 // TODO: I think the diff is to skip recurrence which are before the
69 // requested range. Not sure whether this is actually possible, eg
70 // the repeatCount must be processed from the start.
71 diff = [firStart monthsBetweenDate:rStart];
72 if ((diff != 0) && [rStart compare:firStart] == NSOrderedAscending)
75 count = [rStart monthsBetweenDate:rEnd] + 1;
76 ranges = [NSMutableArray arrayWithCapacity:count];
77 for (i = 0 ; i < count; i++) {
78 NSCalendarDate *start, *end;
79 NGCalendarDateRange *r;
87 if ((test % interval) != 0)
90 start = [firStart dateByAddingYears:0 months:(diff + i) days:0];
91 [start setTimeZone:timeZone];
93 /* check whether we are still in the limits */
95 // TODO: I think we should check in here whether we succeeded the
96 // repeatCount. Currently we precalculate that info in the
97 // -lastInstanceStartDate method.
99 /* Note: the 'until' in the rrule is inclusive as per spec */
100 if ([until compare:start] == NSOrderedAscending) /* start after until */
101 break; /* Note: we assume that the algorithm is sequential */
104 /* create end date */
106 end = [start addTimeInterval:[self->firstRange duration]];
107 [end setTimeZone:timeZone];
109 /* create range and check whether its in the requested range */
111 r = [[NGCalendarDateRange alloc] initWithStartDate:start endDate:end];
112 if ([_r containsDateRange:r])
113 [ranges addObject:r];
114 [r release]; r = nil;
119 - (NSCalendarDate *)lastInstanceStartDate {
120 if ([self->rrule repeatCount] > 0) {
121 NSCalendarDate *until;
122 unsigned months, interval;
124 interval = [self->rrule repeatInterval];
125 months = ([self->rrule repeatCount] - 1) * interval;
126 until = [[self->firstRange startDate] dateByAddingYears:0
131 return [super lastInstanceStartDate];
134 @end /* iCalMonthlyRecurrenceCalculator */