DateTimeOffset和时区更改 [英] DateTimeOffset and time zone change
本文介绍了DateTimeOffset和时区更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一个简单的代码:
Consider a simplee code:
static void Main(string[] args)
{
while (true)
{
Console.ReadKey();
Console.WriteLine(DateTimeOffset.Now.ToString());
}
}
每次按任意键,我都会获得当前时区。
现在当我更改时区,并再次询问日期时 - 我得到相同的时间,即没有应用任何更改。再次运行应用程序后,更改发生了,我终于看到了时区的时间,我设置了。
有人能解释我,如何获得实际没有重新加载应用程序的时间+时区?
谢谢
Each time I press any key, I get the current time with timezone.
Now when I change the timezone, and again "ask the date" - I get the same time, i.e. no changes are applied. After running the application again, the changes take place and I see at last the time from the timezone, I set.
Could somebody explain me, how to get the actual time+timezone without reloading the application?
Thanks
推荐答案
检查 TimeZone.CurrentTimeZone [ ^ ]查看进程是否收到有关更改的通知。时区是线程上下文,它不会遵循系统时区的变化。你必须抓住这样的事件。据我所知,没有具体事件,但附近:请参阅此主题: http://stackoverflow.com/questions/9725180/c-sharp-event-to-detect-daylight-saving-or-even-manual-time-change [<一个href =http://stackoverflow.com/questions/9725180/c-sharp-event-to-detect-daylight-saving-or-even-manual-time-changetarget =_ blanktitle =New Window > ^ ]
Check with TimeZone.CurrentTimeZone[^] to see if the process got notified about the change. Timezone is thread context, it will not follow the system timezone changes. You have to catch such events. As I know there is no specific event, but near: see this topic: http://stackoverflow.com/questions/9725180/c-sharp-event-to-detect-daylight-saving-or-even-manual-time-change[^]
我想这会对你有所帮助
http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx [ ^ ]
i think this will help you
http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx[^]
DateTimeOffset.Now 使用 TimeZoneInfo 并在启动时缓存时区。
清除缓存通话:
DateTimeOffset.Now uses TimeZoneInfo and it caches timezone on startup.
To clear the cache call:
TimeZoneInfo.ClearCachedData();
这可以解决你的问题: - )
This should solve your problem :-)
这篇关于DateTimeOffset和时区更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文