CronExpression Class

Represents a cron expression.

net10.0, net11.0

public sealed class CronExpression : IRecurrenceRule, IEquatable<CronExpression>, IParsable<CronExpression>, ISpanParsable<CronExpression>

netstandard2.0

public sealed class CronExpression : IRecurrenceRule, IEquatable<CronExpression>

Remarks

Two expressions are equal when their fields select the same values, whatever their text: */15 * * * * is equal to 0,15,30,45 * * * *, and @daily to 0 0 * * *. Expressions that are written with different special values, such as 0 0 29 2 * and 0 0 L 2 *, are not equal even when they happen to match the same dates. An expression holds no time zone: it is evaluated in the time zone given to GetNextOccurrences.

Implements

Methods

NameDescription
Equals(CronExpression?)Determines whether this expression selects the same values as another one in every field.
Equals(object?)Determines whether the specified object is equal to the current object.
GetHashCode()Serves as the default hash function.
GetNextOccurrences(DateTime)
GetNextOccurrences(DateTime, TimeZoneInfo)Gets all occurrences of the expression, reading startDate as a wall-clock time in timeZone.
GetNextOccurrences(DateTimeOffset, TimeZoneInfo)Gets all occurrences of the expression, starting from the instant startDate denotes.
Parse(ReadOnlySpan<char>)
Parse(string)
ToString()Returns the text of the expression, as it was parsed without its leading and trailing spaces and tabs.
TryParse(ReadOnlySpan<char>, CronExpression?)
TryParse(string?, CronExpression?)

Equals overloads

Equals(CronExpression?)

CronExpression.cs

Determines whether this expression selects the same values as another one in every field.

public bool Equals(CronExpression? other)
Parameters
other CronExpression?
The expression to compare with.
Returns

bool true if both expressions select the same values; otherwise, false.

Remarks

The text of the expressions is not compared: */15 * * * * is equal to 0,15,30,45 * * * *.

Equals(object?)

CronExpression.cs

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current object.
Returns

bool true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

CronExpression.cs

Serves as the default hash function.

public override int GetHashCode()

Returns

int A hash code for the current object.

GetNextOccurrences overloads

GetNextOccurrences(DateTime)

CronExpression.cs
public IEnumerable<DateTime> GetNextOccurrences(DateTime startDate)
Parameters
startDate DateTime
Returns

IEnumerable<DateTime>

No documentation is available.

GetNextOccurrences(DateTime, TimeZoneInfo)

CronExpression.cs

Gets all occurrences of the expression, reading startDate as a wall-clock time in timeZone.

public IEnumerable<DateTimeOffset> GetNextOccurrences(DateTime startDate, TimeZoneInfo timeZone)
Parameters
startDate DateTime
The wall-clock time to start generating occurrences from. Its DateTime.Kind is ignored.
timeZone TimeZoneInfo
The time zone the expression is evaluated in.
Returns

IEnumerable<DateTimeOffset> An enumerable sequence of occurrences, each carrying the UTC offset in effect at that occurrence.

Exceptions
ArgumentNullException
timeZone is null.
Remarks

The occurrences keep their wall-clock time across a daylight saving transition, so their UTC offset changes. A local time made invalid or ambiguous by a transition is resolved as RFC 5545 section 3.3.5 requires: an ambiguous time keeps its first occurrence, and an invalid time is read with the UTC offset in effect before the gap.

A time inside the gap of a forward transition therefore moves forward by the length of the gap, which can place its occurrence after the occurrences of the times that follow it: the occurrences are still returned in increasing order. An occurrence that denotes the same instant as another one, as the hour skipped by a forward transition does for an hourly expression, is returned once. Across a backward transition, the instants of the repeated hour are skipped.

An occurrence outside the range of DateTimeOffset is not returned: the enumeration ends at the first one after the range.

GetNextOccurrences(DateTimeOffset, TimeZoneInfo)

CronExpression.cs

Gets all occurrences of the expression, starting from the instant startDate denotes.

public IEnumerable<DateTimeOffset> GetNextOccurrences(DateTimeOffset startDate, TimeZoneInfo timeZone)
Parameters
startDate DateTimeOffset
The instant to start generating occurrences from.
timeZone TimeZoneInfo
The time zone the expression is evaluated in.
Returns

IEnumerable<DateTimeOffset> An enumerable sequence of occurrences, each carrying the UTC offset in effect at that occurrence.

Exceptions
ArgumentNullException
timeZone is null.
Remarks

Every occurrence at or after startDate is returned, and none before it. This includes an occurrence whose wall-clock time is before the reading of startDate, but which a forward transition moves after it, and excludes the first reading of a wall-clock time in the hour repeated by a backward transition when startDate is in its second pass.

The resolution of invalid and ambiguous local times is the same as for the DateTime overload.

Parse overloads

Parse(ReadOnlySpan<char>)

CronExpression.cs
public static CronExpression Parse(ReadOnlySpan<char> expression)
Parameters
expression ReadOnlySpan<char>
Returns

CronExpression

No documentation is available.

Parse(string)

CronExpression.cs
public static CronExpression Parse(string expression)
Parameters
expression string
Returns

CronExpression

No documentation is available.

Returns the text of the expression, as it was parsed without its leading and trailing spaces and tabs.

public override string ToString()

Returns

string The text of the expression.

TryParse overloads

TryParse(ReadOnlySpan<char>, CronExpression?)

CronExpression.cs
public static bool TryParse(ReadOnlySpan<char> expression, out CronExpression? cronExpression)
Parameters
expression ReadOnlySpan<char>
cronExpression CronExpression?
Returns

bool

No documentation is available.

TryParse(string?, CronExpression?)

CronExpression.cs
public static bool TryParse(string? expression, out CronExpression? cronExpression)
Parameters
expression string?
cronExpression CronExpression?
Returns

bool

No documentation is available.

Operators

NameDescription
operator ==(CronExpression?, CronExpression?)Determines whether two expressions select the same values in every field.
operator !=(CronExpression?, CronExpression?)Determines whether two expressions select different values in at least one field.

operator ==(CronExpression?, CronExpression?)

CronExpression.cs

Determines whether two expressions select the same values in every field.

public static bool operator ==(CronExpression? left, CronExpression? right)

Parameters

left CronExpression?
right CronExpression?

Returns

bool

operator !=(CronExpression?, CronExpression?)

CronExpression.cs

Determines whether two expressions select different values in at least one field.

public static bool operator !=(CronExpression? left, CronExpression? right)

Parameters

left CronExpression?
right CronExpression?

Returns

bool