This document provides a comprehensive reference for the NaturalCron library's public API.
// Parse a NaturalCron expression
var compiledExpr = NaturalCronExpr.Parse("daily at 14:30");- Parse Methods
- Properties
- Local Time Occurrence Methods
- UTC Occurrence Methods
- IANA Timezone Occurrence Methods
- Timezone Behavior
- Usage Examples
- Rule Filter Methods
public static NaturalCronExpr Parse(string expression)Parses a NaturalCron expression string into a NaturalCronExpr object.
Parameters:
expression: The NaturalCron expression string to parse
Returns: A NaturalCronExpr object representing the parsed expression
Throws: ArgumentException if the expression is invalid
public static (NaturalCronExpr?, IList<string> errors) TryParse(string expression)Attempts to parse a NaturalCron expression string. Returns the parsed expression and any errors encountered.
Parameters:
expression: The NaturalCron expression string to parse
Returns: A tuple containing:
NaturalCronExpr?: The parsed expression object, or null if parsing failedIList<string> errors: A list of error messages (empty if parsing succeeded)
public string Expression { get; }The original expression string used to create this object.
public IReadOnlyCollection<NaturalCronRule> Rules { get; }The parsed rules that define the recurrence.
These methods work with the current thread's timezone and return results in the local timezone.
public DateTime? TryGetNextOccurrence(DateTime baseTime, DateTime? maxLookahead = null)Tries to get the next occurrence using the current thread's timezone. Returns null if no occurrence is found within the specified lookahead period.
Parameters:
baseTime: The base time in the current thread's timezone from which to find the next occurrencemaxLookahead: Maximum lookahead time in the current thread's timezone. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in the current thread's timezone, or null if no occurrence is found
public DateTime GetNextOccurrence(DateTime baseTime, DateTime? maxLookahead = null)Gets the next occurrence using the current thread's timezone. Throws an exception if no occurrence is found within the specified lookahead period.
Parameters:
baseTime: The base time in the current thread's timezone from which to find the next occurrencemaxLookahead: Maximum lookahead time in the current thread's timezone. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in the current thread's timezone
Throws: InvalidOperationException when no next occurrence is found
public IList<DateTime> TryGetNextOccurrences(DateTime baseTime, int count, DateTime? maxLookahead = null)Tries to get the next occurrences using the current thread's timezone. Returns as many occurrences as found, up to the specified count. Stops when no more occurrences are found or the count is reached.
Parameters:
baseTime: The base time in the current thread's timezone from which to find the next occurrencescount: The number of occurrences to retrievemaxLookahead: Maximum lookahead time in the current thread's timezone. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in the current thread's timezone (may contain fewer than the requested count)
public IList<DateTime> GetNextOccurrences(DateTime baseTime, int count, DateTime? maxLookahead = null)Gets the next occurrences using the current thread's timezone. Throws an exception if fewer occurrences than requested are found. Use TryGetNextOccurrences if you are not sure there are enough occurrences available.
Parameters:
baseTime: The base time in the current thread's timezone from which to find the next occurrencescount: The number of occurrences to retrievemaxLookahead: Maximum lookahead time in the current thread's timezone. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in the current thread's timezone
Throws: InvalidOperationException when fewer than the requested number of occurrences are found
These methods work with UTC times and return UTC results.
public DateTime? TryGetNextOccurrenceInUtc(DateTime utcBaseTime, DateTime? utcMaxLookahead = null)Tries to get the next occurrence in UTC. Returns null if no occurrence is found within the specified lookahead period.
Parameters:
utcBaseTime: The base time in UTC from which to find the next occurrenceutcMaxLookahead: Maximum lookahead time in UTC. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in UTC, or null if no occurrence is found
public DateTime GetNextOccurrenceInUtc(DateTime utcBaseTime, DateTime? utcMaxLookahead = null)Gets the next occurrence in UTC. Throws an exception if no occurrence is found within the specified lookahead period.
Parameters:
utcBaseTime: The base time in UTC from which to find the next occurrenceutcMaxLookahead: Maximum lookahead time in UTC. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in UTC
Throws: InvalidOperationException when no next occurrence is found
public IList<DateTime> TryGetNextOccurrencesInUtc(DateTime utcBaseTime, int count, DateTime? utcMaxLookahead = null)Tries to get the next occurrences in UTC. Returns as many occurrences as found, up to the specified count. Stops when no more occurrences are found or the count is reached.
Parameters:
utcBaseTime: The base time in UTC from which to find the next occurrencescount: The number of occurrences to retrieveutcMaxLookahead: Maximum lookahead time in UTC. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in UTC (may contain fewer than the requested count)
public IList<DateTime> GetNextOccurrencesInUtc(DateTime utcBaseTime, int count, DateTime? utcMaxLookahead = null)Gets the next occurrences in UTC. Throws an exception if fewer occurrences than requested are found. Use TryGetNextOccurrencesInUtc if you are not sure there are enough occurrences available.
Parameters:
utcBaseTime: The base time in UTC from which to find the next occurrencescount: The number of occurrences to retrieveutcMaxLookahead: Maximum lookahead time in UTC. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in UTC
Throws: InvalidOperationException when fewer than the requested number of occurrences are found
These methods work with a specified IANA timezone ID and return results in that timezone. The timezone ID is case-insensitive (e.g., "America/New_York" or "america/new_york").
public DateTime? TryGetNextOccurrenceInTz(DateTime baseTimeInTz, string ianaTzId, DateTime? maxLookaheadInTz = null)Tries to get the next occurrence in a specified IANA timezone. Returns null if no occurrence is found within the specified lookahead period.
Parameters:
baseTimeInTz: The base time interpreted in the specified IANA timezone from which to find the next occurrenceianaTzId: IANA timezone ID (case-insensitive, e.g., "America/New_York" or "europe/lisbon")maxLookaheadInTz: Maximum lookahead time in the specified timezone. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in the specified timezone, or null if no occurrence is found
Throws: ArgumentException when the IANA timezone ID is invalid or not recognized
public DateTime GetNextOccurrenceInTz(DateTime baseTimeInTz, string ianaTzId, DateTime? maxLookaheadInTz = null)Gets the next occurrence in a specified IANA timezone. Throws an exception if no occurrence is found within the specified lookahead period.
Parameters:
baseTimeInTz: The base time interpreted in the specified IANA timezone from which to find the next occurrenceianaTzId: IANA timezone ID (case-insensitive, e.g., "America/New_York" or "europe/lisbon")maxLookaheadInTz: Maximum lookahead time in the specified timezone. If null, defaults to DateTime.MaxValue
Returns: The next occurrence in the specified timezone
Throws:
ArgumentExceptionwhen the IANA timezone ID is invalid or not recognizedInvalidOperationExceptionwhen no next occurrence is found
public IList<DateTime> TryGetNextOccurrencesInTz(DateTime baseTimeInTz, int count, string ianaTzId, DateTime? maxLookaheadInTz = null)Tries to get the next occurrences in a specified IANA timezone. Returns as many occurrences as found, up to the specified count. Stops when no more occurrences are found or the count is reached.
Parameters:
baseTimeInTz: The base time interpreted in the specified IANA timezone from which to find the next occurrencescount: The number of occurrences to retrieveianaTzId: IANA timezone ID (case-insensitive, e.g., "America/New_York" or "europe/lisbon")maxLookaheadInTz: Maximum lookahead time in the specified timezone. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in the specified timezone (may contain fewer than the requested count)
Throws: ArgumentException when the IANA timezone ID is invalid or not recognized
public IList<DateTime> GetNextOccurrencesInTz(DateTime baseTimeInTz, int count, string ianaTzId, DateTime? maxLookaheadInTz = null)Gets the next occurrences in a specified IANA timezone. Throws an exception if fewer occurrences than requested are found. Use TryGetNextOccurrencesInTz if you are not sure there are enough occurrences available.
Parameters:
baseTimeInTz: The base time interpreted in the specified IANA timezone from which to find the next occurrencescount: The number of occurrences to retrieveianaTzId: IANA timezone ID (case-insensitive, e.g., "America/New_York" or "europe/lisbon")maxLookaheadInTz: Maximum lookahead time in the specified timezone. If null, defaults to DateTime.MaxValue
Returns: A list of occurrences in the specified timezone
Throws:
ArgumentExceptionwhen the IANA timezone ID is invalid or not recognizedInvalidOperationExceptionwhen fewer than the requested number of occurrences are found
Understanding how timezones work in NaturalCron is crucial for correct usage:
- Expression timezone (
tz Asia/Tokyo): Specifies the timezone for calculation - when the recurrence rules are evaluated - Return value timezone: Always matches the method used:
- UTC methods (
GetNextOccurrenceInUtc) → return UTC time - Local methods (
GetNextOccurrence) → return system/local time - IANA timezone methods (
GetNextOccurrenceInTz) → return in the specified IANA timezone
- UTC methods (
For expression "daily at 09:00 tz Asia/Tokyo":
- Calculates when 9:00 AM Tokyo time occurs
- If using
GetNextOccurrence(), calculates using Tokyo timezone and returns in the current thread timezone - If using
GetNextOccurrenceInUtc(), calculates using Tokyo timezone and returns in UTC - If using
GetNextOccurrenceInTz(baseTime, "America/New_York"), calculates using Tokyo timezone and returns in New York timezone - If no timezone in the expression, the calculation uses the input time's timezone context
- The input parameter time is interpreted in:
- Current thread timezone for local methods
- UTC for UTC methods
- The specified IANA timezone for IANA timezone methods
The tz keyword affects the calculation timezone, not the return timezone.
// Parse an expression
var expr = NaturalCronExpr.Parse("daily at 14:30");
// Get next occurrence in local time
var next = expr.GetNextOccurrence(DateTime.Now);
// Get next occurrence in UTC
var nextUtc = expr.GetNextOccurrenceInUtc(DateTime.UtcNow);var (expr, errors) = NaturalCronExpr.TryParse("daily at 14:30");
if (expr != null)
{
var next = expr.GetNextOccurrence(DateTime.Now);
}
else
{
Console.WriteLine($"Parse errors: {string.Join(", ", errors)}");
}var expr = NaturalCronExpr.Parse("every monday at 09:00");
// Get next 5 occurrences
var occurrences = expr.GetNextOccurrences(DateTime.Now, 5);
// Try to get next 10 occurrences (may return fewer)
var moreOccurrences = expr.TryGetNextOccurrences(DateTime.Now, 10);var expr = NaturalCronExpr.Parse("daily at 09:00 tz America/New_York");
// This calculates 9 AM New York time and returns it in local system time
var nextLocal = expr.GetNextOccurrence(DateTime.Now);
// This calculates 9 AM New York time and returns it in UTC
var nextUtc = expr.GetNextOccurrenceInUtc(DateTime.UtcNow);var expr = NaturalCronExpr.Parse("daily at 14:30");
// Get next occurrence in Tokyo time (input and output in Tokyo timezone)
var tokyoTime = new DateTime(2024, 1, 1, 10, 0, 0);
var nextInTokyo = expr.GetNextOccurrenceInTz(tokyoTime, "Asia/Tokyo");
// Get next 5 occurrences in London time
var londonTime = new DateTime(2024, 1, 1, 10, 0, 0);
var occurrencesInLondon = expr.GetNextOccurrencesInTz(londonTime, 5, "Europe/London");
// Case-insensitive timezone ID
var nextInNY = expr.GetNextOccurrenceInTz(DateTime.Now, "america/new_york");
// Safe usage with Try methods
var result = expr.TryGetNextOccurrenceInTz(DateTime.Now, "America/Los_Angeles");
if (result.HasValue)
{
Console.WriteLine($"Next occurrence: {result.Value}");
}// Expression calculates in Tokyo time, but returns in New York time
var expr = NaturalCronExpr.Parse("daily at 09:00 tz Asia/Tokyo");
var nyTime = new DateTime(2024, 1, 1, 20, 0, 0); // 8 PM in New York
var nextInNY = expr.GetNextOccurrenceInTz(nyTime, "America/New_York");
// Returns when 9 AM Tokyo occurs, but expressed in New York timezoneThese methods return subsets of rules filtered by time unit:
public IReadOnlyCollection<NaturalCronRule> SecondRules()Returns all rules that operate on seconds.
public IReadOnlyCollection<NaturalCronRule> MinuteRules()Returns all rules that operate on minutes.
public IReadOnlyCollection<NaturalCronRule> HourRules()Returns all rules that operate on hours.
public IReadOnlyCollection<NaturalCronRule> DayRules()Returns all rules that operate on days.
public IReadOnlyCollection<NaturalCronRule> WeekRules()Returns all rules that operate on weeks.
public IReadOnlyCollection<NaturalCronRule> MonthRules()Returns all rules that operate on months.
public IReadOnlyCollection<NaturalCronRule> YearRules()Returns all rules that operate on years.
public NaturalCronIanaTimeZoneRule? TimeZoneRule()Returns the timezone rule if one is defined in the expression, otherwise null.