问题描述
不管是什么用户的本地时区设置,使用C#(.NET 2.0)我需要确定的时间(DateTime对象)在东部时区。
Regardless of what the user's local time zone is set to, using C# (.NET 2.0) I need to determine the time (DateTime object) in the Eastern time zone.
我知道这些方法,但似乎没有成为一个明显的方式来获得一个DateTime对象比用户在什么不同的时区。
I know about these methods but there doesn't seem to be an obvious way to get a DateTime object for a different time zone than what the user is in.
DateTime.Now
DateTime.UtcNow
TimeZone.CurrentTimeZone
当然,解决方案需要日光节约时间知道。
Of course, the solution needs to be daylight savings time aware.
推荐答案
由于每个人所提到的,.NET 2不包含任何时区信息。
该信息被存储在注册表中,虽然和它相当琐碎写它周围的包装类:
As everyone else mentioned, .NET 2 doesn't contain any time zone information.The information is stored in the registry, though, and its fairly trivial to write a wrapper class around it:
SOFTWARE \\微软\\的Windows NT \\ CURRENTVERSION \\ Time区域
SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
包含所有时区子项。该TZI字段值包含所有时区的转变和偏置特性,但它在二进制数组全部塞满。最重要的位(偏压和日光),分别存储在位置0 int32s和8:
contains sub-keys for all time zones. The TZI field value contains all the transition and bias properties for a time zone, but it's all stuffed in a binary array. The most important bits (bias and daylight), are int32s stored at positions 0 and 8 respectively:
int bias = BitConverter.ToInt32((byte[])tzKey.GetValue("TZI"), 0);
int daylightBias = BitConverter.ToInt32((byte[])tzKey.GetValue("TZI"), 8);
此页面有一个合理的总结:
http://burks.brighton.ac.uk/burks/language/pascal/uddf/pages/registry.htm
This page has a reasonable summary:http://burks.brighton.ac.uk/burks/language/pascal/uddf/pages/registry.htm
这篇关于获取日期时间对于另一个时区无论本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!