A puzzling phenomenon occurs in obtaining the week number.
Please see the attached source.
In Step 0, the correct week number can be obtained according to ISO8601.
In Step 1, by changing minimumDaysInFirstWeek, a new (correct) week number can be obtained.
In Step 2, if I re-set the firstWeekday, for some reason the week number reverts back to the original, even though the value has not been changed.
Shouldn't Step2 return 1 for both weekOfMonth and weekOfYear?
I have verified this with XCode 13.4 playground.
Thanks in advance.
import Foundation
var calendar = Calendar(identifier: .iso8601)
let date = calendar.date(from: DateComponents(year: 2022, month: 1, day: 1))
print("*** Step 0 ***")
print("firstWeekday : \(calendar.firstWeekday)")
print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)")
print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))")
print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))")
print("\n*** Step 1 ***")
calendar.minimumDaysInFirstWeek = 1
print("firstWeekday : \(calendar.firstWeekday)")
print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)")
print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))")
print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))")
print("\n*** Step 2 ***")
calendar.firstWeekday = 2
print("firstWeekday : \(calendar.firstWeekday)")
print("minimumDaysInFirstWeek : \(calendar.minimumDaysInFirstWeek)")
print("weekOfMonth : \(calendar.component(.weekOfMonth, from: date!))")
print("weekOfYear : \(calendar.component(.weekOfYear, from: date!))")
Result
*** Step 0 ***
firstWeekday : 2
minimumDaysInFirstWeek : 4
weekOfMonth : 0
weekOfYear : 52
*** Step 1 ***
firstWeekday : 2
minimumDaysInFirstWeek : 1
weekOfMonth : 1
weekOfYear : 1
*** Step 2 ***
firstWeekday : 2
minimumDaysInFirstWeek : 1
weekOfMonth : 0
weekOfYear : 52