Skip to content

Commit

Permalink
Merge pull request #304 from beriniwlew/master
Browse files Browse the repository at this point in the history
VTimeZone DAYLIGHT and STANDARD collections
  • Loading branch information
rianjs authored Nov 14, 2017
2 parents a9765cd + e7a264d commit ede9d5b
Show file tree
Hide file tree
Showing 19 changed files with 1,108 additions and 23 deletions.
7 changes: 7 additions & 0 deletions v2/ical.NET.UnitTests/CalendarPropertiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@ public void PropertySerialization_Tests()
var serialized = new CalendarSerializer().SerializeToString(calendar);
Assert.IsTrue(serialized.Contains("X-ALT-DESC;"));
}

[Test]
public void PropertySetValueMustAllowNull()
{
var property = new CalendarProperty();
Assert.DoesNotThrow(() => { property.SetValue(null);});
}
}
}
3 changes: 3 additions & 0 deletions v2/ical.NET.UnitTests/ComponentTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using NUnit.Framework;

namespace Ical.Net.UnitTests
Expand All @@ -15,5 +16,7 @@ public void UniqueComponent1()
Assert.IsNull(evt.Created); // We don't want this to be set automatically
Assert.IsNotNull(evt.DtStamp);
}


}
}
26 changes: 26 additions & 0 deletions v2/ical.NET.UnitTests/DataTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ical.Net.DataTypes;
using NUnit.Framework;

namespace Ical.Net.UnitTests
{
[TestFixture]
public class DataTypeTest
{
[Test, Category("DataType")]
public void OrganizerConstructorMustAcceptNull()
{
Assert.DoesNotThrow(()=> {var o = new Organizer(null);});
}

[Test, Category("DataType")]
public void AttachmentConstructorMustAcceptNull()
{
Assert.DoesNotThrow(() => { var o = new Attachment((byte[]) null); });
Assert.DoesNotThrow(() => { var o = new Attachment((string) null); });
}
}
}
2 changes: 2 additions & 0 deletions v2/ical.NET.UnitTests/Ical.Net.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="CollectionHelpersTests.cs" />
<Compile Include="ComponentTest.cs" />
<Compile Include="ConcurrentDeserializationTests.cs" />
<Compile Include="DataTypeTest.cs" />
<Compile Include="DateTimeSerializerTests.cs" />
<Compile Include="SerializationHelpers.cs" />
<Compile Include="SimpleDeserializationTests.cs" />
Expand All @@ -95,6 +96,7 @@
<Compile Include="ListExtensions.cs" />
<Compile Include="ProgramTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VTimeZoneTest.cs" />
<None Include="app.config" />
<Compile Include="RecurrenceTests.cs" />
<Compile Include="SerializationTests.cs" />
Expand Down
252 changes: 252 additions & 0 deletions v2/ical.NET.UnitTests/VTimeZoneTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using Ical.Net.DataTypes;
using Ical.Net.Interfaces.DataTypes;
using Ical.Net.Serialization.iCalendar.Serializers;
using NUnit.Framework;

