Skip to content

Commit 7c15437

Browse files
committed
Remove if available FoundationPreview 0.4 checks (see #384)
1 parent e09fd5f commit 7c15437

File tree

5 files changed

+54
-116
lines changed

5 files changed

+54
-116
lines changed

Sources/FoundationEssentials/Calendar/Calendar.swift

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,15 @@ public struct Calendar : Hashable, Equatable, Sendable {
194194
if contains(.nanosecond) { result.insert(.nanosecond) }
195195
if contains(.calendar) { result.insert(.calendar) }
196196
if contains(.timeZone) { result.insert(.timeZone) }
197-
if contains(.isLeapMonth) {
198-
if #available(FoundationPreview 0.1, *) {
199-
result.insert(.isLeapMonth)
200-
}
201-
}
202-
if contains(.dayOfYear) {
203-
if #available(FoundationPreview 0.4, *) {
204-
result.insert(.dayOfYear)
205-
}
206-
}
197+
if contains(.isLeapMonth) { result.insert(.isLeapMonth) }
198+
if contains(.dayOfYear) { result.insert(.dayOfYear) }
207199
return result
208200
}
209201

210202
package var highestSetUnit: Calendar.Component? {
211203
if self.contains(.era) { return .era }
212204
if self.contains(.year) { return .year }
213-
if #available(FoundationPreview 0.4, *) {
214-
if self.contains(.dayOfYear) { return .dayOfYear }
215-
}
205+
if self.contains(.dayOfYear) { return .dayOfYear }
216206
if self.contains(.quarter) { return .quarter }
217207
if self.contains(.month) { return .month }
218208
if self.contains(.day) { return .day }
@@ -227,9 +217,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
227217
if self.contains(.nanosecond) { return .nanosecond }
228218

229219
// The algorithms that call this function assume that isLeapMonth counts as a 'highest unit set', but the order is after nanosecond.
230-
if #available(FoundationPreview 0.1, *) {
231-
if self.contains(.isLeapMonth) { return .isLeapMonth }
232-
}
220+
if self.contains(.isLeapMonth) { return .isLeapMonth }
233221

234222
// The calendar and timeZone properties do not count as a 'highest unit set', since they are not ordered in time like the others are.
235223
return nil
@@ -1247,13 +1235,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
12471235
/// - returns: `true` if the date matches all of the components, otherwise `false`.
12481236
@available(iOS 8.0, *)
12491237
public func date(_ date: Date, matchesComponents components: DateComponents) -> Bool {
1250-
let comparedUnits: Set<Calendar.Component>
1251-
1252-
if #available(FoundationPreview 0.4, *) {
1253-
comparedUnits = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear, .dayOfYear, .nanosecond]
1254-
} else {
1255-
comparedUnits = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear, .nanosecond]
1256-
}
1238+
let comparedUnits: Set<Calendar.Component> = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear, .dayOfYear, .nanosecond]
12571239

12581240

