Changing timezone in Windows is not reflected in C# TimeZone calls

天大地大妈咪最大 提交于 2020-06-29 20:27:42

问题


On Windows 7. I am in PST TimeZone. I changed the timezone in Windows to Eastern and then executed this in LinqPad:

 TimeZoneInfo tzinfo = TimeZoneInfo.Local;
 tzinfo.Dump();

 TimeZone localZone = TimeZone.CurrentTimeZone;
  localZone.Dump();

Why is it not showing Eastern timezone and why one structure is showing Pacific timezone and the other Eastern?


回答1:


.NET Caches the local time zone. If you need to be sure that you account for changes the user may have made, you need to call TimeZoneInfo.ClearCachedData() before getting the local time zone.

From the MSDN docs (emphasis mine):

Cached time zone data includes data on the local time zone, the Coordinated Universal Time (UTC) zone, and any time zones that are retrieved by using methods such as FindSystemTimeZoneById and GetSystemTimeZones.

You might call the ClearCachedData method to reduce the memory devoted to the application's cache of time zone information or to reflect the fact that the local system's time zone has changed.

This probably applies within LinqPad also.



来源:https://stackoverflow.com/questions/45850737/changing-timezone-in-windows-is-not-reflected-in-c-sharp-timezone-calls

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