2007-01-31 18:11:29 +01:00
|
|
|
/* iCalTimeZonePeriod.m - this file is part of SOPE
|
|
|
|
*
|
2014-09-24 18:02:12 +02:00
|
|
|
* Copyright (C) 2006-2014 Inverse inc.
|
2007-01-31 18:11:29 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-14 17:07:04 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2007-01-31 18:11:29 +01:00
|
|
|
#import <Foundation/NSCalendarDate.h>
|
|
|
|
#import <Foundation/NSString.h>
|
2007-02-08 17:27:08 +01:00
|
|
|
#import <Foundation/NSTimeZone.h>
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
#import "iCalDateTime.h"
|
2010-04-19 23:05:35 +02:00
|
|
|
#import "iCalByDayMask.h"
|
2010-06-04 22:52:12 +02:00
|
|
|
#import "iCalUTCOffset.h"
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
#import "iCalTimeZonePeriod.h"
|
|
|
|
|
|
|
|
@implementation iCalTimeZonePeriod
|
|
|
|
|
|
|
|
- (Class) classForTag: (NSString *) classTag
|
|
|
|
{
|
|
|
|
Class tagClass;
|
|
|
|
|
|
|
|
if ([classTag isEqualToString: @"RRULE"])
|
|
|
|
tagClass = [iCalRecurrenceRule class];
|
|
|
|
else if ([classTag isEqualToString: @"DTSTART"])
|
|
|
|
tagClass = [iCalDateTime class];
|
2010-06-04 22:52:12 +02:00
|
|
|
else if ([classTag isEqualToString: @"TZOFFSETFROM"]
|
2007-01-31 18:11:29 +01:00
|
|
|
|| [classTag isEqualToString: @"TZOFFSETTO"])
|
2010-06-04 22:52:12 +02:00
|
|
|
tagClass = [iCalUTCOffset class];
|
|
|
|
else if ([classTag isEqualToString: @"TZNAME"])
|
2007-01-31 18:11:29 +01:00
|
|
|
tagClass = [CardElement class];
|
|
|
|
else
|
|
|
|
tagClass = [super classForTag: classTag];
|
|
|
|
|
|
|
|
return tagClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) _secondsOfOffset: (NSString *) offsetName
|
|
|
|
{
|
|
|
|
NSString *offsetTo;
|
|
|
|
BOOL negative;
|
|
|
|
NSRange cursor;
|
|
|
|
unsigned int length;
|
|
|
|
unsigned int seconds;
|
|
|
|
|
|
|
|
seconds = 0;
|
|
|
|
|
|
|
|
offsetTo = [[self uniqueChildWithTag: offsetName]
|
2011-11-14 17:07:04 +01:00
|
|
|
flattenedValuesForKey: @""];
|
2007-01-31 18:11:29 +01:00
|
|
|
length = [offsetTo length];
|
|
|
|
negative = [offsetTo hasPrefix: @"-"];
|
|
|
|
if (negative)
|
|
|
|
{
|
|
|
|
length--;
|
|
|
|
cursor = NSMakeRange(1, 2);
|
|
|
|
}
|
|
|
|
else if ([offsetTo hasPrefix: @"+"])
|
|
|
|
{
|
|
|
|
length--;
|
|
|
|
cursor = NSMakeRange(1, 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cursor = NSMakeRange(0, 2);
|
|
|
|
|
|
|
|
seconds = 3600 * [[offsetTo substringWithRange: cursor] intValue];
|
|
|
|
cursor.location += 2;
|
|
|
|
seconds += 60 * [[offsetTo substringWithRange: cursor] intValue];
|
|
|
|
if (length == 6)
|
|
|
|
{
|
|
|
|
cursor.location += 2;
|
|
|
|
seconds += [[offsetTo substringWithRange: cursor] intValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((negative) ? -seconds : seconds);
|
|
|
|
}
|
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
// - (unsigned int) dayOfWeekFromRruleDay: (iCalWeekDay) day
|
|
|
|
// {
|
|
|
|
// unsigned int dayOfWeek;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
// dayOfWeek = 0;
|
|
|
|
// while (day >> (dayOfWeek + 1))
|
|
|
|
// dayOfWeek++;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
// return dayOfWeek;
|
|
|
|
// }
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2012-10-30 13:49:02 +01:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[startDate release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:11:51 +02:00
|
|
|
- (NSCalendarDate *) startDate
|
|
|
|
{
|
2012-10-30 13:49:02 +01:00
|
|
|
if (!startDate)
|
|
|
|
{
|
|
|
|
startDate = [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"]
|
|
|
|
dateTime];
|
|
|
|
[startDate retain];
|
|
|
|
}
|
|
|
|
return startDate;
|
2009-10-20 22:11:51 +02:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:56:33 +02:00
|
|
|
- (iCalRecurrenceRule *) recurrenceRule
|
|
|
|
{
|
|
|
|
return (iCalRecurrenceRule *) [self firstChildWithTag: @"rrule"];
|
|
|
|
}
|
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
/**
|
|
|
|
* This method returns the date corresponding for to the start of the period
|
|
|
|
* in the year of the reference date.
|
|
|
|
* We assume that a RRULE for a timezone will always be YEARLY with a BYMONTH
|
|
|
|
* and a BYDAY rule.
|
|
|
|
*/
|
2012-10-20 19:27:24 +02:00
|
|
|
- (NSCalendarDate *) _occurrenceForDate: (NSCalendarDate *) refDate
|
|
|
|
byRRule: (iCalRecurrenceRule *) rrule
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
|
|
|
NSCalendarDate *tmpDate;
|
2010-04-19 23:05:35 +02:00
|
|
|
iCalByDayMask *byDayMask;
|
2007-01-31 18:11:29 +01:00
|
|
|
int dayOfWeek, dateDayOfWeek, offset, pos;
|
2009-10-20 22:11:51 +02:00
|
|
|
NSCalendarDate *tzStart;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
byDayMask = [rrule byDayMask];
|
|
|
|
dayOfWeek = 0;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
2010-04-19 23:05:35 +02:00
|
|
|
if (byDayMask == nil)
|
|
|
|
{
|
|
|
|
dayOfWeek = 0;
|
|
|
|
pos = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dayOfWeek = (int)[byDayMask firstDay];
|
|
|
|
pos = [byDayMask firstOccurrence];
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:11:51 +02:00
|
|
|
tzStart = [self startDate];
|
2011-01-14 03:54:33 +01:00
|
|
|
|
2009-10-20 22:11:51 +02:00
|
|
|
[tzStart setTimeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
|
2007-11-12 21:56:56 +01:00
|
|
|
tmpDate = [NSCalendarDate dateWithYear: [refDate yearOfCommonEra]
|
2011-11-14 17:07:04 +01:00
|
|
|
month: [[[rrule byMonth] objectAtIndex: 0] intValue]
|
2015-01-15 21:29:02 +01:00
|
|
|
day: 1
|
|
|
|
hour: [tzStart hourOfDay]
|
2010-10-19 01:06:50 +02:00
|
|
|
minute: [tzStart minuteOfHour] second: 0
|
|
|
|
timeZone: [NSTimeZone timeZoneWithName: @"GMT"]];
|
2015-01-15 21:29:02 +01:00
|
|
|
|
2015-09-14 16:55:50 +02:00
|
|
|
tmpDate = [tmpDate addYear: 0
|
|
|
|
month: ((pos > 0) ? 0 : 1)
|
|
|
|
day: 0
|
|
|
|
hour: 0
|
|
|
|
minute: 0
|
2010-10-19 01:12:04 +02:00
|
|
|
second: 0];
|
2015-01-15 21:29:02 +01:00
|
|
|
|
2010-03-22 18:16:16 +01:00
|
|
|
/* If the day of the time change is "-XSU", we need to determine whether the
|
|
|
|
first day of next month is in the same week. In practice, as most time
|
|
|
|
changes occurs on sundays, it will be false only when that first day is a
|
|
|
|
sunday, but we want to remain algorithmically exact. */
|
2010-10-19 01:12:04 +02:00
|
|
|
dateDayOfWeek = [tmpDate dayOfWeek];
|
2010-03-22 18:16:16 +01:00
|
|
|
if (dateDayOfWeek > dayOfWeek && pos < 0)
|
|
|
|
pos++;
|
|
|
|
|
2015-01-16 15:18:37 +01:00
|
|
|
/* We check if the days of the week are identical. This is important because if they
|
2015-01-15 21:29:02 +01:00
|
|
|
are, "pos" actually includes the first day of tmpDate which means we must decrement
|
2015-01-16 15:18:37 +01:00
|
|
|
pos by 1. This happens for example in the eastern timezone (America/Montreal)
|
|
|
|
in 2015. We have:
|
|
|
|
|
|
|
|
BEGIN:VTIMEZONE
|
|
|
|
TZID:America/Montreal
|
|
|
|
X-LIC-LOCATION:America/Montreal
|
|
|
|
BEGIN:DAYLIGHT
|
|
|
|
TZOFFSETFROM:-0500
|
|
|
|
TZOFFSETTO:-0400
|
|
|
|
TZNAME:EDT
|
|
|
|
DTSTART:19700308T020000
|
|
|
|
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
|
|
|
|
END:DAYLIGHT
|
|
|
|
BEGIN:STANDARD
|
|
|
|
TZOFFSETFROM:-0400
|
|
|
|
TZOFFSETTO:-0500
|
|
|
|
TZNAME:EST
|
|
|
|
DTSTART:19701101T020000
|
|
|
|
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
|
|
|
|
END:STANDARD
|
|
|
|
END:VTIMEZONE
|
|
|
|
|
2015-09-14 16:55:50 +02:00
|
|
|
The time changes occur on a Sunday, but in March, the 1st is a Sunday itself and in November
|
2015-01-16 15:18:37 +01:00
|
|
|
the 1st is also a Sunday. If we don't decrement "pos" by one, tmpDate (which is set to March or November 1st
|
|
|
|
because of "day: 1" will have 14 more days added for March and 7 more days added for November - which will
|
|
|
|
effectively shift the time change by a whole week.
|
2015-09-14 16:55:50 +02:00
|
|
|
|
|
|
|
In Europe/Berlin, we have a different use-case for November. In 2015, November 1st is a Sunday.
|
|
|
|
The time change in November must occur on October 25th but since tmpDate will be November 1st,
|
|
|
|
so a Sunday, dateDayOfWeek will be 0 and dayOfWeek will also be 0 we would decrement tmpDate by 14 days,
|
|
|
|
which is incorrect because it would shift the timezone change one week earlier. We take care about this
|
|
|
|
one with check if pos is greater or equal than 0 and if so, we don't decrement it.
|
|
|
|
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
PRODID:-//Inverse inc.//NONSGML Olson 2014g//EN
|
|
|
|
VERSION:2.0
|
|
|
|
BEGIN:VTIMEZONE
|
|
|
|
TZID:Europe/Berlin
|
|
|
|
X-LIC-LOCATION:Europe/Berlin
|
|
|
|
BEGIN:DAYLIGHT
|
|
|
|
TZOFFSETFROM:+0100
|
|
|
|
TZOFFSETTO:+0200
|
|
|
|
TZNAME:CEST
|
|
|
|
DTSTART:19700329T020000
|
|
|
|
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
|
|
|
|
END:DAYLIGHT
|
|
|
|
BEGIN:STANDARD
|
|
|
|
TZOFFSETFROM:+0200
|
|
|
|
TZOFFSETTO:+0100
|
|
|
|
TZNAME:CET
|
|
|
|
DTSTART:19701025T030000
|
|
|
|
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
|
|
|
|
END:STANDARD
|
|
|
|
END:VTIMEZONE
|
|
|
|
END:VCALENDAR
|
2015-01-16 15:18:37 +01:00
|
|
|
*/
|
2015-09-14 16:55:50 +02:00
|
|
|
if (dayOfWeek == dateDayOfWeek && pos >= 0)
|
2015-01-15 21:29:02 +01:00
|
|
|
pos--;
|
|
|
|
|
2010-03-09 18:06:40 +01:00
|
|
|
offset = (dayOfWeek - dateDayOfWeek) + (pos * 7);
|
2007-01-31 18:11:29 +01:00
|
|
|
tmpDate = [tmpDate addYear: 0 month: 0 day: offset
|
2010-10-19 01:06:50 +02:00
|
|
|
hour: 0 minute: 0 second: 0];
|
2015-01-15 21:29:02 +01:00
|
|
|
|
2007-11-12 21:56:56 +01:00
|
|
|
return tmpDate;
|
|
|
|
}
|
|
|
|
|
2012-10-20 19:27:24 +02:00
|
|
|
- (NSCalendarDate *) occurrenceForDate: (NSCalendarDate *) refDate;
|
2007-11-12 21:56:56 +01:00
|
|
|
{
|
|
|
|
NSCalendarDate *tmpDate;
|
|
|
|
iCalRecurrenceRule *rrule;
|
|
|
|
|
2012-09-25 02:43:12 +02:00
|
|
|
tmpDate = nil;
|
2007-11-12 21:56:56 +01:00
|
|
|
rrule = (iCalRecurrenceRule *) [self uniqueChildWithTag: @"rrule"];
|
2012-09-25 02:43:12 +02:00
|
|
|
|
2007-11-12 21:56:56 +01:00
|
|
|
if ([rrule isVoid])
|
|
|
|
tmpDate
|
|
|
|
= [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"] dateTime];
|
2012-09-25 02:43:12 +02:00
|
|
|
else if ([rrule untilDate] == nil || [refDate compare: [rrule untilDate]] == NSOrderedAscending)
|
2012-10-20 19:27:24 +02:00
|
|
|
tmpDate = [self _occurrenceForDate: refDate byRRule: rrule];
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
return tmpDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) secondsOffsetFromGMT
|
|
|
|
{
|
|
|
|
return [self _secondsOfOffset: @"tzoffsetto"];
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:56:33 +02:00
|
|
|
- (NSComparisonResult) compare: (iCalTimeZonePeriod *) otherPeriod
|
|
|
|
{
|
|
|
|
return [[self startDate] compare: [otherPeriod startDate]];
|
|
|
|
}
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
@end
|