2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "iCalDateHolder.h"
23 #include "iCalObject.h"
26 @interface NSTimeZone(iCalTimeZone)
28 + (NSTimeZone *)timeZoneWithICalId:(NSString *)_tz;
32 @implementation iCalDateHolder
34 static NSTimeZone *gmt = nil;
38 gmt = [[NSTimeZone timeZoneWithName:@"GMT"] retain];
43 [self->string release];
50 - (void)setString:(NSString *)_value {
51 ASSIGNCOPY(self->string, _value);
53 - (NSString *)string {
57 - (void)setTag:(NSString *)_value {
58 ASSIGNCOPY(self->tag, _value);
64 - (void)setTzid:(NSString *)_value {
65 ASSIGNCOPY(self->tzid, _value);
71 /* mapping to Foundation */
73 - (NSTimeZone *)timeZone {
74 // TODO: lookup tzid in iCalCalendar !
80 if ([s hasPrefix:@"/softwarestudio.org"]) {
83 r = [s rangeOfString:@"Europe/"];
85 s = [s substringFromIndex:r.location];
87 return [NSTimeZone timeZoneWithICalId:s];
92 - (id)awakeAfterUsingSaxDecoder:(id)_decoder {
93 NSCalendarDate *date = nil;
99 [self logWithFormat:@"tag %@: got an weird date string '%@' ?!",
104 /* calculate timezone */
106 if ([self->string hasSuffix:@"Z"]) {
107 /* zulu time, eg 20021009T094500Z */
109 s = [s substringToIndex:([s length] - 1)];
112 tz = [self timeZone];
116 20021009T094500 - 15 chars
117 20021009T0945 - 13 chars
118 991009T0945 - 11 chars
122 if ([s rangeOfString:@"T"].length == 0 && [s length] == 8) {
123 /* hm, maybe a date without a time? like an allday event! */
124 int year, month, day;
126 [s getCString:&(buf[0])];
129 day = atoi(&(buf[6])); buf[6] = '\0';
130 month = atoi(&(buf[4])); buf[4] = '\0';
131 year = atoi(&(buf[0]));
133 date = [NSCalendarDate dateWithYear:year month:month day:day
134 hour:0 minute:0 second:0
137 else if ([s length] == 15) {
138 int year, month, day, hour, minute, second;
140 [s getCString:&(buf[0])];
142 second = atoi(&(buf[13])); buf[13] = '\0';
143 minute = atoi(&(buf[11])); buf[11] = '\0';
144 hour = atoi(&(buf[9])); buf[9] = '\0';
145 day = atoi(&(buf[6])); buf[6] = '\0';
146 month = atoi(&(buf[4])); buf[4] = '\0';
147 year = atoi(&(buf[0]));
149 date = [NSCalendarDate dateWithYear:year month:month day:day
150 hour:hour minute:minute second:second
154 NSLog(@"%s: unknown date format (%@) ???", __PRETTY_FUNCTION__, s);
157 NSLog(@"couldn't convert string '%@' to date (format '%@') ..", s);
164 - (void)appendAttributesToDescription:(NSMutableString *)ms {
165 if (self->tag) [ms appendFormat:@" %@", self->tag];
166 if (self->string) [ms appendFormat:@" '%@'", self->string];
167 if (self->tzid) [ms appendFormat:@" tz=%@", self->tzid];
170 - (NSString *)description {
173 ms = [NSMutableString stringWithCapacity:128];
174 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
175 [self appendAttributesToDescription:ms];
176 [ms appendString:@">"];
180 @end /* iCalDateHolder */
182 @implementation NSTimeZone(iCalTimeZone)
184 static NSMutableDictionary *idToTz = nil; // THREAD
186 + (NSTimeZone *)timeZoneWithICalId:(NSString *)_tzid {
187 static NSString *iCalDefaultTZ = nil;
191 idToTz = [[NSMutableDictionary alloc] initWithCapacity:16];
193 if ([_tzid length] == 0) {
195 tz = [iCalObject iCalDefaultTimeZone];
196 if (tz != nil) return tz;
198 if (iCalDefaultTZ == nil) {
201 // TODO: take a default timeZone
202 ud = [NSUserDefaults standardUserDefaults];
203 defTz = [ud stringForKey:@"iCalTimeZoneName"];
204 if ([defTz length] == 0)
205 defTz = [ud stringForKey:@"TimeZoneName"];
206 if ([defTz length] == 0)
207 defTz = [ud stringForKey:@"TimeZone"];
208 if ([defTz length] == 0)
210 iCalDefaultTZ = [defTz retain];
213 _tzid = iCalDefaultTZ;
217 if ([_tzid length] == 0)
220 tz = [idToTz objectForKey:_tzid];
221 if (tz == nil) tz = [NSTimeZone timeZoneWithName:_tzid];
222 if (tz == nil) tz = [NSTimeZone timeZoneWithAbbreviation:_tzid];
225 NSLog(@"couldn't map timezone id %@", _tzid);
228 if (tz) [idToTz setObject:tz forKey:_tzid];
232 @end /* NSTimeZone(iCalTimeZone) */