@@ -316,34 +316,38 @@ extension Calendar {
316
316
var componentCombinations = Calendar . _DateComponentCombinations ( )
317
317
318
318
if recurrence. frequency == . yearly || recurrence. frequency == . monthly {
319
- if recurrence. daysOfTheYear. isEmpty {
319
+ if dayOfYearAction == . expand {
320
+ componentCombinations. months = nil
321
+ componentCombinations. daysOfMonth = nil
322
+ componentCombinations. daysOfYear = recurrence. daysOfTheYear
323
+ } else {
320
324
componentCombinations. months = if recurrence. months. isEmpty { [ RecurrenceRule . Month ( from: components) !] } else { recurrence. months }
321
325
componentCombinations. daysOfMonth = if recurrence. daysOfTheMonth. isEmpty { [ components. day!] } else { recurrence. daysOfTheMonth}
322
326
componentCombinations. daysOfYear = nil
323
- } else {
324
- componentCombinations. months = nil
325
- componentCombinations. daysOfMonth = nil
326
- componentCombinations. daysOfYear = if recurrence. daysOfTheYear. isEmpty { [ components. day!] } else { recurrence. daysOfTheYear}
327
327
}
328
328
} else {
329
329
componentCombinations. months = nil
330
330
componentCombinations. daysOfMonth = nil
331
331
componentCombinations. daysOfYear = nil
332
332
}
333
- let searchInterval = calendar. dateInterval ( of: recurrence. frequency. component, for: anchor) !
334
- let searchRange = searchInterval. start..< searchInterval. end
335
- let searchStart = searchInterval. start
333
+
336
334
if weekdayAction == . expand {
337
335
componentCombinations. weekdays = recurrence. weekdays
338
336
componentCombinations. daysOfYear = nil
339
337
componentCombinations. daysOfMonth = nil
340
- } else if recurrence. frequency == . weekly || !recurrence . weeks . isEmpty {
338
+ } else if recurrence. frequency == . weekly || weekAction == . expand {
341
339
if let weekdayIdx = components. weekday, let weekday = Locale . Weekday ( weekdayIdx) {
340
+ // In a weekly recurrence (or one that expands weeks of year), we want results to fall on the same weekday as the initial date
342
341
componentCombinations. weekdays = [ . every( weekday) ]
343
342
componentCombinations. daysOfYear = nil
344
343
componentCombinations. daysOfMonth = nil
345
344
}
346
345
}
346
+ if weekAction == . expand {
347
+ // In a yearly recurrence with weeks specified, results do not land on any specific month
348
+ componentCombinations. weeksOfYear = recurrence. weeks
349
+ componentCombinations. months = nil
350
+ }
347
351
if recurrence. frequency != . hourly, recurrence. frequency != . minutely {
348
352
componentCombinations. hours = if hourAction == . expand { recurrence. hours } else { components. hour. map { [ $0] } }
349
353
}
@@ -352,12 +356,10 @@ extension Calendar {
352
356
}
353
357
componentCombinations. seconds = if secondAction == . expand { recurrence. seconds } else { components. second. map { [ $0] } }
354
358
355
- if !recurrence. weeks. isEmpty {
356
- if recurrence. frequency == . yearly {
357
- componentCombinations. weeksOfYear = recurrence. weeks
358
- componentCombinations. months = nil
359
- }
360
- }
359
+
360
+ let searchInterval = calendar. dateInterval ( of: recurrence. frequency. component, for: anchor) !
361
+ let searchRange = searchInterval. start..< searchInterval. end
362
+ let searchStart = searchInterval. start
361
363
362
364
// First expand the set of dates, and then filter it. The order
363
365
// of expansions is fixed, and must stay exactly as it is so we
0 commit comments