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 #import <Foundation/NSString.h>
23 #import <Foundation/NSTimeZone.h>
25 #import <NGExtensions/NSCalendarDate+misc.h>
27 #import "NSCalendarDate+ICal.h"
28 #import "iCalDateHolder.h"
30 static NSTimeZone *gmt = nil;
31 static inline void _setupGMT(void) {
33 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
36 @interface iCalDateHolder (PrivateAPI)
37 - (id)awakeAfterUsingSaxDecoder:(id)_decoder;
40 @implementation NSCalendarDate(iCalRepresentation)
44 static NSString *gmtcalfmt = @"%Y%m%dT%H%M%SZ";
46 + (id)calendarDateWithICalRepresentation:(NSString *)_iCalRep {
50 dh = [[iCalDateHolder alloc] init];
51 [dh setString:_iCalRep];
52 date = [dh awakeAfterUsingSaxDecoder:nil];
57 - (NSString *)icalStringInGMT {
62 /* set GMT as timezone */
63 oldtz = [[self timeZone] retain];
69 [self setTimeZone:gmt];
73 s = [self descriptionWithCalendarFormat:gmtcalfmt];
75 /* restore old timezone */
77 [self setTimeZone:oldtz];
84 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
87 if (_tz == gmt || _tz == nil)
88 return [self icalStringInGMT];
89 else if ([_tz isEqual:gmt])
90 return [self icalStringInGMT];
93 NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
94 __PRETTY_FUNCTION__, _tz);
95 return [self icalStringInGMT];
99 - (NSString *)icalString {
101 return [self icalStringWithTimeZone:gmt];
104 @end /* NSDate(ICalValue) */
108 #define ABS(a) ((a) < 0 ? -(a) : (a))
111 @implementation NSCalendarDate (iCalRecurrenceCalculatorExtensions)
113 - (unsigned)yearsBetweenDate:(NSCalendarDate *)_date {
114 return ABS([self yearOfCommonEra] - [_date yearOfCommonEra]);
117 - (unsigned)monthsBetweenDate:(NSCalendarDate *)_date {
118 NSCalendarDate *start, *end;
119 NSComparisonResult order;
122 order = [self compare:_date];
123 if (order == NSOrderedSame)
125 else if (order == NSOrderedAscending) {
133 yDiff = [end yearOfCommonEra] - [start yearOfCommonEra];
135 unsigned monthsRemaining, monthsToGo;
137 monthsRemaining = 12 - [start monthOfYear];
138 monthsToGo = [end monthOfYear];
140 return monthsRemaining + monthsToGo + (12 * yDiff);
142 /* start and end in same year, calculate plain diff */
143 return [end monthOfYear] - [start monthOfYear];
146 - (unsigned)daysBetweenDate:(NSCalendarDate *)_date {
147 return ABS([self julianNumber] - [_date julianNumber]);
149 @end /* NSCalendarDate (iCalRecurrenceCalculatorExtensions) */