Skip to content
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
23 changes: 12 additions & 11 deletions Ical.Net.Tests/RecurrenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
Expand Down Expand Up @@ -3317,19 +3318,19 @@ public void AddExDateToEventAfterGetOccurrencesShouldRecomputeResult()
Assert.That(occurrences, Has.Count.EqualTo(5));

var exDate = _now.AddDays(1);
e.ExceptionDates.Add(new CalDateTime(exDate, false));
e.ExceptionDates.Add(exDate);
occurrences = e.GetOccurrences(searchStart, searchEnd).ToList();
Assert.That(occurrences, Has.Count.EqualTo(4));

//Specifying just a date should "black out" that date
var excludeTwoDaysFromNow = _now.AddDays(2).Date;
e.ExceptionDates.Add(new CalDateTime(excludeTwoDaysFromNow, false));
var excludeTwoDaysFromNow = new CalDateTime(_now.Date).AddDays(2);
e.ExceptionDates.Add(excludeTwoDaysFromNow);
occurrences = e.GetOccurrences(searchStart, searchEnd).ToList();
Assert.That(occurrences, Has.Count.EqualTo(3));
}

private static readonly DateTime _now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
private static readonly DateTime _later = _now.AddHours(1);
private static readonly CalDateTime _now = CalDateTime.Now;
private static readonly CalDateTime _later = _now.AddHours(1);
private static CalendarEvent GetEventWithRecurrenceRules()
{
var dailyForFiveDays = new RecurrencePattern(FrequencyType.Daily, 1)
Expand Down Expand Up @@ -3381,18 +3382,18 @@ public void ExDateTimeZone_Tests()

var e = new CalendarEvent
{
DtStart = new CalDateTime(_now, tzid),
DtEnd = new CalDateTime(_later, tzid),
DtStart = new CalDateTime(_now.Date, _now.Time, tzid),
DtEnd = new CalDateTime(_later.Date, _later.Time, tzid),
RecurrenceRules = new List<RecurrencePattern> { rrule },
};

e.ExceptionDates.Add(new CalDateTime(_now.AddDays(1), tzid));
e.ExceptionDates.Add(new CalDateTime(_now.Date, _now.Time, tzid).AddDays(1));

var serialized = SerializationHelpers.SerializeToString(e);
const string expected = "TZID=Europe/Stockholm";
Assert.That(Regex.Matches(serialized, expected), Has.Count.EqualTo(3));

e.ExceptionDates.Add(new CalDateTime(_now.AddDays(2), tzid));
e.ExceptionDates.Add(new CalDateTime(_now.Date, _now.Time, tzid).AddDays(2));
serialized = SerializationHelpers.SerializeToString(e);
Assert.That(Regex.Matches(serialized, expected), Has.Count.EqualTo(3));
}
Expand Down Expand Up @@ -3458,8 +3459,8 @@ private static CalendarEvent GetSimpleEvent()
{
var e = new CalendarEvent
{
DtStart = new CalDateTime(_now, _tzid),
DtEnd = new CalDateTime(_later, _tzid),
DtStart = new CalDateTime(_now.Date, _now.Time, _tzid),
DtEnd = new CalDateTime(_later.Date, _later.Time, _tzid),
};
return e;
}
Expand Down
18 changes: 9 additions & 9 deletions Ical.Net.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace Ical.Net.Tests;
[TestFixture]
public class SerializationTests
{
private static readonly DateTime _nowTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
private static readonly DateTime _later = _nowTime.AddHours(1);
private static readonly CalDateTime _nowTime = CalDateTime.Now;
private static readonly CalDateTime _later = _nowTime.AddHours(1);
private static CalendarSerializer GetNewSerializer() => new CalendarSerializer();
private static string SerializeToString(Calendar c) => GetNewSerializer().SerializeToString(c);
private static string SerializeToString(CalendarEvent e) => SerializeToString(new Calendar { Events = { e } });
private static CalendarEvent GetSimpleEvent() => new CalendarEvent { DtStart = new CalDateTime(_nowTime), Duration = (_later - _nowTime).ToDurationExact() };
private static Calendar UnserializeCalendar(string s) => Calendar.Load(s);
private static CalendarEvent GetSimpleEvent() => new CalendarEvent { DtStart = new CalDateTime(_nowTime), Duration = (_later.Value - _nowTime.Value).ToDurationExact() };
private static Calendar DeserializeCalendar(string s) => Calendar.Load(s);

internal static void CompareCalendars(Calendar cal1, Calendar cal2)
{
Expand Down Expand Up @@ -382,7 +382,7 @@ public void EventStatusAllCaps()
var serialized = SerializeToString(e);
Assert.That(serialized.Contains(EventStatus.Confirmed, EventStatus.Comparison), Is.True);

var calendar = UnserializeCalendar(serialized);
var calendar = DeserializeCalendar(serialized);
var eventStatus = calendar.Events.First().Status;
Assert.That(string.Equals(EventStatus.Confirmed, eventStatus, EventStatus.Comparison), Is.True);
}
Expand All @@ -399,7 +399,7 @@ public void ToDoStatusAllCaps()
var serialized = SerializeToString(c);
Assert.That(serialized.Contains(TodoStatus.NeedsAction, TodoStatus.Comparison), Is.True);

var calendar = UnserializeCalendar(serialized);
var calendar = DeserializeCalendar(serialized);
var status = calendar.Todos.First().Status;
Assert.That(string.Equals(TodoStatus.NeedsAction, status, TodoStatus.Comparison), Is.True);
}
Expand All @@ -416,7 +416,7 @@ public void JournalStatusAllCaps()
var serialized = SerializeToString(c);
Assert.That(serialized.Contains(JournalStatus.Final, JournalStatus.Comparison), Is.True);

