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"
26 #import <libical/ical.h>
31 static NSTimeZone *gmt = nil;
32 static inline void _setupGMT(void) {
34 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
37 @implementation NSCalendarDate(ICalValue)
39 - (NSTimeZone *)timeZoneFromICalTime:(struct icaltimetype *)_dt
40 defaultTimeZone:(NSTimeZone *)_tz
50 NSLog(@"WARNING(%s): using localtimezone !", __PRETTY_FUNCTION__);
51 return [NSTimeZone localTimeZone];
54 - (id)initWithICalDate:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
57 tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
59 self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
60 hour:12 minute:0 second:0
65 - (id)initWithICalTime:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
69 return [self initWithICalDate:_dt timeZone:_tz];
71 tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
74 self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
75 hour:_dt.hour minute:_dt.minute second:_dt.second
80 - (id)initWithICalDate:(struct icaltimetype)_dt {
82 return [self initWithICalDate:_dt timeZone:gmt];
84 - (id)initWithICalTime:(struct icaltimetype)_dt {
86 return [self initWithICalTime:_dt timeZone:gmt];
89 - (id)initWithICalValueHandle:(icalvalue *)_handle {
90 if (_handle == NULL) {
94 return [self initWithICalTime:icalvalue_get_datetime(_handle)];
96 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
104 if ((val = icalproperty_get_value(_prop)) == NULL) {
105 NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
110 return [self initWithICalValueHandle:val];
115 - (NSCalendarDate *)dateByApplyingICalDuration:(struct icaldurationtype)_dur {
117 return [self dateByAddingYears:0 months:0
118 days:-(_dur.days + (_dur.weeks * 7))
120 minutes:-(_dur.minutes)
121 seconds:-(_dur.seconds)];
124 return [self dateByAddingYears:0 months:0
125 days:(_dur.days + (_dur.weeks * 7))
126 hours:_dur.hours minutes:_dur.minutes seconds:_dur.seconds];
132 static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";
134 - (NSString *)icalStringInGMT {
139 /* set GMT as timezone */
140 oldtz = [[self timeZone] retain];
146 [self setTimeZone:gmt];
150 s = [self descriptionWithCalendarFormat:gmtcalfmt];
152 /* restore old timezone */
154 [self setTimeZone:oldtz];
161 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
164 if (_tz == gmt || _tz == nil)
165 return [self icalStringInGMT];
166 else if ([_tz isEqual:gmt])
167 return [self icalStringInGMT];
170 NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
171 __PRETTY_FUNCTION__, _tz);
172 return [self icalStringInGMT];
176 - (NSString *)icalString {
178 return [self icalStringWithTimeZone:gmt];
181 @end /* NSDate(ICalValue) */