Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

.net - How to clear TimeZoneInfo cache in WinRT?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...