Skip to content

RecurrenceRule produces incorrect sequences for start dates with non-zero nanosecond components #1283

Closed
@lukaskollmer

Description

@lukaskollmer

SF-0009 states that the first element in a sequence of Date occurrences computed from a RecurrenceRule is always the start date (see SF-0009#Usage):

A recurrence rule of a given frequency repeats the start date with the interval of that frequency. For example, assume that now it is February 09 2024, 13:43. Creating a daily recurrence would yield a result for each following date at the same time:

var recurrence = Calendar.RecurrenceRule(calendar: .current, frequency: .daily)
for date in recurrence.recurrences(of: .now) {
    // 2024-02-09, 13:43
    // 2024-02-10, 13:43
    // 2024-02-11, 13:43
    // 2024-02-12, 13:43
    // ...
}

However, this currently is only the case for start dates with a 0 nanosecond component. Any other start date (e.g. also Date.now, unless you happen to be creating the date exactly at the start of a second, which admittedly is rather unlikely), will result in recurrences(of:) returning a sequence that will skip the start date.

The reason for this is that Calendar.DatesByRecurring.Iterator internally uses Calendar._dates(startingAfter:), which in turn uses Calendar._unadjustedDates(after:), which only produces Dates with whole-integer seconds.

The tests currently don't catch this, since all RecurrenceRule tests only use dates with whole-integer components as input.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions