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 "iCalDateHolder.h"
24 #include "iCalObject.h"
27 @interface NSTimeZone(iCalTimeZone)
29 + (NSTimeZone *)timeZoneWithICalId:(NSString *)_tz;
33 @implementation iCalDateHolder
35 static NSTimeZone *gmt = nil;
39 gmt = [[NSTimeZone timeZoneWithName:@"GMT"] retain];
43 [self->string release];
51 - (void)setString:(NSString *)_value {
52 ASSIGNCOPY(self->string, _value);
54 - (NSString *)string {
58 - (void)setTag:(NSString *)_value {
59 ASSIGNCOPY(self->tag, _value);
65 - (void)setTzid:(NSString *)_value {
66 ASSIGNCOPY(self->tzid, _value);
72 /* mapping to Foundation */
74 - (NSTimeZone *)timeZone {
75 // TODO: lookup tzid in iCalCalendar !
81 if ([s hasPrefix:@"/softwarestudio.org"]) {
84 r = [s rangeOfString:@"Europe/"];
86 s = [s substringFromIndex:r.location];
88 return [NSTimeZone timeZoneWithICalId:s];
93 - (id)awakeAfterUsingSaxDecoder:(id)_decoder {
94 NSCalendarDate *date = nil;
100 [self logWithFormat:@"tag %@: got an weird date string '%@' ?!",
105 /* calculate timezone */
107 if ([self->string hasSuffix:@"Z"]) {
108 /* zulu time, eg 20021009T094500Z */
110 s = [s substringToIndex:([s length] - 1)];
113 tz = [self timeZone];
117 20021009T094500 - 15 chars
118 20021009T0945 - 13 chars
119 991009T0945 - 11 chars
123 if ([s rangeOfString:@"T"].length == 0 && [s length] == 8) {
124 /* hm, maybe a date without a time? like an allday event! */
125 int year, month, day;
126 unsigned char buf[16];
127 [s getCString:&(buf[0])];
130 day = atoi(&(buf[6])); buf[6] = '\0';
131 month = atoi(&(buf[4])); buf[4] = '\0';
132 year = atoi(&(buf[0]));
134 date = [NSCalendarDate dateWithYear:year month:month day:day
135 hour:0 minute:0 second:0
138 else if ([s length] == 15) {
139 int year, month, day, hour, minute, second;
140 unsigned char buf[24];
141 [s getCString:&(buf[0])];
143 second = atoi(&(buf[13])); buf[13] = '\0';
144 minute = atoi(&(buf[11])); buf[11] = '\0';
145 hour = atoi(&(buf[9])); buf[9] = '\0';
146 day = atoi(&(buf[6])); buf[6] = '\0';
147 month = atoi(&(buf[4])); buf[4] = '\0';
148 year = atoi(&(buf[0]));
150 date = [NSCalendarDate dateWithYear:year month:month day:day
151 hour:hour minute:minute second:second
155 NSLog(@"%s: unknown date format (%@) ???", __PRETTY_FUNCTION__, s);
158 NSLog(@"couldn't convert string '%@' to date (format '%@') ..", s);
165 - (NSString *)description {
168 ms = [NSMutableString stringWithCapacity:128];
169 [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
171 if (self->tag) [ms appendFormat:@" %@", self->tag];
172 if (self->string) [ms appendFormat:@" '%@'", self->string];
173 if (self->tzid) [ms appendFormat:@" tz=%@", self->tzid];
175 [ms appendString:@">"];
179 @end /* iCalDateHolder */
181 @implementation NSTimeZone(iCalTimeZone)
183 static NSMutableDictionary *idToTz = nil; // THREAD
185 + (NSTimeZone *)timeZoneWithICalId:(NSString *)_tzid {
186 static NSString *iCalDefaultTZ = nil;
190 idToTz = [[NSMutableDictionary alloc] initWithCapacity:16];
192 if ([_tzid length] == 0) {
194 tz = [iCalObject iCalDefaultTimeZone];
195 if (tz != nil) return tz;
197 if (iCalDefaultTZ == nil) {
200 // TODO: take a default timeZone
201 ud = [NSUserDefaults standardUserDefaults];
202 defTz = [ud stringForKey:@"iCalTimeZoneName"];
203 if ([defTz length] == 0)
204 defTz = [ud stringForKey:@"TimeZoneName"];
205 if ([defTz length] == 0)
206 defTz = [ud stringForKey:@"TimeZone"];
207 if ([defTz length] == 0)
209 iCalDefaultTZ = [defTz retain];
212 _tzid = iCalDefaultTZ;
216 if ([_tzid length] == 0)
219 tz = [idToTz objectForKey:_tzid];
220 if (tz == nil) tz = [NSTimeZone timeZoneWithName:_tzid];
221 if (tz == nil) tz = [NSTimeZone timeZoneWithAbbreviation:_tzid];
224 NSLog(@"couldn't map timezone id %@", _tzid);
227 if (tz) [idToTz setObject:tz forKey:_tzid];
231 @end /* NSTimeZone(iCalTimeZone) */