From e4a601dbffb16ea2bf57dc87e30b42a754634d4c Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Mon, 30 Aug 2021 19:35:31 -0700 Subject: [PATCH] Fix TimeZoneInfo.HasIanaId when using Local Time Zone --- .../src/System/TimeZoneInfo.cs | 5 +++-- .../tests/System/TimeZoneInfoTests.cs | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs index ee0baa1e7ec80..c1e1b2e102557 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs @@ -85,7 +85,8 @@ private TimeZoneInfo CreateLocal() timeZone._standardDisplayName, timeZone._daylightDisplayName, timeZone._adjustmentRules, - disableDaylightSavingTime: false); + disableDaylightSavingTime: false, + timeZone.HasIanaId); _localTimeZone = timeZone; } @@ -1954,7 +1955,7 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool else { value = new TimeZoneInfo(match!._id, match._baseUtcOffset, match._displayName, match._standardDisplayName, - match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false); + match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId); } } else diff --git a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs index 38c8bd8c3393a..e9f08a9b389db 100644 --- a/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs +++ b/src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs @@ -2642,6 +2642,24 @@ public static void UsingAlternativeTimeZoneIdsTest(string windowsId, string iana } public static bool SupportIanaNamesConversion => PlatformDetection.IsNotBrowser && PlatformDetection.ICUVersion.Major >= 52; + public static bool SupportIanaNamesConversionAndRemoteExecution => SupportIanaNamesConversion && RemoteExecutor.IsSupported; + + // This test is executed using the remote execution because it needs to run before creating the time zone cache to ensure testing with that state. + // There are already other tests that test after creating the cache. + [ConditionalFact(nameof(SupportIanaNamesConversionAndRemoteExecution))] + public static void IsIanaIdWithNotCacheTest() + { + RemoteExecutor.Invoke(() => + { + Assert.Equal(!s_isWindows || TimeZoneInfo.Local.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase), TimeZoneInfo.Local.HasIanaId); + + TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); + Assert.False(tzi.HasIanaId); + + tzi = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin"); + Assert.True(tzi.HasIanaId); + }).Dispose(); + } [ConditionalFact(nameof(SupportIanaNamesConversion))] [ActiveIssue("https://github.com/dotnet/runtime/issues/52072", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)] @@ -2649,6 +2667,8 @@ public static void IsIanaIdTest() { bool expected = !s_isWindows; + Assert.Equal((expected || TimeZoneInfo.Local.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase)), TimeZoneInfo.Local.HasIanaId); + foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones()) { Assert.True((expected || tzi.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase)) == tzi.HasIanaId, $"`{tzi.Id}` has wrong IANA Id indicator");