Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Introducing TimeZoneInfo.GetSystemTimeZones(bool skipSorting)#89985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tarekgh
merged 2 commits into
dotnet:main
from
tarekgh:IntroduceUnSortedGetSystemTimeZonesAug 4, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2 src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 36 additions & 14 deletions
50 src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -131,6 +131,7 @@ public DateTimeKind GetCorrespondingKind(TimeZoneInfo? timeZone) | ||
| public Dictionary<string, TimeZoneInfo>? _systemTimeZones; | ||
| public ReadOnlyCollection<TimeZoneInfo>? _readOnlySystemTimeZones; | ||
| public ReadOnlyCollection<TimeZoneInfo>? _readOnlyUnsortedSystemTimeZones; | ||
| public Dictionary<string, TimeZoneInfo>? _timeZonesUsingAlternativeIds; | ||
| public bool _allSystemTimeZonesRead; | ||
| } | ||
| @@ -880,41 +881,62 @@ public static TimeZoneInfo FromSerializedString(string source) | ||
| /// <see cref="DisplayName"/>. | ||
| /// This method does *not* throw TimeZoneNotFoundException or InvalidTimeZoneException. | ||
| /// </summary> | ||
| public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones() | ||
| public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones() => GetSystemTimeZones(skipSorting: false); | ||
| /// <summary> | ||
| /// Returns a <see cref="ReadOnlyCollection{TimeZoneInfo}"/> containing all valid TimeZone's from the local machine. | ||
| /// This method does *not* throw TimeZoneNotFoundException or InvalidTimeZoneException. | ||
| /// </summary> | ||
| /// <param name="skipSorting">If true, The collection returned may not necessarily be sorted.</param> | ||
| /// <remarks>By setting the skipSorting parameter to true, the method will attempt to avoid sorting the returned collection. | ||
| /// This option can be beneficial when the caller does not require a sorted list and aims to enhance the performance. </remarks> | ||
| public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones(bool skipSorting) | ||
| { | ||
| CachedData cachedData = s_cachedData; | ||
| lock (cachedData) | ||
| { | ||
| if (cachedData._readOnlySystemTimeZones == null) | ||
| if ((skipSorting ? cachedData._readOnlyUnsortedSystemTimeZones : cachedData._readOnlySystemTimeZones) is null) | ||
| { | ||
| PopulateAllSystemTimeZones(cachedData); | ||
| cachedData._allSystemTimeZonesRead = true; | ||
| if (!cachedData._allSystemTimeZonesRead) | ||
| { | ||
| PopulateAllSystemTimeZones(cachedData); | ||
| cachedData._allSystemTimeZonesRead = true; | ||
| } | ||
| if (cachedData._systemTimeZones != null) | ||
| { | ||
| // return a collection of the cached system time zones | ||
| TimeZoneInfo[] array = new TimeZoneInfo[cachedData._systemTimeZones.Count]; | ||
| cachedData._systemTimeZones.Values.CopyTo(array, 0); | ||
tarekgh marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // sort and copy the TimeZoneInfo's into a ReadOnlyCollection for the user | ||
| Array.Sort(array, static (x, y) => | ||
| if (!skipSorting) | ||
| { | ||
| // sort by BaseUtcOffset first and by DisplayName second - this is similar to the Windows Date/Time control panel | ||
| int comparison = x.BaseUtcOffset.CompareTo(y.BaseUtcOffset); | ||
| return comparison == 0 ? string.CompareOrdinal(x.DisplayName, y.DisplayName) : comparison; | ||
| }); | ||
| cachedData._readOnlySystemTimeZones = new ReadOnlyCollection<TimeZoneInfo>(array); | ||
| // sort and copy the TimeZoneInfo's into a ReadOnlyCollection for the user | ||
| Array.Sort(array, static (x, y) => | ||
| { | ||
| // sort by BaseUtcOffset first and by DisplayName second - this is similar to the Windows Date/Time control panel | ||
| int comparison = x.BaseUtcOffset.CompareTo(y.BaseUtcOffset); | ||
| return comparison == 0 ? string.CompareOrdinal(x.DisplayName, y.DisplayName) : comparison; | ||
| }); | ||
| // Always reset _readOnlyUnsortedSystemTimeZones even if it was initialized before. This prevents the need to maintain two separate cache lists in memory | ||
| // and guarantees that if _readOnlySystemTimeZones is initialized, _readOnlyUnsortedSystemTimeZones is also initialized. | ||
| cachedData._readOnlySystemTimeZones = cachedData._readOnlyUnsortedSystemTimeZones = new ReadOnlyCollection<TimeZoneInfo>(array); | ||
tarekgh marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| else | ||
| { | ||
| cachedData._readOnlyUnsortedSystemTimeZones = new ReadOnlyCollection<TimeZoneInfo>(array); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| cachedData._readOnlySystemTimeZones = ReadOnlyCollection<TimeZoneInfo>.Empty; | ||
| cachedData._readOnlySystemTimeZones = cachedData._readOnlyUnsortedSystemTimeZones = ReadOnlyCollection<TimeZoneInfo>.Empty; | ||
| } | ||
| } | ||
| } | ||
| return cachedData._readOnlySystemTimeZones; | ||
| return skipSorting ? cachedData._readOnlyUnsortedSystemTimeZones! : cachedData._readOnlySystemTimeZones!; | ||
| } | ||
| /// <summary> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78 src/libraries/System.Runtime/tests/System/TimeZoneInfoTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1999,6 +1999,84 @@ public static void GetSystemTimeZones() | ||
| } | ||
| } | ||
| private static void ValidateTimeZonesSorting(ReadOnlyCollection<TimeZoneInfo> zones) | ||
| { | ||
| // validate sorting: first by base offset, then by display name | ||
| for (int i = 1; i < zones.Count; i++) | ||
| { | ||
| TimeZoneInfo previous = zones[i - 1]; | ||
| TimeZoneInfo current = zones[i]; | ||
| int baseOffsetsCompared = current.BaseUtcOffset.CompareTo(previous.BaseUtcOffset); | ||
| Assert.True(baseOffsetsCompared >= 0, | ||
| string.Format($"TimeZoneInfos are out of order. {previous.Id}:{previous.BaseUtcOffset} should be before {current.Id}:{current.BaseUtcOffset}")); | ||
| if (baseOffsetsCompared == 0) | ||
| { | ||
| Assert.True(string.CompareOrdinal(current.DisplayName, previous.DisplayName) >= 0, | ||
| string.Format($"TimeZoneInfos are out of order. {previous.DisplayName} should be before {current.DisplayName}")); | ||
| } | ||
| } | ||
| } | ||
| private static void ValidateDifferentTimeZoneLists(ReadOnlyCollection<TimeZoneInfo> defaultList, ReadOnlyCollection<TimeZoneInfo> nonSortedList, ReadOnlyCollection<TimeZoneInfo> sortedList) | ||
| { | ||
| Assert.Equal(defaultList.Count, nonSortedList.Count); | ||
| Assert.Equal(defaultList.Count, sortedList.Count); | ||
| Assert.Equal(defaultList.Count, nonSortedList.Count); | ||
| Assert.True(object.ReferenceEquals(defaultList, sortedList)); | ||
| Dictionary<string, TimeZoneInfo> zones1Dict = defaultList.ToDictionary(t => t.Id); | ||
| foreach (TimeZoneInfo zone in nonSortedList) | ||
| { | ||
| Assert.True(zones1Dict.TryGetValue(zone.Id, out TimeZoneInfo zone1)); | ||
| } | ||
| ValidateTimeZonesSorting(defaultList); | ||
| } | ||
| [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
tarekgh marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| public static void TestGetSystemTimeZonesCollectionsCallsOrder() | ||
| { | ||
| RemoteExecutor.Invoke(() => | ||
| { | ||
| // | ||
| // Get sorted list first and then the unsorted list | ||
| // | ||
| var zones1 = TimeZoneInfo.GetSystemTimeZones(); | ||
| var zones2 = TimeZoneInfo.GetSystemTimeZones(skipSorting: true); | ||
| var zones3 = TimeZoneInfo.GetSystemTimeZones(skipSorting: false); | ||
| ValidateDifferentTimeZoneLists(zones1, zones2, zones3); | ||
| // | ||
| // Clear our caches so zone enumeration is forced to re-read the data | ||
| // | ||
| TimeZoneInfo.ClearCachedData(); | ||
| // | ||
| // Get unsorted list first and then the sorted list | ||
| // | ||
| zones2 = TimeZoneInfo.GetSystemTimeZones(skipSorting: true); | ||
| zones3 = TimeZoneInfo.GetSystemTimeZones(skipSorting: false); | ||
| zones1 = TimeZoneInfo.GetSystemTimeZones(); | ||
| ValidateDifferentTimeZoneLists(zones1, zones2, zones3); | ||
| }).Dispose(); | ||
| } | ||
| [Fact] | ||
| public static void TestGetSystemTimeZonesCollections() | ||
| { | ||
| // This test doing similar checks as TestGetSystemTimeZonesCollectionsCallsOrder does except we need to | ||
| // run this test without the RemoteExecutor to ensure testing on platforms like Android. | ||
| ReadOnlyCollection<TimeZoneInfo> unsortedList = TimeZoneInfo.GetSystemTimeZones(skipSorting: true); | ||
| ReadOnlyCollection<TimeZoneInfo> sortedList = TimeZoneInfo.GetSystemTimeZones(skipSorting: false); | ||
| ReadOnlyCollection<TimeZoneInfo> defaultList = TimeZoneInfo.GetSystemTimeZones(); | ||
| ValidateDifferentTimeZoneLists(defaultList, unsortedList, sortedList); | ||
| } | ||
| [Fact] | ||
| public static void DaylightTransitionsExactTime() | ||
| { | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.