Skip to content

Duplicate ID in TimeZoneInfo#114

Merged
mattjohnsonpint merged 3 commits intomattjohnsonpint:mainfrom
Lunat1q:main
Aug 7, 2022
Merged

Duplicate ID in TimeZoneInfo#114
mattjohnsonpint merged 3 commits intomattjohnsonpint:mainfrom
Lunat1q:main

Conversation

@Lunat1q
Copy link
Contributor

@Lunat1q Lunat1q commented Apr 27, 2022

With current implementation I face this issue:

System.TypeInitializationException: The type initializer for 'TimeZoneConverter.TZConvert' threw an exception. 
---> System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary.Insert(TKey key, TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer)
   at TimeZoneConverter.TZConvert.GetSystemTimeZones()
   at TimeZoneConverter.TZConvert..cctor()
   at TimeZoneConverter.TZConvert.GetTimeZoneInfo(String windowsOrIanaTimeZoneId)

According to https://referencesource.microsoft.com/#mscorlib/system/timezoneinfo.cs,2881 there could be 2 Time zone Id with same name but different case.
However, I clearly understand that registry key may not have subkeys with same name but different case. Yet this issue is reproducing. To mitigate this and prevent same issue in the future, it worth adding such distinct check.

@Lunat1q
Copy link
Contributor Author

Lunat1q commented May 20, 2022

@mattjohnsonpint, do you have any comments regarding that?

@mattjohnsonpint
Copy link
Owner

Thanks for the PR, and sorry for the delay.

I changed the implementation slightly, to avoid a custom comparer. Also, I added a call to TimeZoneInfo.ClearCachedData before getting the system time zones, which should hopefully avoid the issue entirely.

@mattjohnsonpint mattjohnsonpint merged commit 55cfa25 into mattjohnsonpint:main Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants