2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NSCalendarDate+ICal.h"
23 #include "iCalDateHolder.h"
26 static NSTimeZone *gmt = nil;
27 static inline void _setupGMT(void) {
29 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
32 @interface iCalDateHolder (PrivateAPI)
33 - (id)awakeAfterUsingSaxDecoder:(id)_decoder;
36 @implementation NSCalendarDate(iCalRepresentation)
40 static NSString *gmtcalfmt = @"%Y%m%dT%H%M%SZ";
42 + (id)calendarDateWithICalRepresentation:(NSString *)_iCalRep {
46 dh = [[iCalDateHolder alloc] init];
47 [dh setString:_iCalRep];
48 date = [dh awakeAfterUsingSaxDecoder:nil];
53 - (NSString *)icalStringInGMT {
58 /* set GMT as timezone */
59 oldtz = [[self timeZone] retain];
65 [self setTimeZone:gmt];
69 s = [self descriptionWithCalendarFormat:gmtcalfmt];
71 /* restore old timezone */
73 [self setTimeZone:oldtz];
80 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
83 if (_tz == gmt || _tz == nil)
84 return [self icalStringInGMT];
85 else if ([_tz isEqual:gmt])
86 return [self icalStringInGMT];
89 NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
90 __PRETTY_FUNCTION__, _tz);
91 return [self icalStringInGMT];
95 - (NSString *)icalString {
97 return [self icalStringWithTimeZone:gmt];
100 @end /* NSDate(ICalValue) */
104 #define ABS(a) ((a) < 0 ? -(a) : (a))
107 @implementation NSCalendarDate (iCalRecurrenceCalculatorExtensions)
109 - (unsigned)yearsBetweenDate:(NSCalendarDate *)_date {
110 return ABS([self yearOfCommonEra] - [_date yearOfCommonEra]);
113 - (unsigned)monthsBetweenDate:(NSCalendarDate *)_date {
114 NSCalendarDate *start, *end;
115 NSComparisonResult order;
118 order = [self compare:_date];
119 if (order == NSOrderedSame)
121 else if (order == NSOrderedAscending) {
129 yDiff = [end yearOfCommonEra] - [start yearOfCommonEra];
131 unsigned monthsRemaining, monthsToGo;
133 monthsRemaining = 12 - [start monthOfYear];
134 monthsToGo = [end monthOfYear];
136 return monthsRemaining + monthsToGo + (12 * yDiff);
138 /* start and end in same year, calculate plain diff */
139 return [end monthOfYear] - [start monthOfYear];
142 - (unsigned)daysBetweenDate:(NSCalendarDate *)_date {
143 return ABS([self julianNumber] - [_date julianNumber]);
145 @end /* NSCalendarDate (iCalRecurrenceCalculatorExtensions) */