12591241
let actualUnits = comparedUnits.filter { u in

Sources/FoundationEssentials/Calendar/Calendar_Enumerate.swift

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ extension DateComponents {
8484
guard calendar.value(v, isValidFor: .year) else { return false }
8585
}
8686

87-
if #available(FoundationPreview 0.4, *) {
88-
if let v = self.dayOfYear {
89-
hasAtLeastOneFieldSet = true
90-
guard calendar.value(v, isValidFor: .dayOfYear) else { return false }
91-
}
87+
if let v = self.dayOfYear {
88+
hasAtLeastOneFieldSet = true
89+
guard calendar.value(v, isValidFor: .dayOfYear) else { return false }
9290
}
9391

9492
if let v = self.quarter {
@@ -162,9 +160,7 @@ extension DateComponents {
162160
// A note on performance: this approach is much faster than using key paths, which require a lot more allocations.
163161
if self.era != nil { return .era }
164162
if self.year != nil { return .year }
165-
if #available(FoundationPreview 0.4, *) {
166-
if self.dayOfYear != nil { return .dayOfYear }
167-
}
163+
if self.dayOfYear != nil { return .dayOfYear }
168164
if self.quarter != nil { return .quarter }
169165
if self.month != nil { return .month }
170166
if self.day != nil { return .day }
@@ -198,9 +194,7 @@ extension DateComponents {
198194
if self.day != nil { return .day }
199195
if self.month != nil { return .month }
200196
if self.quarter != nil { return .quarter }
201-
if #available(FoundationPreview 0.4, *) {
202-
if self.dayOfYear != nil { return .dayOfYear }
203-
}
197+
if self.dayOfYear != nil { return .dayOfYear }
204198
if self.year != nil { return .year }
205199
if self.era != nil { return .era }
206200
return nil
@@ -221,9 +215,7 @@ extension DateComponents {
221215
if self.weekOfMonth != nil { units.insert(.weekOfMonth) }
222216
if self.weekOfYear != nil { units.insert(.weekOfYear) }
223217
if self.yearForWeekOfYear != nil { units.insert(.yearForWeekOfYear) }
224-
if #available(FoundationPreview 0.4, *) {
225-
if self.dayOfYear != nil { units.insert(.dayOfYear) }
226-
}
218+
if self.dayOfYear != nil { units.insert(.dayOfYear) }
227219
if self.nanosecond != nil { units.insert(.nanosecond) }
228220
return units
229221
}
@@ -251,9 +243,7 @@ extension DateComponents {
251243
if self.yearForWeekOfYear != other.yearForWeekOfYear { mismatched.insert(.yearForWeekOfYear) }
252244
if self.nanosecond != other.nanosecond { mismatched.insert(.nanosecond) }
253245
if self.isLeapMonth != other.isLeapMonth { mismatched.insert(.isLeapMonth) }
254-
if #available(FoundationPreview 0.4, *) {
255-
if self.dayOfYear != other.dayOfYear { mismatched.insert(.dayOfYear) }
256-
}
246+
if self.dayOfYear != other.dayOfYear { mismatched.insert(.dayOfYear) }
257247

258248
return mismatched
259249
}
@@ -1344,10 +1334,8 @@ extension Calendar {
13441334
searchStartDate = result
13451335
}
13461336

1347-
if #available(FoundationPreview 0.4, *) {
1348-
if let result = try dateAfterMatchingDayOfYear(startingAt: searchStartDate, components: comps, direction: direction) {
1349-
searchStartDate = result
1350-
}
1337+
if let result = try dateAfterMatchingDayOfYear(startingAt: searchStartDate, components: comps, direction: direction) {
1338+
searchStartDate = result
13511339
}
13521340

13531341
if let result = try dateAfterMatchingMonth(startingAt: searchStartDate, components: comps, direction: direction, strictMatching: isStrictMatching) {

Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,9 +1852,7 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
18521852
}
18531853
if components.contains(.month) { dc.month = month }
18541854
if components.contains(.day) { dc.day = day }
1855-
if #available(FoundationPreview 0.4, *) {
1856-
if components.contains(.dayOfYear) { dc.dayOfYear = dayOfYear }
1857-
}
1855+
if components.contains(.dayOfYear) { dc.dayOfYear = dayOfYear }
18581856
if components.contains(.hour) { dc.hour = hour }
18591857
if components.contains(.minute) { dc.minute = minute }
18601858
if components.contains(.second) { dc.second = second }
@@ -2171,34 +2169,30 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
21712169
result = self.date(from: dc, inTimeZone: timeZone)!
21722170

