Skip to content

Commit 22b53e8

Browse files
authored
Fix TimeZoneInfo.HasIanaId when using Local Time Zone (#58414)
1 parent 1e96637 commit 22b53e8

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ private TimeZoneInfo CreateLocal()
8585
timeZone._standardDisplayName,
8686
timeZone._daylightDisplayName,
8787
timeZone._adjustmentRules,
88-
disableDaylightSavingTime: false);
88+
disableDaylightSavingTime: false,
89+
timeZone.HasIanaId);
8990

9091
_localTimeZone = timeZone;
9192
}
@@ -1954,7 +1955,7 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool
19541955
else
19551956
{
19561957
value = new TimeZoneInfo(match!._id, match._baseUtcOffset, match._displayName, match._standardDisplayName,
1957-
match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false);
1958+
match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId);
19581959
}
19591960
}
19601961
else

src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,13 +2642,33 @@ public static void UsingAlternativeTimeZoneIdsTest(string windowsId, string iana
26422642
}
26432643

26442644
public static bool SupportIanaNamesConversion => PlatformDetection.IsNotBrowser && PlatformDetection.ICUVersion.Major >= 52;
2645+
public static bool SupportIanaNamesConversionAndRemoteExecution => SupportIanaNamesConversion && RemoteExecutor.IsSupported;
2646+
2647+
// 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.
2648+
// There are already other tests that test after creating the cache.
2649+
[ConditionalFact(nameof(SupportIanaNamesConversionAndRemoteExecution))]
2650+
public static void IsIanaIdWithNotCacheTest()
2651+
{
2652+
RemoteExecutor.Invoke(() =>
2653+
{
2654+
Assert.Equal(!s_isWindows || TimeZoneInfo.Local.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase), TimeZoneInfo.Local.HasIanaId);
2655+
2656+
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
2657+
Assert.False(tzi.HasIanaId);
2658+
2659+
tzi = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
2660+
Assert.True(tzi.HasIanaId);
2661+
}).Dispose();
2662+
}
26452663

26462664
[ConditionalFact(nameof(SupportIanaNamesConversion))]
26472665
[ActiveIssue("https://github.com/dotnet/runtime/issues/52072", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
26482666
public static void IsIanaIdTest()
26492667
{
26502668
bool expected = !s_isWindows;
26512669

2670+
Assert.Equal((expected || TimeZoneInfo.Local.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase)), TimeZoneInfo.Local.HasIanaId);
2671+
26522672
foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
26532673
{
26542674
Assert.True((expected || tzi.Id.Equals("Utc", StringComparison.OrdinalIgnoreCase)) == tzi.HasIanaId, $"`{tzi.Id}` has wrong IANA Id indicator");

0 commit comments

Comments
 (0)