]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSCalendarDate+matrix.m
fixed a bug in plist comment parsing
[sope] / sope-core / NGExtensions / FdExt.subproj / NSCalendarDate+matrix.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NSCalendarDate+misc.h"
23 #include "common.h"
24
25 @implementation NSCalendarDate(CalMatrix)
26
27 static BOOL debugCalMatrix = NO;
28
29 - (NSArray *)calendarMatrixWithStartDayOfWeek:(short)_caldow
30   onlyCurrentMonth:(BOOL)_onlyThisMonth
31 {
32   // Note: we keep clock time!
33   NSAutoreleasePool *pool;
34   NSCalendarDate *firstInMonth;
35   NSArray        *matrix;
36   NSArray        *weeks[8] = { nil, nil, nil, nil, nil, nil, nil, nil };
37   NSCalendarDate *week[8]  = { nil, nil, nil, nil, nil, nil, nil, nil };
38   unsigned firstDoW, numDaysInLastMonth, i, j, curday, curweek, curmonth;
39
40   /* all the date operations use autorelease, so we wrap it in a pool */
41   pool = [[NSAutoreleasePool alloc] init];
42
43   if (debugCalMatrix)
44     NSLog(@"calmatrix for: %@", self);
45   
46   firstInMonth = [[self firstDayOfMonth] beginOfDay];
47   firstDoW     = [firstInMonth dayOfWeek];
48   curmonth     = [firstInMonth monthOfYear];
49   
50   numDaysInLastMonth = (firstDoW < _caldow)
51     ? (firstDoW + 7 - _caldow)
52     : (firstDoW - _caldow);
53
54   if (debugCalMatrix) {
55     NSLog(@"  LAST: %d FIRST-DOW: %d START-DOW: %d", 
56           numDaysInLastMonth, firstDoW, _caldow);
57   }
58
59   
60   /* first week */
61   
62   if (_onlyThisMonth) {
63     j = 0; /* this is the position where first week days are added */
64   }
65   else {
66     /* add dates from last month */
67     for (i = numDaysInLastMonth; i > 0; i--) {
68       week[numDaysInLastMonth - i] = 
69         [firstInMonth dateByAddingYears:0 months:0 days:-i];
70     }
71     j = numDaysInLastMonth;
72   }
73   week[j] = firstInMonth; j++;
74   
75   for (i = numDaysInLastMonth + 1; i < 7; i++, j++) {
76     week[j] = [firstInMonth dateByAddingYears:0 months:0 
77                             days:(i - numDaysInLastMonth)];
78   }
79   curday  = 7 - numDaysInLastMonth;
80   curweek = 1;
81   if (debugCalMatrix)
82     NSLog(@"  current day after 1st week: %d, week: %d", curday, curweek);
83   
84   /* finish first week */
85   weeks[0] = [[NSArray alloc] initWithObjects:week count:j];
86
87
88   /* follow up weeks */
89
90   while (curweek < 7) {
91     BOOL foundNewMonth = NO;
92     
93     for (i = 0; i < 7; i++, curday++) {
94       week[i] = [firstInMonth dateByAddingYears:0 months:0 days:curday];
95
96       if (!foundNewMonth && curday > 27) {
97         foundNewMonth = ([week[i] monthOfYear] != curmonth) ? YES : NO;
98         if (foundNewMonth && _onlyThisMonth)
99           break;
100       }
101     }
102     
103     if (i > 0) {
104       weeks[curweek] = [[NSArray alloc] initWithObjects:week count:i];
105       curweek++;
106     }
107     if (foundNewMonth)
108       break;
109   }
110
111   
112   /* build final matrix */
113   
114   matrix = [[NSArray alloc] initWithObjects:weeks count:curweek];
115   for (i = 0; i < 8; i++) {
116     [weeks[i] release];
117     weeks[i] = nil;
118   }
119   
120   if (debugCalMatrix)
121     NSLog(@"matrix for %@: %@", self, matrix);
122   
123   [pool release];
124   return [matrix autorelease];
125 }
126
127 @end /* NSCalendarDate(CalMatrix) */