2007-01-31 18:11:29 +01:00
|
|
|
/* iCalDateTime.m - this file is part of SOPE
|
|
|
|
*
|
2008-09-27 03:13:41 +02:00
|
|
|
* Copyright (C) 2006 Inverse inc.
|
2007-01-31 18:11:29 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSTimeZone.h>
|
|
|
|
|
2007-11-29 20:43:51 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
#import "NSCalendarDate+NGCards.h"
|
|
|
|
#import "NSString+NGCards.h"
|
|
|
|
|
|
|
|
#import "iCalCalendar.h"
|
|
|
|
#import "iCalTimeZone.h"
|
|
|
|
|
|
|
|
#import "iCalDateTime.h"
|
|
|
|
|
|
|
|
// static NSTimeZone *localTimeZone = nil;
|
|
|
|
|
|
|
|
@implementation iCalDateTime
|
|
|
|
|
|
|
|
// + (void) initialize
|
|
|
|
// {
|
|
|
|
// if (!localTimeZone)
|
|
|
|
// {
|
|
|
|
// localTimeZone = [NSTimeZone defaultTimeZone];
|
|
|
|
// [localTimeZone retain];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// + (void) setLocalTimeZone: (NSTimeZone *) aTimeZone
|
|
|
|
// {
|
|
|
|
// [localTimeZone release];
|
|
|
|
// localTimeZone = aTimeZone;
|
|
|
|
// [localTimeZone retain];
|
|
|
|
// }
|
|
|
|
|
|
|
|
- (void) setTimeZone: (iCalTimeZone *) iTZ
|
|
|
|
{
|
|
|
|
iCalCalendar *calendar;
|
|
|
|
NSCalendarDate *dateTime;
|
|
|
|
NSString *newTZId;
|
|
|
|
|
|
|
|
dateTime = [self dateTime];
|
|
|
|
if (iTZ)
|
|
|
|
{
|
|
|
|
calendar
|
|
|
|
= (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
|
|
|
|
if (calendar)
|
|
|
|
[calendar addTimeZone: iTZ];
|
|
|
|
newTZId = [iTZ tzId];
|
|
|
|
}
|
2007-12-17 16:29:06 +01:00
|
|
|
else
|
|
|
|
newTZId = nil;
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
[self setValue: 0 ofAttribute: @"tzid" to: newTZId];
|
2009-10-19 17:24:08 +02:00
|
|
|
|
|
|
|
if ([self isAllDay])
|
|
|
|
[self setDate: dateTime];
|
|
|
|
else
|
|
|
|
[self setDateTime: dateTime];
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (iCalTimeZone *) timeZone
|
|
|
|
{
|
|
|
|
iCalCalendar *calendar;
|
|
|
|
NSString *tzId;
|
|
|
|
iCalTimeZone *timeZone;
|
|
|
|
|
2007-11-29 20:43:51 +01:00
|
|
|
timeZone = nil;
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
tzId = [self value: 0 ofAttribute: @"tzid"];
|
2007-11-29 20:43:51 +01:00
|
|
|
if ([tzId length])
|
|
|
|
{
|
|
|
|
calendar
|
|
|
|
= (iCalCalendar *) [self searchParentOfClass: [iCalCalendar class]];
|
|
|
|
timeZone = [calendar timeZoneWithId: tzId];
|
|
|
|
if (!timeZone)
|
|
|
|
[self logWithFormat: @"timezone '%@' not found in calendar", tzId];
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
return timeZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: should implement the case where the TZ would be implicitly local
|
|
|
|
(no TZID and no UTC) */
|
2007-06-07 18:24:20 +02:00
|
|
|
- (void) _setDateTime: (NSCalendarDate *) dateTime
|
|
|
|
forAllDayEntity: (BOOL) forAllDayEntity
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
|
|
|
NSCalendarDate *tmpTime;
|
|
|
|
NSTimeZone *utcTZ;
|
2007-06-07 20:10:30 +02:00
|
|
|
NSString *timeString;
|
2007-01-31 18:11:29 +01:00
|
|
|
iCalTimeZone *tz;
|
|
|
|
|
|
|
|
if (dateTime)
|
|
|
|
{
|
|
|
|
tz = [self timeZone];
|
|
|
|
if (tz)
|
2007-06-13 04:34:29 +02:00
|
|
|
{
|
|
|
|
if (forAllDayEntity)
|
|
|
|
timeString = [tz dateStringForDate: dateTime];
|
|
|
|
else
|
|
|
|
timeString = [tz dateTimeStringForDate: dateTime];
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpTime = [dateTime copy];
|
2007-06-07 18:24:20 +02:00
|
|
|
if (forAllDayEntity)
|
2007-06-07 20:05:45 +02:00
|
|
|
timeString = [tmpTime iCalFormattedDateString];
|
2007-06-07 19:45:48 +02:00
|
|
|
else
|
2008-07-16 23:46:11 +02:00
|
|
|
{
|
|
|
|
utcTZ = [NSTimeZone timeZoneWithName: @"GMT"];
|
|
|
|
[tmpTime setTimeZone: utcTZ];
|
|
|
|
timeString = [NSString stringWithFormat: @"%@Z",
|
|
|
|
[tmpTime iCalFormattedDateTimeString]];
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
[tmpTime release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
timeString = @"";
|
|
|
|
|
|
|
|
[self setValue: 0 to: timeString];
|
|
|
|
}
|
|
|
|
|
2007-06-07 18:24:20 +02:00
|
|
|
- (void) setDateTime: (NSCalendarDate *) dateTime
|
|
|
|
{
|
|
|
|
[self _setDateTime: dateTime forAllDayEntity: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDate: (NSCalendarDate *) dateTime
|
|
|
|
{
|
|
|
|
[self _setDateTime: dateTime forAllDayEntity: YES];
|
|
|
|
}
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
- (NSCalendarDate *) dateTime
|
|
|
|
{
|
|
|
|
iCalTimeZone *iTZ;
|
|
|
|
NSString *date;
|
|
|
|
NSCalendarDate *initialDate, *dateTime;
|
|
|
|
NSTimeZone *tz;
|
|
|
|
|
|
|
|
date = [self value: 0];
|
|
|
|
iTZ = [self timeZone];
|
|
|
|
if (iTZ)
|
|
|
|
dateTime = [iTZ dateForDateTimeString: date];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initialDate = [date asCalendarDate];
|
|
|
|
if (initialDate)
|
|
|
|
{
|
|
|
|
if ([date hasSuffix: @"Z"] || [date hasSuffix: @"z"])
|
|
|
|
dateTime = initialDate;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* same TODO as above */
|
|
|
|
tz = [NSTimeZone defaultTimeZone];
|
|
|
|
dateTime = [initialDate addYear: 0 month: 0 day: 0
|
|
|
|
hour: 0 minute: 0
|
2009-03-18 22:41:22 +01:00
|
|
|
second: -[tz secondsFromGMTForDate: initialDate]];
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dateTime = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateTime;
|
|
|
|
}
|
|
|
|
|
2007-06-07 18:24:20 +02:00
|
|
|
- (BOOL) isAllDay
|
|
|
|
{
|
|
|
|
return [[self value: 0] isAllDayDate];
|
|
|
|
}
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
@end
|