@@ -21,18 +21,22 @@ struct CalendarCache : Sendable {
21
21
// MARK: - Concrete Classes
22
22
23
23
// _CalendarICU, if present
24
- static var calendarICUClass : _CalendarProtocol . Type = {
24
+ static func calendarICUClass( identifier : Calendar . Identifier ) -> _CalendarProtocol . Type ? {
25
25
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
26
26
_CalendarICU. self
27
27
#else
28
28
if let name = _typeByName ( " FoundationInternationalization._CalendarICU " ) , let t = name as? _CalendarProtocol . Type {
29
29
return t
30
30
} else {
31
- // Use the default gregorian class
32
- return _CalendarGregorian. self
31
+ if identifier == . gregorian {
32
+ // Use the default gregorian class
33
+ return _CalendarGregorian. self
34
+ } else {
35
+ return nil
36
+ }
33
37
}
34
38
#endif
35
- } ( )
39
+ }
36
40
37
41
// MARK: - State
38
42
@@ -71,7 +75,9 @@ struct CalendarCache : Sendable {
71
75
return currentCalendar
72
76
} else {
73
77
let id = Locale . current. _calendarIdentifier
74
- let calendar = CalendarCache . calendarICUClass. init ( identifier: id, timeZone: nil , locale: Locale . current, firstWeekday: nil , minimumDaysInFirstWeek: nil , gregorianStartDate: nil )
78
+ // If we cannot create the right kind of class, we fail immediately here
79
+ let calendarClass = CalendarCache . calendarICUClass ( identifier: id) !
80
+ let calendar = calendarClass. init ( identifier: id, timeZone: nil , locale: Locale . current, firstWeekday: nil , minimumDaysInFirstWeek: nil , gregorianStartDate: nil )
75
81
currentCalendar = calendar
76
82
return calendar
77
83
}
@@ -92,7 +98,9 @@ struct CalendarCache : Sendable {
92
98
if let cached = fixedCalendars [ id] {
93
99
return cached
94
100
} else {
95
- let new = CalendarCache . calendarICUClass. init ( identifier: id, timeZone: nil , locale: nil , firstWeekday: nil , minimumDaysInFirstWeek: nil , gregorianStartDate: nil )
101
+ // If we cannot create the right kind of class, we fail immediately here
102
+ let calendarClass = CalendarCache . calendarICUClass ( identifier: id) !
103
+ let new = calendarClass. init ( identifier: id, timeZone: nil , locale: nil , firstWeekday: nil , minimumDaysInFirstWeek: nil , gregorianStartDate: nil )
96
104
fixedCalendars [ id] = new
97
105
return new
98
106
}
@@ -129,6 +137,8 @@ struct CalendarCache : Sendable {
129
137
130
138
func fixed( identifier: Calendar . Identifier , locale: Locale ? , timeZone: TimeZone ? , firstWeekday: Int ? , minimumDaysInFirstWeek: Int ? , gregorianStartDate: Date ? ) -> any _CalendarProtocol {
131
139
// Note: Only the ObjC NSCalendar initWithCoder supports gregorian start date values. For Swift it is always nil.
132
- return CalendarCache . calendarICUClass. init ( identifier: identifier, timeZone: timeZone, locale: locale, firstWeekday: firstWeekday, minimumDaysInFirstWeek: minimumDaysInFirstWeek, gregorianStartDate: gregorianStartDate)
140
+ // If we cannot create the right kind of class, we fail immediately here
141
+ let calendarClass = CalendarCache . calendarICUClass ( identifier: identifier) !
142
+ return calendarClass. init ( identifier: identifier, timeZone: timeZone, locale: locale, firstWeekday: firstWeekday, minimumDaysInFirstWeek: minimumDaysInFirstWeek, gregorianStartDate: gregorianStartDate)
133
143
}
134
144
}
0 commit comments