Fix calendar instance inside the culture after cloning (dotnet/coreclr#26721)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Mon, 16 Sep 2019 19:18:17 +0000 (12:18 -0700)
committerGitHub <noreply@github.com>
Mon, 16 Sep 2019 19:18:17 +0000 (12:18 -0700)
When creating a new culture, we’ll have the equality CultureInfo.DateTimeInfo.Calendar == CultureInfo.calendar be true. After cloning such culture, this equality would not be true. The fix here is to ensure if the equality is true before cloning then should be true after cloning.

Commit migrated from https://github.com/dotnet/coreclr/commit/634bf8c37b42b0613d79e7837d4f9d7884e6c4f6

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs

index 27888e9..f476638 100644 (file)
@@ -1012,7 +1012,15 @@ namespace System.Globalization
                 ci._textInfo = (TextInfo)_textInfo.Clone();
             }
 
-            if (_calendar != null)
+            if (_dateTimeInfo != null && _dateTimeInfo.Calendar == _calendar)
+            {
+                // Usually when we access CultureInfo.DateTimeFormat first time, we create the DateTimeFormatInfo object
+                // using CultureInfo.Calendar. i.e. CultureInfo.DateTimeInfo.Calendar == CultureInfo.calendar.
+                // When cloning CultureInfo, if we know this still the case CultureInfo.DateTimeInfo.Calendar == CultureInfo.calendar
+                // then we can keep the same behavior for the cloned object and no need to create another calendar object.
+                ci._calendar = ci.DateTimeFormat.Calendar;
+            }
+            else if (_calendar != null)
             {
                 ci._calendar = (Calendar)_calendar.Clone();
             }