Our app works quite a bit with dates, but at this time we only support the Gregorian calendar and an app wide NSCalendar instance is initialized as follows:
NSCalendar *appCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
The docs of the above method states that "The returned calendar defaults to the current locale and default time zone." However, when running the app on a device with the region set to "United Kingdom", calling [appCalendar firstWeekday] returned a value of 1 (Sunday) rather than the expected 2 (Monday). If I run [[NSCalendar currentCalendar] firstWeekday], the correct value of 2 is returned. At first I thought that a locale may not be set on "appCalendar", but logging revealed it had one, though it lacked a countrycode etc., which the "currentCalendar" instance does have and which allows it to return the correct firstWeekDay.
Should a locale explicitly be set on the object returned from calendarWithIdentifier and if so, are there any considerations in doing so?
Update
Based on zrzka's answer below, I recommend that a locale is explicitly set when initializing a calendar with an identifier e.g.
NSCalendar *appCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
appCalendar.locale = [NSLocale currentLocale];
The documentation is wrong:
It should be:
CFCalendar.c:_localeis initialized withNULLand_localeIDis initialized with locale identifier of the system locale (which is an empty string on iPhone & simulator)._calis set toNULL.So, because
_calisNULL,__CFCalendarSetupCalis called.Which calls
__CFCalendarCreateUCalendarwith_localeIDwhich is an empty string.I can confirm this behavior on iOS 11, 12 & 13. The source code is for something called CF-Lite, but I went further and disassembled actual CoreFoundation framework and it does the same thing ...
... using an empty identifier from
CFLocaleGetIdentifierfromCFLocaleGetSystem.When you check the
CFCalendarCreateWithIdentifierdocumentation, there's not a word about current locale, time zone, ...What's even more interesting is the difference (section Discussion) for these two methods:
+calendarWithIdentifier:-initWithCalendarIdentifier:But there's no difference,
calendarWithIdentifier:just calls thealloc&initWithCalendarIdentifier:.I believe it's a documentation problem which should be reported to Apple (did it, FB7740798).