]> err.no Git - sope/blob - Recycler/iCalSaxDriver/NSCalendarDate+ICal.m
moved iCalSaxDriver to Recycler
[sope] / Recycler / iCalSaxDriver / NSCalendarDate+ICal.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NSCalendarDate+ICal.h"
24 #include "common.h"
25 #ifdef XCODE_BUILD
26 #import <libical/ical.h>
27 #else
28 #include <ical.h>
29 #endif
30
31 static NSTimeZone *gmt = nil;
32 static inline void _setupGMT(void) {
33   if (gmt == nil)
34     gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
35 }
36
37 @implementation NSCalendarDate(ICalValue)
38
39 - (NSTimeZone *)timeZoneFromICalTime:(struct icaltimetype *)_dt
40   defaultTimeZone:(NSTimeZone *)_tz
41 {
42   _setupGMT();
43
44   if (_dt->is_utc)
45     return gmt;
46
47   if (_tz)
48     return _tz;
49   
50   NSLog(@"WARNING(%s): using localtimezone !", __PRETTY_FUNCTION__);
51   return [NSTimeZone localTimeZone];
52 }
53
54 - (id)initWithICalDate:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
55   NSTimeZone *tz;
56   
57   tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
58   
59   self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
60                hour:12 minute:0 second:0
61                timeZone:_tz];
62   return self;
63 }
64
65 - (id)initWithICalTime:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
66   NSTimeZone *tz;
67   
68   if (_dt.is_date)
69     return [self initWithICalDate:_dt timeZone:_tz];
70   
71   tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
72   
73   
74   self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
75                hour:_dt.hour minute:_dt.minute second:_dt.second
76                timeZone:tz];
77   return self;
78 }
79
80 - (id)initWithICalDate:(struct icaltimetype)_dt {
81   _setupGMT();
82   return [self initWithICalDate:_dt timeZone:gmt];
83 }
84 - (id)initWithICalTime:(struct icaltimetype)_dt {
85   _setupGMT();
86   return [self initWithICalTime:_dt timeZone:gmt];
87 }
88
89 - (id)initWithICalValueHandle:(icalvalue *)_handle {
90   if (_handle == NULL) {
91     RELEASE(self);
92     return nil;
93   }
94   return [self initWithICalTime:icalvalue_get_datetime(_handle)];
95 }
96 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
97   icalvalue *val;
98   
99   if (_prop == NULL) {
100     RELEASE(self);
101     return nil;
102   }
103
104   if ((val = icalproperty_get_value(_prop)) == NULL) {
105     NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
106     RELEASE(self);
107     return nil;
108   }
109   
110   return [self initWithICalValueHandle:val];
111 }
112
113 /* durations */
114
115 - (NSCalendarDate *)dateByApplyingICalDuration:(struct icaldurationtype)_dur {
116   if (_dur.is_neg) {
117     return [self dateByAddingYears:0 months:0
118                  days:-(_dur.days + (_dur.weeks * 7))
119                  hours:-(_dur.hours)
120                  minutes:-(_dur.minutes)
121                  seconds:-(_dur.seconds)];
122   }
123   else {
124     return [self dateByAddingYears:0 months:0
125                  days:(_dur.days + (_dur.weeks * 7))
126                  hours:_dur.hours minutes:_dur.minutes seconds:_dur.seconds];
127   }
128 }
129
130 /* represention */
131
132 static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";
133
134 - (NSString *)icalStringInGMT {
135   NSTimeZone *oldtz;
136   NSString   *s;
137   _setupGMT();
138   
139   /* set GMT as timezone */
140   oldtz = [[self timeZone] retain];
141   if (oldtz == gmt) {
142     [oldtz release];
143     oldtz = nil;
144   }
145   else {
146     [self setTimeZone:gmt];
147   }
148   
149   /* calc string */
150   s = [self descriptionWithCalendarFormat:gmtcalfmt];
151   
152   /* restore old timezone */
153   if (oldtz) {
154     [self setTimeZone:oldtz];
155     [oldtz release];
156   }
157   
158   return s;
159 }
160
161 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
162   _setupGMT();
163   
164   if (_tz == gmt || _tz == nil)
165     return [self icalStringInGMT];
166   else if ([_tz isEqual:gmt])
167     return [self icalStringInGMT];
168   else {
169     /* not in GMT */
170     NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
171           __PRETTY_FUNCTION__, _tz);
172     return [self icalStringInGMT];
173   }
174 }
175
176 - (NSString *)icalString {
177   _setupGMT();
178   return [self icalStringWithTimeZone:gmt];
179 }
180
181 @end /* NSDate(ICalValue) */