Bug 59077 - Thai crash Not a valid calendar for the given culture.
Summary: Thai crash Not a valid calendar for the given culture.
Alias: None
Product: Android
Classification: Xamarin
Component: Linker ()
Version: 7.4 (15.3)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Radek Doulik
Depends on:
Reported: 2017-08-28 08:25 UTC by gcnew
Modified: 2018-04-20 00:42 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

repro sample (8.32 KB, application/x-zip-compressed)
2017-08-28 08:25 UTC, gcnew

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 59077 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description gcnew 2017-08-28 08:25:19 UTC
Created attachment 24441 [details]
repro sample

Previously I reported bug with Thai culture (https://bugzilla.xamarin.com/show_bug.cgi?id=31228).
It was indeed fixed and now works fine, but now I experience another bug which can be reproduced using the attached project. All encodings are checked and included in the project.
In order to reproduce it set android phone language (I tested it on android 4.4) to be Thai language.
Put the following line in the OnCreate:
var date = DateTime.Parse("2000-31-07");
var date = DateTime.ParseExact("2000-31-07","yyyy-dd-MM",null);
And it will crash at runtime with the following exception:
System.ArgumentOutOfRangeException: Not a valid calendar for the given culture.
Parameter name: value
at System.Globalization.DateTimeFormatInfo.set_Calendar (System.Globalization.Calendar value) [0x00154] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at System.Globalization.DateTimeFormatInfo..ctor (System.Globalization.CultureData cultureData, System.Globalization.Calendar cal) [0x00022] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at System.Globalization.CultureInfo.get_DateTimeFormat () [0x00033] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at System.Globalization.CultureInfo.GetFormat (System.Type formatType) [0x0002f] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at System.Globalization.DateTimeFormatInfo.get_CurrentInfo () [0x00021] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at System.DateTime.Parse (System.String s) [0x00000] in <9e5b8ac258034478aed80c25b6e73e52>:0 
at ThaiBug2.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00009] in C:\Users\username\Documents\Visual Studio 2017\Projects\ThaiBug2\ThaiBug2\MainActivity.cs:14 
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <947c88b5da6c4d94ab126d7acb391dc6>:0 
at (wrapper dynamic-method) System.Object:bc4a6bc1-1943-4d65-9d85-57b6d5875e98 (intptr,intptr,intptr)
Comment 1 gcnew 2017-08-28 08:54:54 UTC
Seems like the problem is different. When I use no linking it does work, I mean second command (var date = DateTime.ParseExact("2000-31-07","yyyy-dd-MM",null);) I use Link option SDK Libraries, I don't want to ignore encoding libs. Because it's dumb to include them in order to ignore them by linker. I don't think I need to do the job twice in order to make this work. How do I skip them, I tried to skip the following, but it didn't affect crash:
Comment 2 gcnew 2017-08-28 10:32:32 UTC
mscorlib link skip doesn't work either.
Comment 3 gcnew 2017-08-28 10:47:02 UTC
Put the following somewhere in your code:
private void Init()
  var t = typeof(System.Globalization.ThaiBuddhistCalendar);
Since I don't link my libs I don't even need to call it.
But the bug still remains. It shouldn't work this way.
Seems like Thai calendar is not prepared for linking properly.
Comment 4 Jon Douglas [MSFT] 2017-08-28 18:41:46 UTC
Thank you for the report!

For those unfamiliar with Thai, it is the following option:

Settings -> Language -> ไทย

I was able to CONFIRM this issue and see that the linker is stripping away items in System.Globalization. I believe this is intended behavior unless there is static analysis showing the specific ThaiBuddhistCalendar being used in code(Which is how you worked around it). I would guess this would also happen for Korean, Chinese, Japanese, and other types of calendars in the System.Globalization class.

Seems like a mscorlib issue at this point.
Comment 5 gcnew 2017-09-07 10:48:47 UTC
Turns out my workaround didn't work in Release mode. I had to call the following method in the app ctor:
private static void InitThaiCalendarCrashFix()
     var localeIdentifier = Java.Util.Locale.Default.ToString();
     if (localeIdentifier == "th_TH")
        new System.Globalization.ThaiBuddhistCalendar();
Comment 6 Jungmok Han 2017-11-03 17:58:39 UTC
Have you tried to pass the CultureInfo in as IFormatProvider to your Parse calls mentioned in the bug description?

var date = DateTime.Parse("2000-31-07", CultureInfo.InvariantCulture);
var date = DateTime.ParseExact("2000-31-07", "yyyy-dd-MM", CultureInfo.InvariantCulture);
Comment 7 Mikalai Daronin 2017-11-11 18:58:27 UTC
I observe very similar crash with this code: "dateTimeObj.ToString("o");". https://github.com/onovotny/MetroLog/issues/110
Comment 8 gcnew 2017-12-14 05:57:12 UTC
@Jungmok I didn't try other approaches since my workaround solved the problem.