2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
23 #include "NSCalendarDate+ICal.h"
27 static NSTimeZone *gmt = nil;
28 static inline void _setupGMT(void) {
30 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
33 @implementation NSCalendarDate(ICalValue)
35 - (NSTimeZone *)timeZoneFromICalTime:(struct icaltimetype *)_dt
36 defaultTimeZone:(NSTimeZone *)_tz
46 NSLog(@"WARNING(%s): using localtimezone !", __PRETTY_FUNCTION__);
47 return [NSTimeZone localTimeZone];
50 - (id)initWithICalDate:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
53 tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
55 self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
56 hour:12 minute:0 second:0
61 - (id)initWithICalTime:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
65 return [self initWithICalDate:_dt timeZone:_tz];
67 tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
70 self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
71 hour:_dt.hour minute:_dt.minute second:_dt.second
76 - (id)initWithICalDate:(struct icaltimetype)_dt {
78 return [self initWithICalDate:_dt timeZone:gmt];
80 - (id)initWithICalTime:(struct icaltimetype)_dt {
82 return [self initWithICalTime:_dt timeZone:gmt];
85 - (id)initWithICalValueHandle:(icalvalue *)_handle {
86 if (_handle == NULL) {
90 return [self initWithICalTime:icalvalue_get_datetime(_handle)];
92 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
100 if ((val = icalproperty_get_value(_prop)) == NULL) {
101 NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
106 return [self initWithICalValueHandle:val];
111 - (NSCalendarDate *)dateByApplyingICalDuration:(struct icaldurationtype)_dur {
113 return [self dateByAddingYears:0 months:0
114 days:-(_dur.days + (_dur.weeks * 7))
116 minutes:-(_dur.minutes)
117 seconds:-(_dur.seconds)];
120 return [self dateByAddingYears:0 months:0
121 days:(_dur.days + (_dur.weeks * 7))
122 hours:_dur.hours minutes:_dur.minutes seconds:_dur.seconds];
128 static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";
130 - (NSString *)icalStringInGMT {
135 /* set GMT as timezone */
136 oldtz = [[self timeZone] retain];
142 [self setTimeZone:gmt];
146 s = [self descriptionWithCalendarFormat:gmtcalfmt];
148 /* restore old timezone */
150 [self setTimeZone:oldtz];
157 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
160 if (_tz == gmt || _tz == nil)
161 return [self icalStringInGMT];
162 else if ([_tz isEqual:gmt])
163 return [self icalStringInGMT];
166 NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
167 __PRETTY_FUNCTION__, _tz);
168 return [self icalStringInGMT];
172 - (NSString *)icalString {
174 return [self icalStringWithTimeZone:gmt];
177 @end /* NSDate(ICalValue) */