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"
25 static NSTimeZone *gmt = nil;
26 static inline void _setupGMT(void) {
28 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
31 @implementation NSCalendarDate(ICalValue)
35 static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";
37 - (NSString *)icalStringInGMT {
42 /* set GMT as timezone */
43 oldtz = [[self timeZone] retain];
49 [self setTimeZone:gmt];
53 s = [self descriptionWithCalendarFormat:gmtcalfmt];
55 /* restore old timezone */
57 [self setTimeZone:oldtz];
64 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
67 if (_tz == gmt || _tz == nil)
68 return [self icalStringInGMT];
69 else if ([_tz isEqual:gmt])
70 return [self icalStringInGMT];
73 NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
74 __PRETTY_FUNCTION__, _tz);
75 return [self icalStringInGMT];
79 - (NSString *)icalString {
81 return [self icalStringWithTimeZone:gmt];
84 @end /* NSDate(ICalValue) */
88 #define ABS(a) ((a) < 0 ? -(a) : (a))
91 @implementation NSCalendarDate (iCalRecurrenceCalculatorExtensions)
93 - (unsigned)yearsBetweenDate:(NSCalendarDate *)_date {
94 return ABS([self yearOfCommonEra] - [_date yearOfCommonEra]);
97 - (unsigned)monthsBetweenDate:(NSCalendarDate *)_date {
98 NSCalendarDate *start, *end;
99 NSComparisonResult order;
102 order = [self compare:_date];
103 if (order == NSOrderedSame)
105 else if (order == NSOrderedAscending) {
113 yDiff = [end yearOfCommonEra] - [start yearOfCommonEra];
115 unsigned monthsRemaining, monthsToGo;
117 monthsRemaining = 12 - [start monthOfYear];
118 monthsToGo = [end monthOfYear];
120 return monthsRemaining + monthsToGo + (12 * yDiff);
122 /* start and end in same year, calculate plain diff */
123 return [end monthOfYear] - [start monthOfYear];
126 - (unsigned)daysBetweenDate:(NSCalendarDate *)_date {
127 return ABS([self julianNumber] - [_date julianNumber]);
129 @end /* NSCalendarDate (iCalRecurrenceCalculatorExtensions) */