无论本地时区如何,都可以获取另一个时区的日期时间 [英] Get DateTime For Another Time Zone Regardless of Local Time Zone
问题描述
无论用户的本地时区设置为何,使用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\Microsoft\Windows NT\\ \\CurrentVersion\TimeZones
SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
包含所有时区的子键。 TZI字段值包含时区的所有转换和偏置属性,但它都填充在二进制数组中。最重要的位(偏差和日光)分别是位于0和8位置的int32:
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
这篇关于无论本地时区如何,都可以获取另一个时区的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!