Calendar selected in device settings not available in CultureInfo #109924
Description
Description
<style> </style>We are building a MAUI app with .NET 9 for iOS and Android and saw an issue in iOS and some Android device e.g. Samsung Galaxy Note 20 in which if the calendar is changed in the setting the CultureInfo doesn’t have the updated calendar information. Details are below along with a sample application.
- To display a date, a DateTime object is constructed, and DateTime.ToString() invoked to format the date. ToString() expects a CultureInfo object which represents the culture settings on the phone, including the user selected calendar. and format the date using the DateTime.ToString() method which takes as input the CultureInfo.
- Expectation is that the Calendar returned by CultureInfo.DefaultThreadCurrentUICulture represents the actual platform calendar selected by the user (Gregorian, Buddhist, Japanese). This calendar object is presumably used by the ToString() implementation to format a date using the correct calendar. oString() implementation.
- When CultureInfo.DefaultThreadCurrentUICulture properties are inspected, sometimes the associated Calendar object matches the actual calendar selected by the on the phone by the user and sometimes it does not match.
- When the user phone settings are set to Language= Thai, Country=Thailand, Calendar=Buddhist, CultureInfo returns the correct Calendar object representing Buddhist calendar on iOS or Android.
- When passed to the ToString() method to get a ShortDatePattern, the ToString method does not format the date as expected. It only correctly formats it when the app is restarted on iOS. This indicates a potential issue with the ToString() implementation.
- When passed to the ToString() method to get a LongDatePattern using ToString("D", CultureInfo.DefaultThreadCurrentUICulture), the ToString method does return the correct format.
- In all other language, country, calendar combinations tested, CultureInfo does not even return a correct calendar object and the Calendar represent Gregorian irrespective of what is selected by the user. In this case since Calendar object is incorrect, there is no expectation ToString() would work correctly.
Below is the table that shows what date format is shown in the app when we set language, region and calendar (when available) on an iOS and Android devices
Language | Country | Calendar | Expected | Actual | Platform | Remarks |
---|---|---|---|---|---|---|
English | US | Gregorian | Gregorian | Gregorian | iOS | |
English | US | Buddhist | Buddhist | Gregorian | iOS | Even after app restart |
English | US | Japanese | Japanese | Gregorian | iOS | Even after app restart |
Thai | Thailand | Gregorian | Gregorian | Buddhist | iOS | Even after app restart |
Thai | Thailand | Buddhist | Buddhist | Buddhist | iOS | |
Thai | Thailand | Japanese | Japanese | Buddhist | iOS | Even after app restart |
Japanese | Japan | Gregorian | Gregorian | Gregorian | iOS | Even after app restart |
Japanese | Japan | Buddhist | Buddhist | Gregorian | iOS | Even after app restart |
Japanese | Japan | Japanese | Japanese | Gregorian | iOS | Even after app restart |
English | US | Gregorian | Gregorian | Gregorian | Android | |
English | US | Hijri | Hijri | Gregorian | Android | |
English | US | Chinese Lunar | Chinese Lunar | Gregorian | Android | |
Thai | Thailand | Chinese Lunar | Chinese Lunar | Gregorian | Android | |
Thai | Thailand | Gregorian | Gregorian | Gregorian | Android | |
Thai | Thailand | Hijri | Hijri | Gregorian | Android | |
Japanese | Japan | Chinese Lunar | Chinese Lunar | Gregorian | Android | |
Japanese | Japan | Gregorian | Gregorian | Gregorian | Android | |
Japanese | Japan | Hijri | Hijri | Gregorian | Android | |
For iOS we can see option to select Gregorian, Japanese or Buddhist calendar from the device settings.
For Android on Samsung Note 20 we can see calendars like Chinese Lunar, Hijri, Korean Lunar, Shamsi etc. Not all Android devices have the ability to change the calendar.
Steps to Reproduce
Attached is a sample application that will show the dates and locale information.
Steps
- Install the application
- Check the locale, calendar and date information
- Go to device settings -> general -> language and region (On iOS)
- Change the language, region and calendar
- Relaunch the application and check if the language, date and calendar match as per the device setting.
Link to public reproduction project repository
https://github.com/ashrivax23/DateTimeFormatMauiSample
Version with bug
9.0.0-rc.2.24503.2
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
iOS, Android
Affected platform versions
No response
Did you find any workaround?
For iOS I could write a method using NSCalendar to find which calendar is set on the device setting and based on that set the calendar in the app but that has other issues like characters from other language show with the date e.g. Thai characters shown when language is English and calendar is Buddhist.
For Android found no work arounds.