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