From 38377420f9df74a4522661cb154d06e0e4cd4215 Mon Sep 17 00:00:00 2001 From: helge Date: Mon, 19 Sep 2005 22:42:22 +0000 Subject: [PATCH] added rule calculation git-svn-id: http://svn.opengroupware.org/SOPE/trunk@1118 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-ical/samples/ievalrrule.m | 113 +++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 13 deletions(-) diff --git a/sope-ical/samples/ievalrrule.m b/sope-ical/samples/ievalrrule.m index 1ce1111d..41fd4d3d 100644 --- a/sope-ical/samples/ievalrrule.m +++ b/sope-ical/samples/ievalrrule.m @@ -20,36 +20,114 @@ */ #include +#include +#include #include "common.h" +static NSCalendarDate *dateForString(NSString *_s) { + // copied from ogo-chkaptconflicts, maybe move to NGExtensions? + static NSCalendarDate *now = nil; + static NSCalendarDate *mon = nil; + + if (now == nil) now = [[NSCalendarDate date] retain]; + if (mon == nil) mon = [[now mondayOfWeek] retain]; + _s = [_s lowercaseString]; + + if ([_s isEqualToString:@"now"]) return now; + if ([_s isEqualToString:@"tomorrow"]) return [now tomorrow]; + if ([_s isEqualToString:@"yesterday"]) return [now yesterday]; + + if ([_s hasPrefix:@"mon"]) return mon; + if ([_s hasPrefix:@"tue"]) return [mon dateByAddingYears:0 months:0 days:1]; + if ([_s hasPrefix:@"wed"]) return [mon dateByAddingYears:0 months:0 days:2]; + if ([_s hasPrefix:@"thu"]) return [mon dateByAddingYears:0 months:0 days:3]; + if ([_s hasPrefix:@"fri"]) return [mon dateByAddingYears:0 months:0 days:4]; + if ([_s hasPrefix:@"sat"]) return [mon dateByAddingYears:0 months:0 days:5]; + if ([_s hasPrefix:@"sun"]) return [mon dateByAddingYears:0 months:0 days:6]; + + switch ([_s length]) { + case 6: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m"]; + case 8: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d"]; + case 10: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%-m-%d"]; + case 13: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H%M"]; + case 14: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y%m%d %H:%M"]; + case 16: + return [NSCalendarDate dateWithString:_s calendarFormat:@"%Y-%m-%d %H:%M"]; + default: + return nil; + } +} + static int usage(NSArray *args) { fprintf(stderr, - "usage: %s \n" + "usage: %s \n" "\n" "sample:\n" - " %s 'FREQ=MONTHLY;BYDAY=2TU' 20050901 20060301\n", + " %s 'FREQ=MONTHLY;BYDAY=2TU' '20050901 14:00' '20050901 15:00' " + "20060921\n", [[args objectAtIndex:0] cString], [[args objectAtIndex:0] cString]); return 1; } +static void printInstances(NSArray *instances) { + unsigned i, count; + + if ((count = [instances count]) == 0) { + printf("no reccurrences in given range\n"); + return; + } + + for (i = 0; i < count; i++) { + NGCalendarDateRange *instance; + NSString *s; + + instance = [instances objectAtIndex:i]; + + s = [[instance startDate] descriptionWithCalendarFormat: + @"%a, %Y-%m-%d at %H:%M"]; + printf("%s - ", [s cString]); + + s = [[instance endDate] descriptionWithCalendarFormat: + [[instance startDate] isDateOnSameDay: + [instance endDate]] + ? @"%H:%M" + : @"%a, %Y-%m-%d at %H:%M"]; + printf("%s\n", [s cString]); + } +} + static int runIt(NSArray *args) { - iCalRecurrenceRule *rrule; - NSCalendarDate *from, *to; - NSString *pattern; - NSString *s; + iCalRecurrenceCalculator *cpu; + iCalRecurrenceRule *rrule; + NGCalendarDateRange *startRange, *calcRange; + NSCalendarDate *from, *to, *cycleTo; + NSString *pattern; + NSArray *instances; - if ([args count] < 4) + if ([args count] < 5) return usage(args); pattern = [args objectAtIndex:1]; - s = [[args objectAtIndex:2] stringByAppendingString:@" 00:00"]; - from = [NSCalendarDate dateWithString:s calendarFormat:@"%Y%m%d %H:%M"]; - s = [[args objectAtIndex:3] stringByAppendingString:@" 23:59"]; - to = [NSCalendarDate dateWithString:s calendarFormat:@"%Y%m%d %H:%M"]; + from = dateForString([args objectAtIndex:2]); + to = dateForString([args objectAtIndex:3]); + cycleTo = dateForString([args objectAtIndex:4]); - if (from == nil || to == nil || ![pattern isNotEmpty]) + if (from == nil || to == nil || cycleTo == nil || ![pattern isNotEmpty]) return usage(args); + + startRange = + [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:to]; + + calcRange = + [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:cycleTo]; + + /* parse rrule */ if ((rrule = [[iCalRecurrenceRule alloc] initWithString:pattern]) == nil) { usage(args); @@ -58,9 +136,18 @@ static int runIt(NSArray *args) { return 2; } - NSLog(@"from: %@ to: %@", from, to); + NSLog(@"from: %@ to: %@, cycle %@", from, to, cycleTo); NSLog(@"rrule: %@", rrule); + /* calculate */ + + cpu = [iCalRecurrenceCalculator + recurrenceCalculatorForRecurrenceRule:rrule + withFirstInstanceCalendarDateRange:startRange]; + + instances = [cpu recurrenceRangesWithinCalendarDateRange:calcRange]; + printInstances(instances); + return 0; } -- 2.39.5