1 /* iCalDateTime.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/NSString.h>
24 #import <Foundation/NSTimeZone.h>
26 #import <NGExtensions/NSObject+Logs.h>
28 #import "NSCalendarDate+NGCards.h"
29 #import "NSString+NGCards.h"
31 #import "iCalCalendar.h"
32 #import "iCalTimeZone.h"
34 #import "iCalDateTime.h"
36 // static NSTimeZone *localTimeZone = nil;
38 @implementation iCalDateTime
40 // + (void) initialize
42 // if (!localTimeZone)
44 // localTimeZone = [NSTimeZone defaultTimeZone];
45 // [localTimeZone retain];
49 // + (void) setLocalTimeZone: (NSTimeZone *) aTimeZone
51 // [localTimeZone release];
52 // localTimeZone = aTimeZone;
53 // [localTimeZone retain];
56 - (void) setTimeZone: (iCalTimeZone *) iTZ
58 iCalCalendar *calendar;
59 NSCalendarDate *dateTime;
62 dateTime = [self dateTime];
66 = (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
68 [calendar addTimeZone: iTZ];
74 [self setValue: 0 ofAttribute: @"tzid" to: newTZId];
75 [self setDateTime: dateTime];
78 - (iCalTimeZone *) timeZone
80 iCalCalendar *calendar;
82 iCalTimeZone *timeZone;
86 tzId = [self value: 0 ofAttribute: @"tzid"];
90 = (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
91 timeZone = [calendar timeZoneWithId: tzId];
93 [self logWithFormat: @"timezone '%@' not found in calendar", tzId];
99 /* TODO: should implement the case where the TZ would be implicitly local
100 (no TZID and no UTC) */
101 - (void) _setDateTime: (NSCalendarDate *) dateTime
102 forAllDayEntity: (BOOL) forAllDayEntity
104 NSCalendarDate *tmpTime;
106 NSString *timeString;
111 tz = [self timeZone];
115 timeString = [tz dateStringForDate: dateTime];
117 timeString = [tz dateTimeStringForDate: dateTime];
121 utcTZ = [NSTimeZone timeZoneWithName: @"GMT"];
123 tmpTime = [dateTime copy];
124 [tmpTime setTimeZone: utcTZ];
126 timeString = [tmpTime iCalFormattedDateString];
128 timeString = [NSString stringWithFormat: @"%@Z",
129 [tmpTime iCalFormattedDateTimeString]];
136 [self setValue: 0 to: timeString];
139 - (void) setDateTime: (NSCalendarDate *) dateTime
141 [self _setDateTime: dateTime forAllDayEntity: NO];
144 - (void) setDate: (NSCalendarDate *) dateTime
146 [self _setDateTime: dateTime forAllDayEntity: YES];
149 - (NSCalendarDate *) dateTime
153 NSCalendarDate *initialDate, *dateTime;
156 date = [self value: 0];
157 iTZ = [self timeZone];
159 dateTime = [iTZ dateForDateTimeString: date];
162 initialDate = [date asCalendarDate];
165 if ([date hasSuffix: @"Z"] || [date hasSuffix: @"z"])
166 dateTime = initialDate;
169 /* same TODO as above */
170 tz = [NSTimeZone defaultTimeZone];
171 dateTime = [initialDate addYear: 0 month: 0 day: 0
173 second: -[tz secondsFromGMT]];
185 return [[self value: 0] isAllDayDate];