Skip to content

Commit 7e451ba

Browse files
authored
Change RecurrencePattern.Until to Nullable<DateTime> (#666)
1 parent 1330b86 commit 7e451ba

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

Ical.Net.Tests/RecurrenceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3570,7 +3570,7 @@ public void UntilTimeZoneSerializationTests(string tzId, DateTimeKind expectedKi
35703570

35713571
Assert.That(serialized.Contains(expectedContains), Is.True);
35723572

3573-
var deserializedKind = Calendar.Load(serialized).Events.First().RecurrenceRules.First().Until.Kind;
3573+
var deserializedKind = Calendar.Load(serialized).Events.First().RecurrenceRules.First().Until?.Kind;
35743574

35753575
Assert.That(deserializedKind, Is.EqualTo(expectedKind));
35763576
}

Ical.Net/DataTypes/RecurrencePattern.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license.
44
//
55

6+
#nullable enable
67
using System;
78
using System.Collections.Generic;
89
using System.IO;
@@ -26,20 +27,7 @@ public class RecurrencePattern : EncodableDataType
2627
#pragma warning restore 0618
2728
public FrequencyType Frequency { get; set; }
2829

29-
private DateTime _until = DateTime.MinValue;
30-
public DateTime Until
31-
{
32-
get => _until;
33-
set
34-
{
35-
if (_until == value && _until.Kind == value.Kind)
36-
{
37-
return;
38-
}
39-
40-
_until = value;
41-
}
42-
}
30+
public DateTime? Until { get; set; }
4331

4432
public int Count { get; set; } = int.MinValue;
4533

@@ -146,7 +134,8 @@ public RecurrencePattern(string value) : this()
146134
return;
147135
}
148136
var serializer = new RecurrencePatternSerializer();
149-
CopyFrom(serializer.Deserialize(new StringReader(value)) as ICopyable);
137+
if (serializer.Deserialize(new StringReader(value)) is ICopyable deserialized)
138+
CopyFrom(deserialized);
150139
}
151140

152141
public override string ToString()
@@ -174,7 +163,7 @@ protected bool Equals(RecurrencePattern other) => (Interval == other.Interval)
174163
&& CollectionEquals(ByMonth, other.ByMonth)
175164
&& CollectionEquals(BySetPosition, other.BySetPosition);
176165

177-
public override bool Equals(object obj)
166+
public override bool Equals(object? obj)
178167
{
179168
if (ReferenceEquals(null, obj)) return false;
180169
if (ReferenceEquals(this, obj)) return true;
@@ -237,4 +226,4 @@ public override void CopyFrom(ICopyable obj)
237226
}
238227

239228
private static bool CollectionEquals<T>(IEnumerable<T> c1, IEnumerable<T> c2) => c1.SequenceEqual(c2);
240-
}
229+
}

Ical.Net/Evaluation/RecurrencePatternEvaluator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ private RecurrencePattern ProcessRecurrencePattern(IDateTime referenceDate)
2929
r.CopyFrom(Pattern);
3030

3131
// Convert the UNTIL value to one that matches the same time information as the reference date
32-
if (r.Until != DateTime.MinValue)
32+
if (r.Until is not null)
3333
{
34-
r.Until = MatchTimeZone(referenceDate, r.Until);
34+
r.Until = MatchTimeZone(referenceDate, r.Until.Value);
3535
}
3636

3737
if (referenceDate.HasTime)
@@ -243,10 +243,10 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see
243243

244244
var noCandidateIncrementCount = 0;
245245
var candidate = DateTime.MinValue;
246-
int dateCount = 0;
246+
var dateCount = 0;
247247
while (maxCount < 0 || dateCount < maxCount)
248248
{
249-
if (pattern.Until != DateTime.MinValue && candidate != DateTime.MinValue && candidate > pattern.Until)
249+
if (pattern.Until is not null && candidate != DateTime.MinValue && candidate > pattern.Until)
250250
{
251251
break;
252252
}
@@ -291,7 +291,7 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see
291291
continue;
292292
}
293293

294-
if (pattern.Until == DateTime.MinValue || candidate <= pattern.Until)
294+
if (pattern.Until is null || candidate <= pattern.Until)
295295
{
296296
yield return candidate;
297297
dateCount++;

Ical.Net/Serialization/DataTypes/RecurrencePatternSerializer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public virtual void CheckMutuallyExclusive<T, TU>(string name1, string name2, T
8383
// that to be unassigned.
8484

8585
var t1 = obj1.GetType();
86+
var t2 = obj2.GetType();
8687

8788
var fi1 = t1.GetField("MinValue");
88-
var fi2 = t1.GetField("MinValue");
89+
var fi2 = t2.GetField("MinValue");
8990

9091
var isMin1 = fi1 != null && obj1.Equals(fi1.GetValue(null));
9192
var isMin2 = fi2 != null && obj2.Equals(fi2.GetValue(null));
@@ -142,13 +143,13 @@ public override string SerializeToString(object obj)
142143
values.Add("INTERVAL=" + interval);
143144
}
144145

145-
if (recur.Until != DateTime.MinValue)
146+
if (recur.Until is not null)
146147
{
147148
var serializer = factory.Build(typeof(IDateTime), SerializationContext) as IStringSerializer;
148149
if (serializer != null)
149150
{
150-
var until = new CalDateTime(DateOnly.FromDateTime(recur.Until), TimeOnly.FromDateTime(recur.Until),
151-
recur.Until.Kind == DateTimeKind.Utc ? "UTC" : null);
151+
var until = new CalDateTime(DateOnly.FromDateTime(recur.Until.Value), TimeOnly.FromDateTime(recur.Until.Value),
152+
recur.Until.Value.Kind == DateTimeKind.Utc ? "UTC" : null);
152153

153154
values.Add("UNTIL=" + serializer.SerializeToString(until));
154155
}

0 commit comments

Comments
 (0)