var calendar = UnserializeCalendar(serialized);
var calendar = DeserializeCalendar(serialized);
var status = calendar.Journals.First().Status;
Assert.That(string.Equals(JournalStatus.Final, status, JournalStatus.Comparison), Is.True);
}
Expand Down Expand Up @@ -507,8 +507,8 @@ public void TestRRuleUntilSerialization()
const string someTz = "Europe/Volgograd";
var e = new CalendarEvent
{
Start = new CalDateTime(_nowTime, someTz),
End = new CalDateTime(_nowTime.AddHours(1), someTz),
Start = _nowTime.ToTimeZone(someTz),
End = _nowTime.AddHours(1).ToTimeZone(someTz),
RecurrenceRules = new List<RecurrencePattern> { rrule },
};
var c = new Calendar
Expand Down
11 changes: 6 additions & 5 deletions Ical.Net/CalendarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Globalization;
using Ical.Net.DataTypes;

namespace Ical.Net;

Expand All @@ -13,7 +14,7 @@ public static class CalendarExtensions
/// <summary>
/// Calculate the week number according to ISO.8601, as required by RFC 5545.
/// </summary>
public static int GetIso8601WeekOfYear(this System.Globalization.Calendar calendar, DateTime time, DayOfWeek firstDayOfWeek)
public static int GetIso8601WeekOfYear(this System.Globalization.Calendar calendar, CalDateTime time, DayOfWeek firstDayOfWeek)
{
// A week is defined as a
// seven day period, starting on the day of the week defined to be
Expand All @@ -24,7 +25,7 @@ public static int GetIso8601WeekOfYear(this System.Globalization.Calendar calend
// We add 3 to make sure the test date is in the 'right' year, because
// otherwise we might end up with week 53 in a year that only has 52.
var tTest = GetStartOfWeek(time, firstDayOfWeek).AddDays(3);
var res = calendar.GetWeekOfYear(tTest, CalendarWeekRule.FirstFourDayWeek, firstDayOfWeek);
var res = calendar.GetWeekOfYear(tTest.Value, CalendarWeekRule.FirstFourDayWeek, firstDayOfWeek);

return res;
}
Expand All @@ -33,7 +34,7 @@ public static int GetIso8601WeekOfYear(this System.Globalization.Calendar calend
/// Calculate and return the date that represents the first day of the week the given date is
/// in, according to the week numbering required by RFC 5545.
/// </summary>
private static DateTime GetStartOfWeek(this DateTime t, DayOfWeek firstDayOfWeek)
private static CalDateTime GetStartOfWeek(this CalDateTime t, DayOfWeek firstDayOfWeek)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could also be DateOnly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

see above

{
var t0 = ((int) firstDayOfWeek) % 7;
var tn = ((int) t.DayOfWeek) % 7;
Expand All @@ -49,7 +50,7 @@ private static DateTime GetStartOfWeek(this DateTime t, DayOfWeek firstDayOfWeek
/// E.g. for `2019-12-31` with first day of the week being Monday, the method will return 2020,
/// because the week that contains `2019-12-31` is the first week of 2020.
/// </remarks>
public static int GetIso8601YearOfWeek(this System.Globalization.Calendar calendar, DateTime time, DayOfWeek firstDayOfWeek)
public static int GetIso8601YearOfWeek(this System.Globalization.Calendar calendar, CalDateTime time, DayOfWeek firstDayOfWeek)
{
var year = time.Year;
if ((time.Month >= 12) && (calendar.GetIso8601WeekOfYear(time, firstDayOfWeek) == 1))
Expand All @@ -66,7 +67,7 @@ public static int GetIso8601YearOfWeek(this System.Globalization.Calendar calend
public static int GetIso8601WeeksInYear(this System.Globalization.Calendar calendar, int year, DayOfWeek firstDayOfWeek)
{
// The last week of the year is the week that contains the 4th-last day of the year (which is the 28th of December in Gregorian Calendar).
var testTime = new DateTime(year + 1, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).AddDays(-4);
var testTime = new CalDateTime(year + 1, 1, 1, 0, 0, 0, null).AddDays(-4);
return calendar.GetIso8601WeekOfYear(testTime, firstDayOfWeek);
}
}
2 changes: 1 addition & 1 deletion Ical.Net/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Evaluator()
Calendar = CultureInfo.CurrentCulture.Calendar;
}

protected void IncrementDate(ref DateTime dt, RecurrencePattern pattern, int interval)
protected void IncrementDate(ref CalDateTime dt, RecurrencePattern pattern, int interval)
{
if (interval == 0)
return;
Expand Down
Loading