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 #import <Foundation/NSDate.h>
23 #import <Foundation/NSArray.h>
25 #import <NGExtensions/NSCalendarDate+misc.h>
27 #import "iCalRecurrenceCalculator.h"
29 @interface iCalDailyRecurrenceCalculator : iCalRecurrenceCalculator
32 #import <NGExtensions/NGCalendarDateRange.h>
33 #import "iCalRecurrenceRule.h"
34 #import "NSCalendarDate+ICal.h"
36 @interface iCalRecurrenceCalculator(PrivateAPI)
37 - (NSCalendarDate *)lastInstanceStartDate;
40 @implementation iCalDailyRecurrenceCalculator
42 - (NSArray *)recurrenceRangesWithinCalendarDateRange:(NGCalendarDateRange *)_r{
43 NSMutableArray *ranges;
44 NSCalendarDate *firStart;
45 long i, jnFirst, jnStart, jnEnd, startEndCount;
48 firStart = [self->firstRange startDate];
49 jnFirst = [firStart julianNumber];
50 jnEnd = [[_r endDate] julianNumber];
55 jnStart = [[_r startDate] julianNumber];
56 interval = [self->rrule repeatInterval];
58 /* if rule is bound, check the bounds */
59 if (![self->rrule isInfinite]) {
60 NSCalendarDate *until;
63 until = [self->rrule untilDate];
65 if ([until compare:[_r startDate]] == NSOrderedAscending)
67 jnRuleLast = [until julianNumber];
70 jnRuleLast = (interval * [self->rrule repeatCount])
72 if (jnRuleLast < jnStart)
75 /* jnStart < jnRuleLast < jnEnd ? */
76 if (jnEnd > jnRuleLast)
80 startEndCount = (jnEnd - jnStart) + 1;
81 ranges = [NSMutableArray arrayWithCapacity:startEndCount];
82 for (i = 0 ; i < startEndCount; i++) {
85 jnCurrent = jnStart + i;
86 if (jnCurrent >= jnFirst) {
89 jnTest = jnCurrent - jnFirst;
90 if ((jnTest % interval) == 0) {
91 NSCalendarDate *start, *end;
92 NGCalendarDateRange *r;
94 start = [NSCalendarDate dateForJulianNumber:jnCurrent];
95 [start setTimeZone:[firStart timeZone]];
96 start = [start hour: [firStart hourOfDay]
97 minute:[firStart minuteOfHour]
98 second:[firStart secondOfMinute]];
99 end = [start addTimeInterval:[self->firstRange duration]];
100 r = [NGCalendarDateRange calendarDateRangeWithStartDate:start
102 if ([_r containsDateRange:r])
103 [ranges addObject:r];
110 - (NSCalendarDate *)lastInstanceStartDate {
111 if ([self->rrule repeatCount] > 0) {
112 long jnFirst, jnRuleLast;
113 NSCalendarDate *firStart, *until;
115 firStart = [self->firstRange startDate];
116 jnFirst = [firStart julianNumber];
117 jnRuleLast = ([self->rrule repeatInterval] *
118 [self->rrule repeatCount]) +
120 until = [NSCalendarDate dateForJulianNumber:jnRuleLast];
121 until = [until hour: [firStart hourOfDay]
122 minute:[firStart minuteOfHour]
123 second:[firStart secondOfMinute]];
126 return [super lastInstanceStartDate];
129 @end /* iCalDailyRecurrenceCalculator */