How to clear TimeZoneInfo cache in WinRT?

泄露秘密 提交于 2019-12-17 21:20:27

问题


In .NET some values used by TimeZoneInfo are cached, such as the current time zone in use by TimeZoneInfo.Local, DateTime.ToLocalTime, etc. In traditional .NET framework apps, this cache can be cleared by calling TimeZoneInfo.ClearCachedData(), as explained in the docs.

However, in WinRT applications, such as Windows Store apps, and Windows Universal Apps, the ClearCachedData API is not available.

How do you clear the time zone cache in a WinRT environment?


回答1:


There's an undocumented way to achieve this, as seen here in the .NET reference source.

TimeZoneInfo.ConvertTime(new DateTime(0), TimeZoneInfo.Utc);

This will clear the time zone cache, and it works in WinRT as well as traditional .NET Framework (4.6+), .NET Core, and PCL.

According to my colleagues at Microsoft on the .NET team, this is an unofficial workaround to a known problem. It is not expected to be removed, but it is an implementation detail and not part of the public API, so use at your own risk.

Note that TimeZoneInfo.ClearCachedData should be used instead of this approach, when it is available. It was added to .NET Standard 2.0, and UWP 10.1 (see all environments here).



来源:https://stackoverflow.com/questions/34300083/how-to-clear-timezoneinfo-cache-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!