namespace Ical.Net.UnitTests
{
[TestFixture]
class VTimeZoneTest
{
[Test, Category("VTimeZone")]
public void InvalidTzIdShouldThrowException()
{
Assert.Throws<ArgumentException>(() => { new VTimeZone("shouldFail"); });
}

[Test, Category("VTimeZone")]
public void VTimeZoneFromDateTimeZoneNullZoneShouldThrowException()
{
Assert.Throws<ArgumentException>(() => { CreateTestCalendar("shouldFail"); });
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaPhoenixShouldSerializeProperly()
{
var tzId = "America/Phoenix";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Phoenix"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("RDATE:19670430T020000"), "Daylight savings for Phoenix was not serialized properly.");
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaPhoenixShouldSerializeProperly2()
{
var tzId = "America/Phoenix";
var iCal = CreateTestCalendar(tzId, DateTime.Now, false);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Phoenix"), "Time zone not found in serialization");
Assert.IsFalse(serialized.Contains("BEGIN:DAYLIGHT"), "Daylight savings should not exist for Phoenix.");
}

[Test, Category("VTimeZone")]
public void VTimeZoneUsMountainStandardTimeShouldSerializeProperly()
{
var tzId = "US Mountain Standard Time";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:US Mountain Standard Time"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"));
Assert.IsFalse(serialized.Contains("BEGIN:DAYLIGHT"));
Assert.IsTrue(serialized.Contains("X-LIC-LOCATION"),"X-LIC-LOCATION was not serialized");
}

[Test, Category("VTimeZone")]
public void VTimeZoneCentralAmericaStandardTimeShouldSerializeProperly()
{
var tzId = "Central America Standard Time";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:Central America Standard Time"), "Time zone not found in serialization");
}

[Test, Category("VTimeZone")]
public void VTimeZoneEasternStandardTimeShouldSerializeProperly()
{
var tzId = "Eastern Standard Time";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:Eastern Standard Time"), "Time zone not found in serialization");
}

[Test, Category("VTimeZone")]
public void VTimeZoneEuropeMoscowShouldSerializeProperly()
{
var tzId = "Europe/Moscow";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:Europe/Moscow"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");

Assert.IsTrue(serialized.Contains("TZNAME:MSD"), "MSD was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:MSK"), "MSK info was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:MSD"), "MSD was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:MST"), "MST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:MMT"), "MMT was not serialized");
Assert.IsTrue(serialized.Contains("TZOFFSETFROM:+023017"), "TZOFFSETFROM:+023017 was not serialized");
Assert.IsTrue(serialized.Contains("TZOFFSETTO:+023017"), "TZOFFSETTO:+023017 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19180916T010000"), "DTSTART:19180916T010000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19171228T000000"), "DTSTART:19171228T000000 was not serialized");
Assert.IsTrue(serialized.Contains("RDATE:19991031T030000"), "RDATE:19991031T030000 was not serialized");
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaChicagoShouldSerializeProperly()
{
var tzId = "America/Chicago";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Chicago"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CDT"), "CDT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CST"), "CST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:EST"), "EST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CWT"), "CWT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CPT"), "CPT was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19181027T020000"), "DTSTART:19181027T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19450814T180000"), "DTSTART:19450814T180000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19420209T020000"), "DTSTART:19420209T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19360301T020000"), "DTSTART:19360301T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20070311T020000"), "DTSTART:20070311T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20071104T020000"), "DTSTART:20071104T020000 was not serialized");
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaLosAngelesShouldSerializeProperly()
{
var tzId = "America/Los_Angeles";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Los_Angeles"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BYDAY=2SU"), "BYDAY=2SU was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:PDT"), "PDT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:PST"), "PST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:PPT"), "PPT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:PWT"), "PWT was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19180331T020000"), "DTSTART:19180331T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20071104T020000"), "DTSTART:20071104T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20070311T020000"), "DTSTART:20070311T020000 was not serialized");

//Assert.IsTrue(serialized.Contains("TZURL:http://tzurl.org/zoneinfo/America/Los_Angeles"), "TZURL:http://tzurl.org/zoneinfo/America/Los_Angeles was not serialized");
//Assert.IsTrue(serialized.Contains("RDATE:19600424T010000"), "RDATE:19600424T010000 was not serialized"); // NodaTime doesn't match with what tzurl has
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaAnchorageShouldSerializeProperly()
{
var tzId = "America/Anchorage";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Anchorage"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CAT"), "CAT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:CAWT"), "CAWT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:AHST"), "AHST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:AHDT"), "AHDT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:AKST"), "AKST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:YST"), "YST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:AHDT"), "AHDT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:LMT"), "LMT was not serialized");
Assert.IsTrue(serialized.Contains("RDATE:19731028T020000"), "RDATE:19731028T020000 was not serialized");
Assert.IsTrue(serialized.Contains("RDATE:19801026T020000"), "RDATE:19801026T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19420209T020000"), "DTSTART:19420209T020000 was not serialized");
Assert.IsFalse(serialized.Contains("RDATE:19670401/P1D"), "RDate was not properly serialized for vtimezone, should be RDATE:19670401T000000");
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaEirunepeShouldSerializeProperly()
{
var tzId = "America/Eirunepe";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Eirunepe"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:ACST"), "ACST was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:ACT"), "ACT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:AMT"), "AMT was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19311003T110000"), "DTSTART:19311003T110000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19320401T000000"), "DTSTART:19320401T000000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20080624T000000"), "DTSTART:20080624T000000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:19501201T000000"), "DTSTART:19501201T000000 was not serialized");
// Should not contain the following
Assert.IsFalse(serialized.Contains("RDATE:19501201T000000/P1D"), "The RDATE was not serialized correctly, should be RDATE:19501201T000000");
}

