在 Windows 中更改时区不会反映在 C# TimeZone 调用中 [英] Changing timezone in Windows is not reflected in C# TimeZone calls

查看:61
本文介绍了在 Windows 中更改时区不会反映在 C# TimeZone 调用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 7 上.我在 PST 时区.我将 Windows 中的时区更改为东方,然后在 LinqPad 中执行:

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();

为什么不显示东部时区,为什么一个结构显示太平洋时区而另一个结构显示东部时区?

推荐答案

.NET 缓存本地时区.如果您需要确保您考虑了用户可能所做的更改,您需要调用 TimeZoneInfo.ClearCachedData() 在获取本地时区之前.

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

来自 MSDN 文档(重点是我的):

From the MSDN docs (emphasis mine):

缓存的时区数据包括有关本地时区、协调世界时 (UTC) 区域以及使用诸如 FindSystemTimeZoneByIdGetSystemTimeZones<等方法检索的任何时区的数据/代码>.

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.

您可以调用ClearCachedData 方法来减少专用于应用程序时区信息缓存的内存或反映本地系统时区已更改的事实.

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.

这可能也适用于 LinqPad.

This probably applies within LinqPad also.

这篇关于在 Windows 中更改时区不会反映在 C# TimeZone 调用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