From c2b6779a2791d1663c84b704aa3603f9d8ff822f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20B=C3=BChlmann?= Date: Mon, 23 Oct 2017 13:40:38 +0200 Subject: [PATCH] Ignore timezone if two periods are compared for equality This solves the case where exception dates are ignored due to different time zone than the recurring event. --- .../Ical.Net/Ical.Net/DataTypes/Period.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/net-core/Ical.Net/Ical.Net/DataTypes/Period.cs b/net-core/Ical.Net/Ical.Net/DataTypes/Period.cs index 3d1e18ef7..6676c8288 100644 --- a/net-core/Ical.Net/Ical.Net/DataTypes/Period.cs +++ b/net-core/Ical.Net/Ical.Net/DataTypes/Period.cs @@ -1,7 +1,7 @@ -using System; using Ical.Net.Interfaces.DataTypes; using Ical.Net.Interfaces.General; using Ical.Net.Serialization.iCalendar.Serializers.DataTypes; +using System; namespace Ical.Net.DataTypes { @@ -11,7 +11,7 @@ public class Period : EncodableDataType, IComparable public Period() { } public Period(IDateTime occurs) - : this(occurs, default(TimeSpan)) {} + : this(occurs, default(TimeSpan)) { } public Period(IDateTime start, IDateTime end) { @@ -60,21 +60,30 @@ public override void CopyFrom(ICopyable obj) Duration = p.Duration; } - protected bool Equals(Period other) => Equals(StartTime, other.StartTime) && Equals(EndTime, other.EndTime) && Duration.Equals(other.Duration); + protected bool Equals(Period other) => IsSameInstant(StartTime, other.StartTime) && IsSameInstant(EndTime, other.EndTime) && Duration.Equals(other.Duration); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; - return obj.GetType() == GetType() && Equals((Period) obj); + return obj.GetType() == GetType() && Equals((Period)obj); + } + + public static bool IsSameInstant(IDateTime a, IDateTime b) + { + if (a == null || b == null) + { + return a == b; + } + return Equals(a.AsUtc, b.AsUtc); } public override int GetHashCode() { unchecked { - var hashCode = StartTime?.GetHashCode() ?? 0; - hashCode = (hashCode * 397) ^ (EndTime?.GetHashCode() ?? 0); + var hashCode = StartTime?.AsUtc.GetHashCode() ?? 0; + hashCode = (hashCode * 397) ^ (EndTime?.AsUtc.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ Duration.GetHashCode(); return hashCode; }