Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with getting occurrences at the border of years #244

Merged
merged 1 commit into from Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v2/Ical.Net.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Ical.Net</id>
<version>2.2.32</version>
<version>2.2.33</version>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In future PRs, please don't change the release number)

<title>Ical.Net</title>
<authors>Rian Stockbower, Douglas Day, M. David Peterson</authors>
<owners>Rian Stockbower</owners>
Expand Down
41 changes: 41 additions & 0 deletions v2/ical.NET.UnitTests/GetOccurrenceTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ical.Net.DataTypes;
using Ical.Net.Interfaces.DataTypes;
using Ical.Net.Utility;
using NUnit.Framework;

namespace Ical.Net.UnitTests
Expand Down Expand Up @@ -51,6 +54,44 @@ public void WrongDurationTest()
Assert.AreEqual(secondEndCopy, secondOccurrence.Period.EndTime);
}

[Test]
public void SkippedOccurrenceOnWeeklyPattern()
{
const int evaluationsCount = 1000;
var eventStart = new CalDateTime(new DateTime(2016, 1, 1, 10, 0, 0, DateTimeKind.Utc));
var eventEnd = new CalDateTime(new DateTime(2016, 1, 1, 11, 0, 0, DateTimeKind.Utc));
var vEvent = new Event
{
DtStart = eventStart,
DtEnd = eventEnd,
};

var pattern = new RecurrencePattern
{
Frequency = FrequencyType.Weekly,
ByDay = new List<IWeekDay> { new WeekDay(DayOfWeek.Friday) }

};
vEvent.RecurrenceRules.Add(pattern);
var calendar = new Calendar();
calendar.Events.Add(vEvent);

var intervalStart = eventStart;
var intervalEnd = intervalStart.AddDays(7 * evaluationsCount);

var occurrences = RecurrenceUtil.GetOccurrences(vEvent, intervalStart, intervalEnd, false)
.Select(o => o.Period.StartTime)
.OrderBy(dt => dt)
.ToList();
Assert.AreEqual(evaluationsCount, occurrences.Count);
for (DateTime currentOccurrence = intervalStart.AsUtc;
currentOccurrence.CompareTo(intervalEnd.AsUtc) < 0; currentOccurrence = currentOccurrence.AddDays(7))
{
Assert.IsTrue(occurrences.Contains(new CalDateTime(currentOccurrence)),
string.Format("Collection does not contain {0}, but it is a {1}", currentOccurrence, currentOccurrence.DayOfWeek));
}
}

[Test]
public void EnumerationChangedException()
{
Expand Down
14 changes: 13 additions & 1 deletion v2/ical.NET/Evaluation/RecurrencePatternEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,22 @@ private List<DateTime> GetAbsWeekDays(DateTime date, IWeekDay weekDay, IRecurren
date = date.AddDays(1);
}

while (Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, pattern.FirstDayOfWeek) == weekNo)
var nextWeekNo = Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,
pattern.FirstDayOfWeek);

var currentWeekNo = Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,
pattern.FirstDayOfWeek);
while (currentWeekNo == weekNo ||
//When we manage weekly recurring pattern and we have boundary case:
//Weekdays: 31.12, 01.01, 02.01, 03.01, 04.01, 05.01, 06.01
//31.12 - It's a 53th week number, but all another are 1st week number.
//So, we should add exception rule for this situation(only for weekly events).
(nextWeekNo < weekNo && currentWeekNo == nextWeekNo && pattern.Frequency == FrequencyType.Weekly))
{
days.Add(date);
date = date.AddDays(7);
currentWeekNo = Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,
pattern.FirstDayOfWeek);
}
}
else if (pattern.Frequency == FrequencyType.Monthly || pattern.ByMonth.Count > 0)
Expand Down