21732171
case .dayOfYear:
2174-
if #available(FoundationPreview 0.4, *) {
2175-
var monthIncludingDayOfYear = monthBasedComponents
2176-
monthIncludingDayOfYear.insert(.dayOfYear)
2177-
let dc = dateComponents(monthIncludingDayOfYear, from: date, in: timeZone)
2178-
guard let year = dc.year, let dayOfYear = dc.dayOfYear else {
2179-
preconditionFailure("dateComponents(:from:in:) unexpectedly returns nil for requested component")
2180-
}
2181-
2182-
let range: Range<Int>
2183-
if gregorianYearIsLeap(year) {
2184-
// max is 366
2185-
range = 1..<367
2186-
} else {
2187-
// max is 365
2188-
range = 1..<366
2189-
}
2190-
2191-
let newDayOfYear = add(amount: amount, to: dayOfYear, wrappingTo: range)
2192-
// Clear the month and day from the date components. Keep the era, year, and time values (hour, min, etc.)
2193-
var adjustedDateComponents = dc
2194-
adjustedDateComponents.month = nil
2195-
adjustedDateComponents.day = nil
2196-
adjustedDateComponents.dayOfYear = newDayOfYear
2197-
result = self.date(from: adjustedDateComponents, inTimeZone: timeZone)!
2198-
} else {
2199-
result = date
2172+
var monthIncludingDayOfYear = monthBasedComponents
2173+
monthIncludingDayOfYear.insert(.dayOfYear)
2174+
let dc = dateComponents(monthIncludingDayOfYear, from: date, in: timeZone)
2175+
guard let year = dc.year, let dayOfYear = dc.dayOfYear else {
2176+
preconditionFailure("dateComponents(:from:in:) unexpectedly returns nil for requested component")
22002177
}
2201-
2178+
2179+
let range: Range<Int>
2180+
if gregorianYearIsLeap(year) {
2181+
// max is 366
2182+
range = 1..<367
2183+
} else {
2184+
// max is 365
2185+
range = 1..<366
2186+
}
2187+
2188+
let newDayOfYear = add(amount: amount, to: dayOfYear, wrappingTo: range)
2189+
// Clear the month and day from the date components. Keep the era, year, and time values (hour, min, etc.)
2190+
var adjustedDateComponents = dc
2191+
adjustedDateComponents.month = nil
2192+
adjustedDateComponents.day = nil
2193+
adjustedDateComponents.dayOfYear = newDayOfYear
2194+
result = self.date(from: adjustedDateComponents, inTimeZone: timeZone)!
2195+
22022196
case .day:
22032197
let (_, monthStart, daysInMonth, inGregorianCutoverMonth) = dayOfMonthConsideringGregorianCutover(date, inTimeZone: timeZone)
22042198

Sources/FoundationEssentials/Calendar/DateComponents.swift

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public struct DateComponents : Hashable, Equatable, Sendable {
7373
self.weekOfMonth = weekOfMonth
7474
self.weekOfYear = weekOfYear
7575
self.yearForWeekOfYear = yearForWeekOfYear
76-
77-
if #available(FoundationPreview 0.4, *) {
78-
self.dayOfYear = nil
79-
}
76+
self.dayOfYear = nil
8077
}
8178

8279
package init?(component: Calendar.Component, value: Int) {
@@ -286,8 +283,7 @@ public struct DateComponents : Hashable, Equatable, Sendable {
286283
case .weekOfYear: self.weekOfYear = value
287284
case .yearForWeekOfYear: self.yearForWeekOfYear = value
288285
case .nanosecond: self.nanosecond = value
289-
case .dayOfYear:
290-
if #available(FoundationPreview 0.4, *) { self.dayOfYear = value }
286+
case .dayOfYear: self.dayOfYear = value
291287
case .calendar, .timeZone, .isLeapMonth:
292288
// Do nothing
293289
break
@@ -314,7 +310,7 @@ public struct DateComponents : Hashable, Equatable, Sendable {
314310
case .weekOfYear: return self.weekOfYear
315311
case .yearForWeekOfYear: return self.yearForWeekOfYear
316312
case .nanosecond: return self.nanosecond
317-
case .dayOfYear: if #available(FoundationPreview 0.4, *) { return self.dayOfYear } else { return nil }
313+
case .dayOfYear: return self.dayOfYear
318314
case .calendar, .timeZone, .isLeapMonth:
319315
return nil
320316
}
@@ -366,12 +362,7 @@ public struct DateComponents : Hashable, Equatable, Sendable {
366362
}
367363

368364
// This is similar to the list of units and keys\. in Calendar_Enumerate.swift, but this one does not include nanosecond or leap month
369-
let units : [Calendar.Component]
370-
if #available(FoundationPreview 0.4, *) {
371-
units = [.era, .year, .quarter, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .weekOfMonth, .weekOfYear, .yearForWeekOfYear, .dayOfYear]
372-
} else {
373-
units = [.era, .year, .quarter, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .weekOfMonth, .weekOfYear, .yearForWeekOfYear]
374-
}
365+
let units : [Calendar.Component] = [.era, .year, .quarter, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .weekOfMonth, .weekOfYear, .yearForWeekOfYear, .dayOfYear]
375366

376367
let newComponents = calendar.dateComponents(Set(units), from: date)
377368

@@ -388,9 +379,8 @@ public struct DateComponents : Hashable, Equatable, Sendable {
388379
if let weekOfMonth = _weekOfMonth, weekOfMonth != newComponents.weekOfMonth { return false }
389380
if let weekOfYear = _weekOfYear, weekOfYear != newComponents.weekOfYear { return false }
390381
if let yearForWeekOfYear = _yearForWeekOfYear, yearForWeekOfYear != newComponents.yearForWeekOfYear { return false }
391-
if #available(FoundationPreview 0.4, *) {
392-
if let dayOfYear = _dayOfYear, dayOfYear != newComponents.dayOfYear { return false }
393-
}
382+
if let dayOfYear = _dayOfYear, dayOfYear != newComponents.dayOfYear { return false }
383+
394384
return true
395385
}
396386

