Skip to content

Commit 33467bb

Browse files
committed
Adjust the Calendar_Gregorian use of dayOfYear pending some more work there
1 parent 4020fe9 commit 33467bb

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ enum ResolvedDateComponents {
110110

111111
return (year, month)
112112
}
113-
113+
114114
init(dateComponents components: DateComponents) {
115115
var (year, month) = Self.yearMonth(forDateComponent: components)
116116
let minWeekdayOrdinal = 1
@@ -2128,7 +2128,7 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
21282128

21292129
// month-based calculations uses .day, .month, and .year, while week-based uses .weekday, .weekOfYear and .yearForWeekOfYear.
21302130
// When performing date adding calculations, we need to be specific whether it's "month based" or "week based". We do not want existing week-related fields in the DateComponents to conflict with the newly set month-related fields when doing month-based calculation, and vice versa. So it's necessary to only include relevant components rather than all components when performing adding calculation.
2131-
let monthBasedComponents : Calendar.ComponentSet = [.era, .year, .month, .day, .dayOfYear, .hour, .minute, .second, .nanosecond]
2131+
let monthBasedComponents : Calendar.ComponentSet = [.era, .year, .month, .day, .hour, .minute, .second, .nanosecond]
21322132
let weekBasedComponents: Calendar.ComponentSet = [.era, .weekday, .weekOfYear, .yearForWeekOfYear, .hour, .minute, .second, .nanosecond ]
21332133

21342134
var result: Date
@@ -2172,7 +2172,9 @@ internal final class _CalendarGregorian: _CalendarProtocol, @unchecked Sendable
21722172

21732173
case .dayOfYear:
21742174
if #available(FoundationPreview 0.4, *) {
2175-
let dc = dateComponents(monthBasedComponents, from: date, in: timeZone)
2175+
var monthIncludingDayOfYear = monthBasedComponents
2176+
monthIncludingDayOfYear.insert(.dayOfYear)
2177+
let dc = dateComponents(monthIncludingDayOfYear, from: date, in: timeZone)
21762178
guard let year = dc.year, let dayOfYear = dc.dayOfYear else {
21772179
preconditionFailure("dateComponents(:from:in:) unexpectedly returns nil for requested component")
21782180
}

Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift

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

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-
}
1803+
let useDayOfMonth = startAtUnit == .day || startAtUnit == .weekday || startAtUnit == .weekdayOrdinal
18081804

18091805
if useDayOfMonth {
18101806
let targetDay = ucal_get(ucalendar, UCAL_DAY_OF_MONTH, &status)

0 commit comments

Comments
 (0)