[Test, Category("VTimeZone")]
public void VTimeZoneAmericaDetroitShouldSerializeProperly()
{
var tzId = "America/Detroit";
var iCal = CreateTestCalendar(tzId);
var serializer = new CalendarSerializer();
var serialized = serializer.SerializeToString(iCal);

Assert.IsTrue(serialized.Contains("TZID:America/Detroit"), "Time zone not found in serialization");
Assert.IsTrue(serialized.Contains("BEGIN:STANDARD"), "The standard timezone info was not serialized");
Assert.IsTrue(serialized.Contains("BEGIN:DAYLIGHT"), "The daylight timezone info was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:EDT"), "EDT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:EPT"), "EPT was not serialized");
Assert.IsTrue(serialized.Contains("TZNAME:EST"), "EST was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20070311T020000"), "DTSTART:20070311T020000 was not serialized");
Assert.IsTrue(serialized.Contains("DTSTART:20071104T020000"), "DTSTART:20071104T020000 was not serialized");
}

private static Calendar CreateTestCalendar(string tzId, DateTime? earliestTime = null, bool includeHistoricalData = true)
{
var iCal = new Calendar();

if (earliestTime == null)
{
earliestTime = new DateTime(1900,1,1);
}
iCal.AddTimeZone(tzId, earliestTime.Value, includeHistoricalData);

var calEvent = new Event
{
Description = "Test Recurring Event",
Start = new CalDateTime(DateTime.Now, tzId),
End = new CalDateTime(DateTime.Now.AddHours(1), tzId),
RecurrenceRules = new List<IRecurrencePattern> {new RecurrencePattern(FrequencyType.Daily)}
};
iCal.Events.Add(calEvent);

var calEvent2 = new Event
{
Description = "Test Recurring Event 2",
Start = new CalDateTime(DateTime.Now.AddHours(2), tzId),
End = new CalDateTime(DateTime.Now.AddHours(3), tzId),
RecurrenceRules = new List<IRecurrencePattern> {new RecurrencePattern(FrequencyType.Daily)}
};
iCal.Events.Add(calEvent2);
return iCal;
}
}
}
42 changes: 42 additions & 0 deletions v2/ical.NET/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,41 @@ public virtual RecurrenceEvaluationModeType RecurrenceEvaluationMode
set { Properties.Set("X-DDAY-ICAL-RECURRENCE-EVALUATION-MODE", value); }
}

/// <summary>
/// Adds a system time zone to the iCalendar. This time zone may
/// then be used in date/time objects contained in the
/// calendar.
/// </summary>
/// <param name="tzi">A System.TimeZoneInfo object to add to the calendar.</param>
/// <returns>The time zone added to the calendar.</returns>
public ITimeZone AddTimeZone(TimeZoneInfo tzi)
{
ITimeZone tz = VTimeZone.FromSystemTimeZone(tzi);
this.AddChild(tz);
return tz;
}

public ITimeZone AddTimeZone(TimeZoneInfo tzi, DateTime earliestDateTimeToSupport, bool includeHistoricalData)
{
ITimeZone tz = VTimeZone.FromSystemTimeZone(tzi, earliestDateTimeToSupport, includeHistoricalData);
this.AddChild(tz);
return tz;
}

public ITimeZone AddTimeZone(string tzId)
{
ITimeZone tz = VTimeZone.FromTzId(tzId);
this.AddChild(tz);
return tz;
}

public ITimeZone AddTimeZone(string tzId, DateTime earliestDateTimeToSupport, bool includeHistoricalData)
{
ITimeZone tz = VTimeZone.FromTzId(tzId, earliestDateTimeToSupport, includeHistoricalData);
this.AddChild(tz);
return tz;
}

/// <summary>
/// Adds a time zone to the iCalendar. This time zone may
/// then be used in date/time objects contained in the
Expand All @@ -297,6 +332,13 @@ public ITimeZone AddTimeZone(ITimeZone tz)
return tz;
}

public ITimeZone AddLocalTimeZone(DateTime earliestDateTimeToSupport, bool includeHistoricalData)
{
ITimeZone tz = VTimeZone.FromLocalTimeZone(earliestDateTimeToSupport, includeHistoricalData);
this.AddChild(tz);
return tz;
}

/// <summary>
/// Evaluates component recurrences for the given range of time.
/// <example>
Expand Down
Loading

0 comments on commit ede9d5b

Please sign in to comment.