From 1393190c4d461992d043ee49dd895f587f4d2cc3 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 28 Sep 2021 09:52:47 -0700 Subject: [PATCH 1/4] Fix TimeZone when running with Globalization Invariant Mode --- ...TimeZoneInfo.FullGlobalizationData.Unix.cs | 5 ++++ .../tests/System/TimeZoneInfoTests.cs | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs index 5a80d7b448649d..e0c284b6c53522 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs @@ -24,6 +24,11 @@ public sealed partial class TimeZoneInfo // Main function that is called during construction to populate the three display names private static void TryPopulateTimeZoneDisplayNamesFromGlobalizationData(string timeZoneId, TimeSpan baseUtcOffset, ref string? standardDisplayName, ref string? daylightDisplayName, ref string? displayName) { + if (GlobalizationMode.Invariant) + { + return; + } + // Determine the culture to use CultureInfo uiCulture = CultureInfo.CurrentUICulture; if (uiCulture.Name.Length == 0) diff --git a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs index f560cf7d10ae4a..3bb4c7dc963e20 100644 --- a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs @@ -2541,6 +2541,32 @@ public static void NJulianRuleTest(string posixRule, int dayNumber, int monthNum } } + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + [PlatformSpecific(TestPlatforms.AnyUnix)] + public static void TimeZoneInfo_LocalZoneWithInvariantMode() + { + bool runningInInvariantMode = false; + try { CultureInfo.GetCultureInfo("en-US"); } catch { runningInInvariantMode = true; } + + string hostTZId = TimeZoneInfo.Local.Id; + + ProcessStartInfo psi = new ProcessStartInfo() { UseShellExecute = false }; + psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", runningInInvariantMode ? "0" : "1"); + + RemoteExecutor.Invoke((tzId, hostIsRunningInInvariantMode) => + { + bool hostInvariantMode = bool.Parse(hostIsRunningInInvariantMode); + + if (!hostInvariantMode) + { + Assert.Throws(() => CultureInfo.GetCultureInfo("en-US")); + } + + Assert.Equal(tzId, TimeZoneInfo.Local.Id); + + }, hostTZId, runningInInvariantMode.ToString(), new RemoteInvokeOptions { StartInfo = psi}).Dispose(); + } + [Fact] public static void TimeZoneInfo_DaylightDeltaIsNoMoreThan12Hours() { From e6b8e04459b838bbbb191b4c23e15efab738df8e Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 28 Sep 2021 10:45:07 -0700 Subject: [PATCH 2/4] Use PlatformDetection class in the test --- .../System.Runtime/tests/System/TimeZoneInfoTests.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs index 3bb4c7dc963e20..a3b310eb5828b8 100644 --- a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs @@ -2545,13 +2545,10 @@ public static void NJulianRuleTest(string posixRule, int dayNumber, int monthNum [PlatformSpecific(TestPlatforms.AnyUnix)] public static void TimeZoneInfo_LocalZoneWithInvariantMode() { - bool runningInInvariantMode = false; - try { CultureInfo.GetCultureInfo("en-US"); } catch { runningInInvariantMode = true; } - string hostTZId = TimeZoneInfo.Local.Id; ProcessStartInfo psi = new ProcessStartInfo() { UseShellExecute = false }; - psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", runningInInvariantMode ? "0" : "1"); + psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", PlatformDetection.IsInvariantGlobalization ? "0" : "1"); RemoteExecutor.Invoke((tzId, hostIsRunningInInvariantMode) => { @@ -2564,7 +2561,7 @@ public static void TimeZoneInfo_LocalZoneWithInvariantMode() Assert.Equal(tzId, TimeZoneInfo.Local.Id); - }, hostTZId, runningInInvariantMode.ToString(), new RemoteInvokeOptions { StartInfo = psi}).Dispose(); + }, hostTZId, PlatformDetection.IsInvariantGlobalization.ToString(), new RemoteInvokeOptions { StartInfo = psi}).Dispose(); } [Fact] From a1202fd4113a105ed087d11796a442c80102d910 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 28 Sep 2021 11:00:07 -0700 Subject: [PATCH 3/4] Not restricting teh test to Linux only --- src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs index a3b310eb5828b8..b9bad01252542f 100644 --- a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs @@ -2542,7 +2542,6 @@ public static void NJulianRuleTest(string posixRule, int dayNumber, int monthNum } [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] - [PlatformSpecific(TestPlatforms.AnyUnix)] public static void TimeZoneInfo_LocalZoneWithInvariantMode() { string hostTZId = TimeZoneInfo.Local.Id; From cf98ee0d7d504c3a508cbe195c4e3aa2c8a38715 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 28 Sep 2021 11:25:17 -0700 Subject: [PATCH 4/4] Add test comment --- src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs index b9bad01252542f..a61f6a347c296c 100644 --- a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs @@ -2555,6 +2555,8 @@ public static void TimeZoneInfo_LocalZoneWithInvariantMode() if (!hostInvariantMode) { + // If hostInvariantMode is false, means the child process should enable the globalization invariant mode. + // We validate here that by trying to create a culture which should throws in such mode. Assert.Throws(() => CultureInfo.GetCultureInfo("en-US")); }