nextAbsoluteOccurrence method

DateTime? nextAbsoluteOccurrence(
  1. DateTime from, {
  2. TimeRange? subrange,
})

Returns the next occurence anchored to from. If the nearest occurence does not fit in range, returns null.

You can provide subrange to narrow down the search to the intersection of the range and the subrange.

This is different from nextOccurrence, as it will return null if the closest occurence does not fit in the range, even if there are possible next closest occurences that fit in the range.

e.g., If you have weekly and monthly recurrence, but only the monthly recurrence's nextOccurrence fits in the range, it will not be returned as weekly's nextOccurrence is nearer.

Implementation

DateTime? nextAbsoluteOccurrence(DateTime from, {TimeRange? subrange}) {
  final TimeRange? effectiveRange =
      subrange == null ? range : (range & subrange);

  if (effectiveRange == null) return null;

  final List<DateTime?> occurences =
      rules.map((rule) => rule.nextOccurrence(from)).toSet().toList();

  if (occurences.isEmpty) {
    return null;
  }

  occurences.sort(_nextComparator);

  final DateTime candidate = occurences.first!;

  if (effectiveRange.contains(candidate)) {
    return candidate;
  }

  return null;
}