]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalTimeZonePeriod.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / iCalTimeZonePeriod.m
1 /* iCalTimeZonePeriod.m - this file is part of SOPE
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSCalendarDate.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSTimeZone.h>
26
27 #import "iCalDateTime.h"
28 #import "iCalRecurrenceRule.h"
29
30 #import "iCalTimeZonePeriod.h"
31
32 @implementation iCalTimeZonePeriod
33
34 - (Class) classForTag: (NSString *) classTag
35 {
36   Class tagClass;
37
38   if ([classTag isEqualToString: @"RRULE"])
39     tagClass = [iCalRecurrenceRule class];
40   else if ([classTag isEqualToString: @"DTSTART"])
41     tagClass = [iCalDateTime class];
42   else if ([classTag isEqualToString: @"TZNAME"]
43            || [classTag isEqualToString: @"TZOFFSETFROM"]
44            || [classTag isEqualToString: @"TZOFFSETTO"])
45     tagClass = [CardElement class];
46   else
47     tagClass = [super classForTag: classTag];
48
49   return tagClass;
50 }
51
52 - (int) _secondsOfOffset: (NSString *) offsetName
53 {
54   NSString *offsetTo;
55   BOOL negative;
56   NSRange cursor;
57   unsigned int length;
58   unsigned int seconds;
59
60   seconds = 0;
61
62   offsetTo = [[self uniqueChildWithTag: offsetName]
63                value: 0];
64   length = [offsetTo length];
65   negative = [offsetTo hasPrefix: @"-"];
66   if (negative)
67     {
68       length--;
69       cursor = NSMakeRange(1, 2);
70     }
71   else if ([offsetTo hasPrefix: @"+"])
72     {
73       length--;
74       cursor = NSMakeRange(1, 2);
75     }
76   else
77     cursor = NSMakeRange(0, 2);
78
79   seconds = 3600 * [[offsetTo substringWithRange: cursor] intValue];
80   cursor.location += 2;
81   seconds += 60 * [[offsetTo substringWithRange: cursor] intValue];
82   if (length == 6)
83     {
84       cursor.location += 2;
85       seconds += [[offsetTo substringWithRange: cursor] intValue];
86     }
87
88   return ((negative) ? -seconds : seconds);
89 }
90
91 - (unsigned int) dayOfWeekFromRruleDay: (iCalWeekDay) day
92 {
93   unsigned int dayOfWeek;
94
95   dayOfWeek = 1;
96   while (day >> dayOfWeek)
97     dayOfWeek++;
98
99   return dayOfWeek;
100 }
101
102 - (NSCalendarDate *) occurenceForDate: (NSCalendarDate *) refDate;
103 {
104   NSCalendarDate *tmpDate;
105   iCalRecurrenceRule *rrule;
106   NSString *byDay;
107   int dayOfWeek, dateDayOfWeek, offset, pos;
108
109   rrule = (iCalRecurrenceRule *) [self uniqueChildWithTag: @"rrule"];
110   byDay = [rrule namedValue: @"byday"];
111   dayOfWeek = [self dayOfWeekFromRruleDay: [rrule byDayMask]];
112   pos = [[byDay substringToIndex: 2] intValue];
113   if (!pos)
114     pos = 1;
115
116   tmpDate = [NSCalendarDate
117               dateWithYear: [refDate yearOfCommonEra]
118               month: [[rrule namedValue: @"bymonth"] intValue]
119               day: 1 hour: 0 minute: 0 second: 0
120               timeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
121   tmpDate = [tmpDate addYear: 0 month: ((pos > 0) ? 0 : 1)
122                      day: 0 hour: 0 minute: 0
123                      second: -[self _secondsOfOffset: @"tzoffsetfrom"]];
124   dateDayOfWeek = [tmpDate dayOfWeek];
125   offset = (dayOfWeek - dateDayOfWeek);
126   if (pos > 0 && offset < 0)
127     offset += 7;
128   offset += (pos * 7);
129   tmpDate = [tmpDate addYear: 0 month: 0 day: offset
130                      hour: 0 minute: 0 second: 0];
131
132   return tmpDate;
133 }
134
135 - (int) secondsOffsetFromGMT
136 {
137   return [self _secondsOfOffset: @"tzoffsetto"];
138 }
139
140 @end