RecurrenceSet Class
Definition
- Namespace
- Bodu.Globalization.Recurrence
- Assembly
- Bodu.Globalization.Recurrence.dll
- Package
- Bodu.Globalization.Recurrence 1.0.0
Represents a composed set of recurring instants: one or more RecurrenceRule streams anchored at a
common start, merged with explicit recurrence dates (RDATE) and with exception dates (EXDATE) removed.
public sealed class RecurrenceSet : IEquatable<RecurrenceSet>, IFormattable, IParsable<RecurrenceSet>
- Inheritance
-
RecurrenceSet
- Implements
- Inherited Members
- Extension Methods
Remarks
The occurrence set is the ascending, duplicate-free union of every rule expansion and every explicit date, less any instant that matches an exception date. Each rule is anchored at Start, so the start instant is emitted when a rule (or an explicit date) produces it - it is not added implicitly.
The set can be built programmatically through the constructor, or parsed from an iCalendar property block with Parse(string).
Constructors
RecurrenceSet(DateTime, IEnumerable<RecurrenceRule>, IEnumerable<DateTime>?, IEnumerable<DateTime>?)
Initializes a new instance of the RecurrenceSet class.
public RecurrenceSet(DateTime start, IEnumerable<RecurrenceRule> rules, IEnumerable<DateTime>? dates = null, IEnumerable<DateTime>? exceptionDates = null)
Parameters
startDateTimeThe common start instant the rules are anchored to.
rulesIEnumerable<RecurrenceRule>The recurrence rules; may be empty when explicit dates are supplied.
datesIEnumerable<DateTime>The explicit recurrence dates to add, or null for none.
exceptionDatesIEnumerable<DateTime>The exception dates to remove, or null for none.
Exceptions
- ArgumentNullException
Thrown when
rulesis null.- ArgumentException
Thrown when neither a rule nor an explicit date is supplied.
Properties
Dates
Gets the explicit recurrence dates added to the occurrence set.
public IReadOnlyList<DateTime> Dates { get; }
Property Value
- IReadOnlyList<DateTime>
The explicit dates in ascending order.
ExceptionDates
Gets the exception dates removed from the occurrence set.
public IReadOnlyList<DateTime> ExceptionDates { get; }
Property Value
- IReadOnlyList<DateTime>
The exception dates in ascending order.
Rules
Gets the recurrence rules that contribute to the occurrence set.
public IReadOnlyList<RecurrenceRule> Rules { get; }
Property Value
- IReadOnlyList<RecurrenceRule>
The rules, in the order supplied.
Start
Gets the common start instant the rules are anchored to.
public DateTime Start { get; }
Property Value
- DateTime
The start (
DTSTART) instant.
Methods
Equals(RecurrenceSet?)
Determines whether this set is equal to another set by comparing the start, rules, dates, and exception dates.
public bool Equals(RecurrenceSet? other)
Parameters
otherRecurrenceSetThe set to compare with this instance.
Returns
Equals(object?)
Determines whether this set is equal to another object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with this instance.
Returns
- bool
true when
objis a RecurrenceSet equal to this instance; otherwise false.
GetHashCode()
Returns a hash code derived from the set's start and component counts.
public override int GetHashCode()
Returns
- int
A hash code consistent with Equals(RecurrenceSet?).
GetNextOccurrence(DateTime, bool)
Returns the first occurrence of the set that falls after the specified instant.
public DateTime? GetNextOccurrence(DateTime after, bool inclusive = false)
Parameters
afterDateTimeThe instant the returned occurrence must follow.
inclusivebooltrue to allow an occurrence exactly equal to
after; otherwise the occurrence must be strictly later.
Returns
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
GetNextOccurrence(DateTimeOffset, bool)
Returns the first occurrence of the set that falls after the specified instant, preserving its offset.
public DateTimeOffset? GetNextOccurrence(DateTimeOffset after, bool inclusive = false)
Parameters
afterDateTimeOffsetThe instant the returned occurrence must follow.
inclusivebooltrue to allow an occurrence exactly equal to
after; otherwise the occurrence must be strictly later.
Returns
- DateTimeOffset?
The next occurrence carrying the offset of
after, or null when the set produces none.
Remarks
The set's start and dates are wall-clock values; the query interprets them in the offset of
after and performs no other offset conversion.
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
GetOccurrences()
Enumerates the occurrences of the set in ascending chronological order.
public IEnumerable<DateTime> GetOccurrences()
Returns
- IEnumerable<DateTime>
The ascending, duplicate-free occurrences with exception dates removed. The sequence is unbounded when any contributing rule is unbounded; use GetOccurrences(DateTime, DateTime) or Take<TSource>(IEnumerable<TSource>, int) to bound it.
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
GetOccurrences(DateTime, DateTime)
Enumerates the occurrences of the set that fall within an inclusive window.
public IEnumerable<DateTime> GetOccurrences(DateTime from, DateTime to)
Parameters
fromDateTimeThe inclusive lower bound of the window.
toDateTimeThe inclusive upper bound of the window.
Returns
- IEnumerable<DateTime>
The occurrences within
[from, to]in ascending chronological order.
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
GetPreviousOccurrence(DateTime, bool)
Returns the last occurrence of the set that falls before the specified instant.
public DateTime? GetPreviousOccurrence(DateTime before, bool inclusive = false)
Parameters
beforeDateTimeThe instant the returned occurrence must precede.
inclusivebooltrue to allow an occurrence exactly equal to
before; otherwise the occurrence must be strictly earlier.
Returns
Remarks
Due-ness evaluation is a previous-occurrence comparison - typically
lastCompleted < GetPreviousOccurrence(now, inclusive: true) - so missed occurrences coalesce
structurally: the answer is a single instant, never a backlog.
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
GetPreviousOccurrence(DateTimeOffset, bool)
Returns the last occurrence of the set that falls before the specified instant, preserving its offset.
public DateTimeOffset? GetPreviousOccurrence(DateTimeOffset before, bool inclusive = false)
Parameters
beforeDateTimeOffsetThe instant the returned occurrence must precede.
inclusivebooltrue to allow an occurrence exactly equal to
before; otherwise the occurrence must be strictly earlier.
Returns
- DateTimeOffset?
The previous occurrence carrying the offset of
before, or null when none precedes it.
Remarks
The set's start and dates are wall-clock values; the query interprets them in the offset of
before and performs no other offset conversion.
Exceptions
- NotSupportedException
Thrown when a contributing rule uses a sub-daily frequency.
Parse(string)
Parses an iCalendar property block containing a DTSTART line, one or more RRULE lines, and
optional RDATE / EXDATE lines.
public static RecurrenceSet Parse(string s)
Parameters
sstringThe property-block text. Lines other than the recognized recurrence properties are ignored.
Returns
- RecurrenceSet
The parsed set.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when the text is not a valid recurrence-set property block.
ToString()
Returns the canonical iCalendar property-block text of the set: a DTSTART line, one RRULE line per
rule, and RDATE / EXDATE lines when explicit or exception dates are present.
public override string ToString()
Returns
- string
The canonical property-block text, which round-trips through Parse(string).
Remarks
Lines are separated by CRLF per RFC 5545, with no trailing line break. Explicit and exception dates each render as a single comma-separated line in ascending order.
ToString(string?)
Returns the canonical iCalendar property-block text of the set.
public string ToString(string? format)
Parameters
Returns
- string
The canonical property-block text.
Exceptions
- FormatException
Thrown when
formatis not a supported specifier.
ToString(string?, IFormatProvider?)
Returns the canonical iCalendar property-block text of the set.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringThe format specifier. Only the general specifier (
"G"or null) is supported.formatProviderIFormatProviderUnused; property-block text is culture-invariant.
Returns
- string
The canonical property-block text.
Exceptions
- FormatException
Thrown when
formatis not a supported specifier.
TryParse(string?, out RecurrenceSet)
Attempts to parse an iCalendar property block.
public static bool TryParse(string? s, out RecurrenceSet result)
Parameters
sstringThe property-block text.
resultRecurrenceSetThe parsed set, or null on failure.
Returns
TryParse(string?, out RecurrenceSet, out string?)
Attempts to parse an iCalendar property block, reporting the parse defect on failure.
public static bool TryParse(string? s, out RecurrenceSet result, out string? failureMessage)
Parameters
sstringThe property-block text.
resultRecurrenceSetThe parsed set, or null on failure.
failureMessagestringnull on success; otherwise a message naming the specific defect, suitable for surfacing to the user verbatim.
Returns
Explicit Interface Implementations
Parse(string, IFormatProvider?)
Parses an iCalendar property block.
static RecurrenceSet Parse(string s, IFormatProvider? provider)
Parameters
sstringThe property-block text. Lines other than the recognized recurrence properties are ignored.
providerIFormatProviderUnused; property-block text is culture-invariant.
Returns
- RecurrenceSet
The parsed set.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when the text is not a valid recurrence-set property block.
TryParse(string?, IFormatProvider?, out RecurrenceSet)
Attempts to parse an iCalendar property block.
static bool TryParse(string? s, IFormatProvider? provider, out RecurrenceSet result)
Parameters
sstringThe property-block text.
providerIFormatProviderUnused; property-block text is culture-invariant.
resultRecurrenceSetThe parsed set, or null on failure.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |