Skip to content

Commit 4020fe9

Browse files
committed
Disable day of year for in-progress Gregorian calendar implementation
1 parent e07c32e commit 4020fe9

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Cache.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ struct CalendarCache : Sendable {
2121
// MARK: - Concrete Classes
2222

2323
// _CalendarICU, if present
24-
static var calendarICUClass: _CalendarProtocol.Type = {
24+
static func calendarICUClass(identifier: Calendar.Identifier) -> _CalendarProtocol.Type? {
2525
#if FOUNDATION_FRAMEWORK && canImport(FoundationICU)
2626
_CalendarICU.self
2727
#else
2828
if let name = _typeByName("FoundationInternationalization._CalendarICU"), let t = name as? _CalendarProtocol.Type {
2929
return t
3030
} 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+
}
3337
}
3438
#endif
35-
}()
39+
}
3640

3741
// MARK: - State
3842

@@ -71,7 +75,9 @@ struct CalendarCache : Sendable {
7175
return currentCalendar
7276
} else {
7377
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)
7581
currentCalendar = calendar
7682
return calendar
7783
}
@@ -92,7 +98,9 @@ struct CalendarCache : Sendable {
9298
if let cached = fixedCalendars[id] {
9399
return cached
94100
} 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)
96104
fixedCalendars[id] = new
97105
return new
98106
}
@@ -129,6 +137,8 @@ struct CalendarCache : Sendable {
129137

130138
func fixed(identifier: Calendar.Identifier, locale: Locale?, timeZone: TimeZone?, firstWeekday: Int?, minimumDaysInFirstWeek: Int?, gregorianStartDate: Date?) -> any _CalendarProtocol {
131139
// 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)
133143
}
134144
}

Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ extension Date {
5656
/// This helper records which components should take precedence.
5757
enum ResolvedDateComponents {
5858

59-
case dayOfYear(year: Int, dayOfYear: Int)
59+
// TODO: Day of Year
60+
// case dayOfYear(year: Int, dayOfYear: Int)
6061
case day(year: Int, month: Int, day: Int?, weekOfYear: Int?)
6162
case weekdayOrdinal(year: Int, month: Int, weekdayOrdinal: Int, weekday: Int?)
6263
case weekOfYear(year: Int, weekOfYear: Int?, weekday: Int?)
@@ -114,7 +115,7 @@ enum ResolvedDateComponents {
114115
var (year, month) = Self.yearMonth(forDateComponent: components)
115116
let minWeekdayOrdinal = 1
116117

117-
// TODO: Handle day of year
118+
// TODO: Check day of year value here
118119
if let d = components.day {
119120
if components.yearForWeekOfYear != nil, let weekOfYear = components.weekOfYear {
120121
if components.month == nil && weekOfYear >= 52 {
@@ -1509,8 +1510,6 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
15091510

15101511
var rawYear: Int
15111512
switch resolvedComponents {
1512-
case .dayOfYear(_, _):
1513-
fatalError("TODO - day of year calculation")
15141513
case .day(let year, let month, _, _):
15151514
rawMonth = month
15161515
rawYear = year
@@ -1534,8 +1533,6 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
15341533

15351534
let julianDay: Int
15361535
switch resolvedComponents {
1537-
case .dayOfYear(_, _):
1538-
fatalError("TODO - day of year calculation")
15391536
case .day(_, _, let day, _):
15401537
julianDay = julianDayAtBeginningOfYear + (day ?? 1)
15411538
case .weekdayOrdinal(_, _, let weekdayOrdinal, let weekday):
@@ -1603,7 +1600,6 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
16031600
useJulianReference = year == gregorianStartYear
16041601
case .weekOfMonth(_, _, _, _): break
16051602
case .day(_, _, _, _): break
1606-
case .dayOfYear(_, _): break
16071603
case .weekdayOrdinal(_, _, _, _): break
16081604
}
16091605

Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,11 @@ internal final class _CalendarICU: _CalendarProtocol, @unchecked Sendable {
18001800
} while ucal_get(ucalendar, UCAL_ERA, &status) < targetEra
18011801
}
18021802

1803-
let useDayOfMonth = startAtUnit == .day || startAtUnit == .weekday || startAtUnit == .weekdayOrdinal
1803+
let useDayOfMonth = if #available(FoundationPreview 0.4, *) {
1804+
startAtUnit == .day || startAtUnit == .weekday || startAtUnit == .weekdayOrdinal || startAtUnit == .dayOfYear
1805+
} else {
1806+
startAtUnit == .day || startAtUnit == .weekday || startAtUnit == .weekdayOrdinal
1807+
}
18041808

18051809
if useDayOfMonth {
18061810
let targetDay = ucal_get(ucalendar, UCAL_DAY_OF_MONTH, &status)

0 commit comments

Comments
 (0)