sogo/SOPE/NGCards/iCalTimeZone.m
Wolfgang Sourdeau 52b9643f43 Monotone-Parent: c7fdfee7f222580acc0dcab2c03bab46e8f5f301
Monotone-Revision: 98210b359e6e3fe57ef302cfac263161a8f25967

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2007-06-13T02:34:29
Monotone-Branch: ca.inverse.sogo
2007-06-13 02:34:29 +00:00

144 lines
4 KiB
Objective-C

/* iCalTimeZone.m - this file is part of SOPE
*
* Copyright (C) 2006 Inverse groupe conseil
*
* 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/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSString.h>
#import <Foundation/NSTimeZone.h>
#import "NSCalendarDate+NGCards.h"
#import "NSString+NGCards.h"
#import "iCalTimeZonePeriod.h"
#import "iCalTimeZone.h"
@implementation iCalTimeZone
- (Class) classForTag: (NSString *) classTag
{
Class tagClass;
if ([classTag isEqualToString: @"STANDARD"]
|| [classTag isEqualToString: @"DAYLIGHT"])
tagClass = [iCalTimeZonePeriod class];
else if ([classTag isEqualToString: @"TZID"])
tagClass = [CardElement class];
else
tagClass = [super classForTag: classTag];
return tagClass;
}
- (void) setTzId: (NSString *) tzId
{
[[self uniqueChildWithTag: @"tzid"] setValue: 0 to: tzId];
}
- (NSString *) tzId
{
return [[self uniqueChildWithTag: @"tzid"] value: 0];
}
- (NSCalendarDate *) _occurenceForPeriodNamed: (NSString *) pName
forDate: (NSCalendarDate *) aDate
{
NSArray *periods;
iCalTimeZonePeriod *period;
NSCalendarDate *occurence;
periods = [self childrenWithTag: pName];
if ([periods count])
{
period = (iCalTimeZonePeriod *) [periods objectAtIndex: 0];
occurence = [period occurenceForDate: aDate];
}
else
occurence = nil;
return occurence;
}
- (iCalTimeZonePeriod *) periodForDate: (NSCalendarDate *) date
{
NSCalendarDate *daylightOccurence, *standardOccurence;
iCalTimeZonePeriod *period;
/* FIXME, this could cause crashes when timezones are not properly
specified, but let's say it won't happen often... */
daylightOccurence = [self _occurenceForPeriodNamed: @"daylight"
forDate: date];
standardOccurence = [self _occurenceForPeriodNamed: @"standard"
forDate: date];
if ([date earlierDate: daylightOccurence] == date
|| [date earlierDate: standardOccurence] == standardOccurence)
period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"standard"];
else
period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"daylight"];
NSLog (@"chosen period: '%@'", [period tag]);
return period;
}
- (NSCalendarDate *) _computedDateTimeForDate: (NSCalendarDate *) date
{
NSCalendarDate *tmpDate;
NSTimeZone *utc;
utc = [NSTimeZone timeZoneWithName: @"GMT"];
tmpDate = [date copy];
[tmpDate autorelease];
[tmpDate setTimeZone: utc];
return [tmpDate addYear: 0 month: 0 day: 0
hour: 0 minute: 0
second: [[self periodForDate: date] secondsOffsetFromGMT]];
}
- (NSString *) dateTimeStringForDate: (NSCalendarDate *) date
{
return [[self _computedDateTimeForDate: date]
iCalFormattedDateTimeString];
}
- (NSString *) dateStringForDate: (NSCalendarDate *) date
{
return [[self _computedDateTimeForDate: date]
iCalFormattedDateString];
}
- (NSCalendarDate *) dateForDateTimeString: (NSString *) string
{
NSCalendarDate *tmpDate;
iCalTimeZonePeriod *period;
tmpDate = [string asCalendarDate];
period = [self periodForDate: tmpDate];
return [tmpDate addYear: 0 month: 0 day: 0
hour: 0 minute: 0
second: -[period secondsOffsetFromGMT]];
}
@end