diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs index 3c8f5119b7d2ac..0cd7cb3ebf3437 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs @@ -127,6 +127,7 @@ public DateTimeKind GetCorrespondingKind(TimeZoneInfo? timeZone) public Dictionary? _systemTimeZones; public ReadOnlyCollection? _readOnlySystemTimeZones; + public Dictionary? _timeZonesUsingAlternativeIds; public bool _allSystemTimeZonesRead; } @@ -1898,6 +1899,9 @@ private static TimeZoneInfoResult TryGetTimeZone(string id, bool dstDisabled, ou } } + cachedData._timeZonesUsingAlternativeIds ??= new Dictionary(StringComparer.OrdinalIgnoreCase); + cachedData._timeZonesUsingAlternativeIds[id] = zone; + Debug.Assert(zone != null); value = zone; } @@ -1917,17 +1921,26 @@ private static TimeZoneInfoResult TryGetTimeZoneUsingId(string id, bool dstDisab // check the cache if (cachedData._systemTimeZones != null) { - if (cachedData._systemTimeZones.TryGetValue(id, out TimeZoneInfo? match)) + if (cachedData._systemTimeZones.TryGetValue(id, out value)) { - if (dstDisabled && match._supportsDaylightSavingTime) + if (dstDisabled && value._supportsDaylightSavingTime) { // we found a cache hit but we want a time zone without DST and this one has DST data - value = CreateCustomTimeZone(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName); + value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName); } - else + + return result; + } + } + + if (cachedData._timeZonesUsingAlternativeIds != null) + { + if (cachedData._timeZonesUsingAlternativeIds.TryGetValue(id, out value)) + { + if (dstDisabled && value._supportsDaylightSavingTime) { - value = new TimeZoneInfo(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName, - match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId); + // we found a cache hit but we want a time zone without DST and this one has DST data + value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName); } return result; @@ -1958,7 +1971,7 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool { TimeZoneInfoResult result; - result = TryGetTimeZoneFromLocalMachine(id, out TimeZoneInfo? match, out e); + result = TryGetTimeZoneFromLocalMachine(id, out value, out e); if (result == TimeZoneInfoResult.Success) { @@ -1971,24 +1984,15 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, bool // uses reference equality with the Utc object. if (!id.Equals(UtcId, StringComparison.OrdinalIgnoreCase)) { - cachedData._systemTimeZones.Add(id, match!); + cachedData._systemTimeZones.Add(id, value!); } - if (dstDisabled && match!._supportsDaylightSavingTime) + if (dstDisabled && value!._supportsDaylightSavingTime) { // we found a cache hit but we want a time zone without DST and this one has DST data - value = CreateCustomTimeZone(match._id, match._baseUtcOffset, match._displayName, match._standardDisplayName); - } - else - { - value = new TimeZoneInfo(match!._id, match._baseUtcOffset, match._displayName, match._standardDisplayName, - match._daylightDisplayName, match._adjustmentRules, disableDaylightSavingTime: false, match.HasIanaId); + value = CreateCustomTimeZone(value._id, value._baseUtcOffset, value._displayName, value._standardDisplayName); } } - else - { - value = null; - } return result; }