@@ -458,10 +448,8 @@ public struct DateComponents : Hashable, Equatable, Sendable {
458448
return false
459449
}
460450

461-
if #available(FoundationPreview 0.4, *) {
462-
if lhs.dayOfYear != rhs.dayOfYear {
463-
return false
464-
}
451+
if lhs.dayOfYear != rhs.dayOfYear {
452+
return false
465453
}
466454

467455
if !((lhs.isLeapMonth == false && rhs.isLeapMonth == nil) ||
@@ -507,9 +495,7 @@ extension DateComponents : CustomStringConvertible, CustomDebugStringConvertible
507495
if let r = quarter { c.append((label: "quarter", value: r)) }
508496
if let r = weekOfMonth { c.append((label: "weekOfMonth", value: r)) }
509497
if let r = weekOfYear { c.append((label: "weekOfYear", value: r)) }
510-
if #available(FoundationPreview 0.4, *) {
511-
if let r = dayOfYear { c.append((label: "dayOfYear", value: r)) }
512-
}
498+
if let r = dayOfYear { c.append((label: "dayOfYear", value: r)) }
513499
if let r = yearForWeekOfYear { c.append((label: "yearForWeekOfYear", value: r)) }
514500
if let r = isLeapMonth { c.append((label: "isLeapMonth", value: r)) }
515501
return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct)
@@ -584,10 +570,8 @@ extension DateComponents : Codable {
584570
self.isLeapMonth = isLeapMonth
585571
}
586572

587-
if #available(FoundationPreview 0.4, *) {
588-
if let dayOfYear {
589-
self.dayOfYear = dayOfYear
590-
}
573+
if let dayOfYear {
574+
self.dayOfYear = dayOfYear
591575
}
592576
}
593577

@@ -610,9 +594,7 @@ extension DateComponents : Codable {
610594
try container.encodeIfPresent(self.weekOfYear, forKey: .weekOfYear)
611595
try container.encodeIfPresent(self.yearForWeekOfYear, forKey: .yearForWeekOfYear)
612596
try container.encodeIfPresent(self.isLeapMonth, forKey: .isLeapMonth)
613-
if #available(FoundationPreview 0.4, *) {
614-
try container.encodeIfPresent(self.dayOfYear, forKey: .dayOfYear)
615-
}
597+
try container.encodeIfPresent(self.dayOfYear, forKey: .dayOfYear)
616598
}
617599
}
618600

Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,7 @@ internal final class _CalendarICU: _CalendarProtocol, @unchecked Sendable {
11941194
if let value = components.second { ucal_set(ucalendar, UCAL_SECOND, Int32(truncatingIfNeeded: value)) }
11951195
if let value = components.nanosecond { nanosecond = Double(value) }
11961196
if let isLeap = components.isLeapMonth, isLeap { ucal_set(ucalendar, UCAL_IS_LEAP_MONTH, 1) }
1197-
if #available(FoundationPreview 0.4, *) {
1198-
if let value = components.dayOfYear { ucal_set(ucalendar, UCAL_DAY_OF_YEAR, Int32(truncatingIfNeeded: value)) }
1199-
}
1197+
if let value = components.dayOfYear { ucal_set(ucalendar, UCAL_DAY_OF_YEAR, Int32(truncatingIfNeeded: value)) }
12001198

