2 Copyright (C) 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 <NGiCal/iCalRecurrenceRule.h>
23 #include <NGiCal/iCalRecurrenceCalculator.h>
24 #include <NGExtensions/NGCalendarDateRange.h>
27 static NSCalendarDate *dateForString(NSString *_s) {
28 // copied from ogo-chkaptconflicts, maybe move to NGExtensions?
29 static NSCalendarDate *now = nil;
30 static NSCalendarDate *mon = nil;
32 if (now == nil) now = [[NSCalendarDate date] retain];
33 if (mon == nil) mon = [[now mondayOfWeek] retain];
34 _s = [_s lowercaseString];
36 if ([_s isEqualToString:@"now"]) return now;
37 if ([_s isEqualToString:@"tomorrow"]) return [now tomorrow];
38 if ([_s isEqualToString:@"yesterday"]) return [now yesterday];
40 if ([_s hasPrefix:@"mon"]) return mon;
41 if ([_s hasPrefix:@"tue"]) return [mon dateByAddingYears:0 months:0 days:1];
42 if ([_s hasPrefix:@"wed"]) return [mon dateByAddingYears:0 months:0 days:2];
43 if ([_s hasPrefix:@"thu"]) return [mon dateByAddingYears:0 months:0 days:3];
44 if ([_s hasPrefix:@"fri"]) return [mon dateByAddingYears:0 months:0 days:4];
45 if ([_s hasPrefix:@"sat"]) return [mon dateByAddingYears:0 months:0 days:5];
46 if ([_s hasPrefix:@"sun"]) return [mon dateByAddingYears:0 months:0 days:6];
48 switch ([_s length]) {
50 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m"];
52 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d"];
54 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%-m-%d"];
56 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H%M"];
58 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H:%M"];
60 return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y-%m-%d %H:%M"];
66 static int usage(NSArray *args) {
68 "usage: %s <rrule> <startdate> <enddate> <cycleend>\n"
71 " %s 'FREQ=MONTHLY;BYDAY=2TU' '20050901 14:00' '20050901 15:00' "
73 [[args objectAtIndex:0] cString],
74 [[args objectAtIndex:0] cString]);
78 static void printInstances(NSArray *instances) {
81 if ((count = [instances count]) == 0) {
82 printf("no reccurrences in given range\n");
86 for (i = 0; i < count; i++) {
87 NGCalendarDateRange *instance;
90 instance = [instances objectAtIndex:i];
92 s = [[instance startDate] descriptionWithCalendarFormat:
93 @"%a, %Y-%m-%d at %H:%M"];
94 printf("%s - ", [s cString]);
96 s = [[instance endDate] descriptionWithCalendarFormat:
97 [[instance startDate] isDateOnSameDay:
100 : @"%a, %Y-%m-%d at %H:%M"];
101 printf("%s\n", [s cString]);
105 static int runIt(NSArray *args) {
106 iCalRecurrenceCalculator *cpu;
107 iCalRecurrenceRule *rrule;
108 NGCalendarDateRange *startRange, *calcRange;
109 NSCalendarDate *from, *to, *cycleTo;
113 if ([args count] < 5)
116 pattern = [args objectAtIndex:1];
117 from = dateForString([args objectAtIndex:2]);
118 to = dateForString([args objectAtIndex:3]);
119 cycleTo = dateForString([args objectAtIndex:4]);
121 if (from == nil || to == nil || cycleTo == nil || ![pattern isNotEmpty])
125 [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:to];
128 [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:cycleTo];
132 if ((rrule = [[iCalRecurrenceRule alloc] initWithString:pattern]) == nil) {
134 fprintf(stderr, "error: could not parse reccurence rule: '%s'\n",
139 NSLog(@"from: %@ to: %@, cycle %@", from, to, cycleTo);
140 NSLog(@"rrule: %@", rrule);
144 cpu = [iCalRecurrenceCalculator
145 recurrenceCalculatorForRecurrenceRule:rrule
146 withFirstInstanceCalendarDateRange:startRange];
148 instances = [cpu recurrenceRangesWithinCalendarDateRange:calcRange];
149 printInstances(instances);
154 int main(int argc, char **argv, char **env) {
155 NSAutoreleasePool *pool;
158 pool = [[NSAutoreleasePool alloc] init];
159 #if LIB_FOUNDATION_LIBRARY
160 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
163 rc = runIt([[NSProcessInfo processInfo] argumentsWithoutDefaults]);