1 /* iCalTimeZone.m - this file is part of SOPE
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSTimeZone.h>
28 #import "NSCalendarDate+NGCards.h"
29 #import "NSString+NGCards.h"
30 #import "iCalTimeZonePeriod.h"
32 #import "iCalTimeZone.h"
34 @implementation iCalTimeZone
36 - (Class) classForTag: (NSString *) classTag
40 if ([classTag isEqualToString: @"STANDARD"]
41 || [classTag isEqualToString: @"DAYLIGHT"])
42 tagClass = [iCalTimeZonePeriod class];
43 else if ([classTag isEqualToString: @"TZID"])
44 tagClass = [CardElement class];
46 tagClass = [super classForTag: classTag];
51 - (void) setTzId: (NSString *) tzId
53 [[self uniqueChildWithTag: @"tzid"] setValue: 0 to: tzId];
58 return [[self uniqueChildWithTag: @"tzid"] value: 0];
61 - (NSCalendarDate *) _occurenceForPeriodNamed: (NSString *) pName
62 forDate: (NSCalendarDate *) aDate
65 iCalTimeZonePeriod *period;
66 NSCalendarDate *occurence;
68 periods = [self childrenWithTag: pName];
71 period = (iCalTimeZonePeriod *) [periods objectAtIndex: 0];
72 occurence = [period occurenceForDate: aDate];
80 - (iCalTimeZonePeriod *) periodForDate: (NSCalendarDate *) date
82 NSCalendarDate *daylightOccurence, *standardOccurence;
83 iCalTimeZonePeriod *period;
85 /* FIXME, this could cause crashes when timezones are not properly
86 specified, but let's say it won't happen often... */
88 daylightOccurence = [self _occurenceForPeriodNamed: @"daylight"
90 standardOccurence = [self _occurenceForPeriodNamed: @"standard"
92 if ([date earlierDate: daylightOccurence] == date
93 || [date earlierDate: standardOccurence] == standardOccurence)
94 period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"standard"];
96 period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"daylight"];
98 NSLog (@"chosen period: '%@'", [period tag]);
103 - (NSCalendarDate *) _computedDateTimeForDate: (NSCalendarDate *) date
105 NSCalendarDate *tmpDate;
108 utc = [NSTimeZone timeZoneWithName: @"GMT"];
109 tmpDate = [date copy];
110 [tmpDate autorelease];
111 [tmpDate setTimeZone: utc];
113 return [tmpDate addYear: 0 month: 0 day: 0
115 second: [[self periodForDate: date] secondsOffsetFromGMT]];
118 - (NSString *) dateTimeStringForDate: (NSCalendarDate *) date
120 return [[self _computedDateTimeForDate: date]
121 iCalFormattedDateTimeString];
124 - (NSString *) dateStringForDate: (NSCalendarDate *) date
126 return [[self _computedDateTimeForDate: date]
127 iCalFormattedDateString];
130 - (NSCalendarDate *) dateForDateTimeString: (NSString *) string
132 NSCalendarDate *tmpDate;
133 iCalTimeZonePeriod *period;
135 tmpDate = [string asCalendarDate];
136 period = [self periodForDate: tmpDate];
138 return [tmpDate addYear: 0 month: 0 day: 0
140 second: -[period secondsOffsetFromGMT]];