]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalDateTime.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / iCalDateTime.m
1 /* iCalDateTime.m - this file is part of SOPE
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSString.h>
24 #import <Foundation/NSTimeZone.h>
25
26 #import "NSCalendarDate+NGCards.h"
27 #import "NSString+NGCards.h"
28
29 #import "iCalCalendar.h"
30 #import "iCalTimeZone.h"
31
32 #import "iCalDateTime.h"
33
34 // static NSTimeZone *localTimeZone = nil;
35
36 @implementation iCalDateTime
37
38 // + (void) initialize
39 // {
40 //   if (!localTimeZone)
41 //     {
42 //       localTimeZone = [NSTimeZone defaultTimeZone];
43 //       [localTimeZone retain];
44 //     }
45 // }
46
47 // + (void) setLocalTimeZone: (NSTimeZone *) aTimeZone
48 // {
49 //   [localTimeZone release];
50 //   localTimeZone = aTimeZone;
51 //   [localTimeZone retain];
52 // }
53
54 - (void) setTimeZone: (iCalTimeZone *) iTZ
55 {
56   iCalCalendar *calendar;
57   NSCalendarDate *dateTime;
58   NSString *newTZId;
59
60   dateTime = [self dateTime];
61   if (iTZ)
62     {
63       calendar
64         = (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
65       if (calendar)
66         [calendar addTimeZone: iTZ];
67       newTZId = [iTZ tzId];
68     }
69   [self setValue: 0 ofAttribute: @"tzid" to: newTZId];
70   [self setDateTime: dateTime];
71 }
72
73 - (iCalTimeZone *) timeZone
74 {
75   iCalCalendar *calendar;
76   NSString *tzId;
77   iCalTimeZone *timeZone;
78
79   tzId = [self value: 0 ofAttribute: @"tzid"];
80   calendar
81     = (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
82   if ([tzId length] && calendar)
83     timeZone = [calendar timeZoneWithId: tzId];
84   else
85     timeZone = nil;
86
87   return timeZone;
88 }
89
90 /* TODO: should implement the case where the TZ would be implicitly local
91    (no TZID and no UTC) */
92 - (void) _setDateTime: (NSCalendarDate *) dateTime
93       forAllDayEntity: (BOOL) forAllDayEntity
94 {
95   NSCalendarDate *tmpTime;
96   NSTimeZone *utcTZ;
97   NSString *timeString;
98   iCalTimeZone *tz;
99
100   if (dateTime)
101     {
102       tz = [self timeZone];
103       if (tz)
104         {
105           if (forAllDayEntity)
106             timeString = [tz dateStringForDate: dateTime];
107           else
108             timeString = [tz dateTimeStringForDate: dateTime];
109         }
110       else
111         {
112           utcTZ = [NSTimeZone timeZoneWithName: @"GMT"];
113
114           tmpTime = [dateTime copy];
115           [tmpTime setTimeZone: utcTZ];
116           if (forAllDayEntity)
117             timeString = [tmpTime iCalFormattedDateString];
118           else
119             timeString = [NSString stringWithFormat: @"%@Z",
120                                    [tmpTime iCalFormattedDateTimeString]];
121           [tmpTime release];
122         }
123     }
124   else
125     timeString = @"";
126
127   [self setValue: 0 to: timeString];
128 }
129
130 - (void) setDateTime: (NSCalendarDate *) dateTime
131 {
132   [self _setDateTime: dateTime forAllDayEntity: NO];
133 }
134
135 - (void) setDate: (NSCalendarDate *) dateTime
136 {
137   [self _setDateTime: dateTime forAllDayEntity: YES];
138 }
139
140 - (NSCalendarDate *) dateTime
141 {
142   iCalTimeZone *iTZ;
143   NSString *date;
144   NSCalendarDate *initialDate, *dateTime;
145   NSTimeZone *tz;
146
147   date = [self value: 0];
148   iTZ = [self timeZone];
149   if (iTZ)
150     dateTime = [iTZ dateForDateTimeString: date];
151   else
152     {
153       initialDate = [date asCalendarDate];
154       if (initialDate)
155         {
156           if ([date hasSuffix: @"Z"] || [date hasSuffix: @"z"])
157             dateTime = initialDate;
158           else
159             {
160               /* same TODO as above */
161               tz = [NSTimeZone defaultTimeZone];
162               dateTime = [initialDate addYear: 0 month: 0 day: 0
163                                       hour: 0 minute: 0
164                                       second: -[tz secondsFromGMT]];
165             }
166         }
167       else
168         dateTime = nil;
169     }
170
171   return dateTime;
172 }
173
174 - (BOOL) isAllDay
175 {
176   return [[self value: 0] isAllDayDate];
177 }
178
179 @end