Skip to content

Commit

Permalink
Remove remaining usages of MinValue and clean up related code (#668)
Browse files Browse the repository at this point in the history
* Replace use of `DateTime.MinValue` in RecurrencePatternEvaluator.

* Simplify and avoid using reflection in CheckMutuallyExclusive()
  • Loading branch information
minichma authored Dec 10, 2024
1 parent fb18177 commit 4ab2415
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Ical.Net/Evaluation/RecurrencePatternEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see
var expandBehavior = RecurrenceUtil.GetExpandBehaviorList(pattern);

var noCandidateIncrementCount = 0;
var candidate = DateTime.MinValue;
DateTime? candidate = null;
var dateCount = 0;
while (maxCount < 0 || dateCount < maxCount)
{
if (pattern.Until is not null && candidate != DateTime.MinValue && candidate > pattern.Until)
if (pattern.Until is not null && candidate > pattern.Until)
{
break;
}

if (candidate != DateTime.MinValue && candidate > periodEnd)
if (candidate > periodEnd)
{
break;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see

if (pattern.Until is null || candidate <= pattern.Until)
{
yield return candidate;
yield return candidate.Value;
dateCount++;
}
}
Expand Down
21 changes: 4 additions & 17 deletions Ical.Net/Serialization/DataTypes/RecurrencePatternSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,11 @@ public virtual void CheckRange(string name, int? value, int min, int max, bool a
}
}

public virtual void CheckMutuallyExclusive<T, TU>(string name1, string name2, T obj1, TU obj2)
public virtual void CheckMutuallyExclusive<T, TU>(string name1, string name2, T? obj1, TU? obj2)
where T : struct
where TU : struct
{
if (Equals(obj1, default(T)) || Equals(obj2, default(TU)))
{
return;
}
// If the object is MinValue instead of its default, consider
// that to be unassigned.

var t1 = obj1.GetType();
var t2 = obj2.GetType();

var fi1 = t1.GetField("MinValue");
var fi2 = t2.GetField("MinValue");

var isMin1 = fi1 != null && obj1.Equals(fi1.GetValue(null));
var isMin2 = fi2 != null && obj2.Equals(fi2.GetValue(null));
if (isMin1 || isMin2)
if ((obj1 == null) || (obj2 == null))
{
return;
}
Expand Down

0 comments on commit 4ab2415

Please sign in to comment.