Skip to content

Commit afc2c56

Browse files
committed
Simplify date component construction
1 parent a516f2d commit afc2c56

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,34 +316,38 @@ extension Calendar {
316316
var componentCombinations = Calendar._DateComponentCombinations()
317317

318318
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 {
320324
componentCombinations.months = if recurrence.months.isEmpty { [RecurrenceRule.Month(from: components)!] } else { recurrence.months }
321325
componentCombinations.daysOfMonth = if recurrence.daysOfTheMonth.isEmpty { [components.day!] } else { recurrence.daysOfTheMonth}
322326
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}
327327
}
328328
} else {
329329
componentCombinations.months = nil
330330
componentCombinations.daysOfMonth = nil
331331
componentCombinations.daysOfYear = nil
332332
}
333-
let searchInterval = calendar.dateInterval(of: recurrence.frequency.component, for: anchor)!
334-
let searchRange = searchInterval.start..<searchInterval.end
335-
let searchStart = searchInterval.start
333+
336334
if weekdayAction == .expand {
337335
componentCombinations.weekdays = recurrence.weekdays
338336
componentCombinations.daysOfYear = nil
339337
componentCombinations.daysOfMonth = nil
340-
} else if recurrence.frequency == .weekly || !recurrence.weeks.isEmpty {
338+
} else if recurrence.frequency == .weekly || weekAction == .expand {
341339
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
342341
componentCombinations.weekdays = [.every(weekday)]
343342
componentCombinations.daysOfYear = nil
344343
componentCombinations.daysOfMonth = nil
345344
}
346345
}
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+
}
347351
if recurrence.frequency != .hourly, recurrence.frequency != .minutely {
348352
componentCombinations.hours = if hourAction == .expand { recurrence.hours } else { components.hour.map { [$0] } }
349353
}
@@ -352,12 +356,10 @@ extension Calendar {
352356
}
353357
componentCombinations.seconds = if secondAction == .expand { recurrence.seconds } else { components.second.map { [$0] } }
354358

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
361363

362364
// First expand the set of dates, and then filter it. The order
363365
// of expansions is fixed, and must stay exactly as it is so we

0 commit comments

Comments
 (0)