12011199
var status = U_ZERO_ERROR
12021200
let udate = ucal_getMillis(ucalendar, &status)
@@ -1239,9 +1237,7 @@ internal final class _CalendarICU: _CalendarProtocol, @unchecked Sendable {
12391237
// ICU's Month is -1 from DateComponents
12401238
if components.contains(.month) { dc.month = Int(ucal_get(ucalendar, UCAL_MONTH, &status)) + 1 }
12411239
if components.contains(.day) { dc.day = Int(ucal_get(ucalendar, UCAL_DAY_OF_MONTH, &status)) }
1242-
if #available(FoundationPreview 0.4, *) {
1243-
if components.contains(.dayOfYear) { dc.dayOfYear = Int(ucal_get(ucalendar, UCAL_DAY_OF_YEAR, &status)) }
1244-
}
1240+
if components.contains(.dayOfYear) { dc.dayOfYear = Int(ucal_get(ucalendar, UCAL_DAY_OF_YEAR, &status)) }
12451241
if components.contains(.weekOfYear) { dc.weekOfYear = Int(ucal_get(ucalendar, UCAL_WEEK_OF_YEAR, &status)) }
12461242
if components.contains(.weekOfMonth) { dc.weekOfMonth = Int(ucal_get(ucalendar, UCAL_WEEK_OF_MONTH, &status)) }
12471243
if components.contains(.yearForWeekOfYear) { dc.yearForWeekOfYear = Int(ucal_get(ucalendar, UCAL_YEAR_WOY, &status)) }
@@ -1294,9 +1290,7 @@ internal final class _CalendarICU: _CalendarProtocol, @unchecked Sendable {
12941290
// if let _ = components.quarter { }
12951291
if let amount = components.month { _ = _locked_add(UCAL_MONTH, amount: amount, wrap: wrappingComponents, status: &status) }
12961292
if let amount = components.day { _ = _locked_add(UCAL_DAY_OF_MONTH, amount: amount, wrap: wrappingComponents, status: &status) }
1297-
if #available(FoundationPreview 0.4, *) {
1298-
if let amount = components.dayOfYear { _ = _locked_add(UCAL_DAY_OF_YEAR, amount: amount, wrap: wrappingComponents, status: &status) }
1299-
}
1293+
if let amount = components.dayOfYear { _ = _locked_add(UCAL_DAY_OF_YEAR, amount: amount, wrap: wrappingComponents, status: &status) }
13001294
if let amount = components.weekOfYear { _ = _locked_add(UCAL_WEEK_OF_YEAR, amount: amount, wrap: wrappingComponents, status: &status) }
13011295
// `week` is for backward compatibility only, and is only used if weekOfYear is missing
13021296
if let amount = components.week, components.weekOfYear == nil { _ = _locked_add(UCAL_WEEK_OF_YEAR, amount: amount, wrap: wrappingComponents, status: &status) }
@@ -1357,9 +1351,7 @@ internal final class _CalendarICU: _CalendarProtocol, @unchecked Sendable {
13571351
if components.contains(.weekOfYear) { dc.weekOfYear = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_WEEK_OF_YEAR, &status)) }
13581352
if components.contains(.weekOfMonth) { dc.weekOfMonth = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_WEEK_OF_MONTH, &status)) }
13591353
if components.contains(.day) { dc.day = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_DAY_OF_MONTH, &status)) }
1360-
if #available(FoundationPreview 0.4, *) {
1361-
if components.contains(.dayOfYear) { dc.dayOfYear = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_DAY_OF_YEAR, &status)) }
1362-
}
1354+
if components.contains(.dayOfYear) { dc.dayOfYear = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_DAY_OF_YEAR, &status)) }
13631355
if components.contains(.weekday) { dc.weekday = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_DAY_OF_WEEK, &status)) }
13641356
if components.contains(.weekdayOrdinal) { dc.weekdayOrdinal = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)) }
13651357
if components.contains(.hour) { dc.hour = Int(ucal_getFieldDifference(ucalendar, goal, UCAL_HOUR_OF_DAY, &status)) }

0 commit comments

Comments
 (0)