我需要获得(IST)爱尔兰标准时间的TimeZoneInfo。我执行了以下声明但以异常结束。我在这做错了什么?
TimeZoneInfo tmz = TimeZoneInfo.FindSystemTimeZoneById("Ireland Standard Time");
例外说
在当地找不到时区ID“爱尔兰标准时间” 计算机。
答案 0 :(得分:6)
爱尔兰的时区ID是“GMT标准时间”。这个适用于我TimeZoneInfo.FindSystemTimeZoneById
一个Windows 7。
答案 1 :(得分:2)
我猜你得到一个 TimeZoneNotFoundException
找不到id指定的时区标识符。这意味着 一个名称与id匹配的注册表项不存在,或者是 密钥存在但不包含任何时区数据。
使用的注册表项是:
HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Time 区
答案 2 :(得分:2)
似乎没有任何这样的时区。
您可以使用GetSystemTimeZones
方法获取时区。我用了这段代码:
foreach (var zone in TimeZoneInfo.GetSystemTimeZones()) {
Console.WriteLine("{0:00.00} {1}", zone.BaseUtcOffset.TotalHours, zone.Id);
}
在列表中间是您可能感兴趣的时区:
...
-01,00 Azores Standard Time
-01,00 Cape Verde Standard Time
00,00 Morocco Standard Time
00,00 UTC
00,00 GMT Standard Time
00,00 Greenwich Standard Time
01,00 W. Europe Standard Time
01,00 Central Europe Standard Time
01,00 Romance Standard Time
01,00 Central European Standard Time
01,00 W. Central Africa Standard Time
01,00 Namibia Standard Time
...
答案 3 :(得分:1)
TimeZoneInfo
支持的时区列表位于HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
。我的Windows 7副本没有爱尔兰的任何信息。
答案 4 :(得分:0)
来自MSDN:
FindSystemTimeZoneById尝试将id与HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Time Zones的子项名称匹配
在我的本地电脑(Windows 7)上,我没有找到ID“爱尔兰标准时间”
答案 5 :(得分:0)
从msdn
复制的示例运行它以查看您可以使用的时区ID:
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The local system has the following {0} time zones", zones.Count);
foreach (TimeZoneInfo zone in zones)
Console.WriteLine(zone.Id);
答案 6 :(得分:0)
我试图在VB.NET中找到这个解决方案。感谢https://msdn.microsoft.com/en-us/library/bb397784(v=vs.110).aspx处的代码,我已经找到了解决方案。希望这会让你到达需要一个好的VB.NET到C#转换器来结束这个已有5年历史的问题。
Dim IST As TimeZoneInfo
' Declare necessary TimeZoneInfo.AdjustmentRule objects for time zone
' delta is the amount of change during DST
Dim delta As New TimeSpan(1, 0, 0)
Dim adjustment As TimeZoneInfo.AdjustmentRule
Dim adjustmentList As New List(Of TimeZoneInfo.AdjustmentRule)
' Declare transition time variables to hold transition time information
Dim transitionRuleStart, transitionRuleEnd As TimeZoneInfo.TransitionTime
' Simplifying some elements for later use
Dim CurrTime As Date = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified)
Dim EST As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim CST As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
Dim UTC As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("UTC")
' Define new Irish Standard Time zone at UTC, but with DST
transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00 AM#, 3, 2, DayOfWeek.Sunday)
transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(#2:00:00 AM#, 11, 1, DayOfWeek.Sunday)
adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(#1/1/2007#, Date.MaxValue.Date, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add(adjustment)
Dim adjustments(adjustmentList.Count - 1) As TimeZoneInfo.AdjustmentRule
adjustmentList.CopyTo(adjustments)
IST = TimeZoneInfo.CreateCustomTimeZone("IST", New TimeSpan(0, 0, 0), _
"(GMT 00:00) Irish Standard Time (Ireland)", "Irish Standard Time", _
"Irish Daylight Time", adjustments)
' Testing.
Debug.Print("Time in Ireland: " & TimeZoneInfo.ConvertTime(CurrTime, UTC, IST))
Debug.Print("Time in New York: " & TimeZoneInfo.ConvertTime(CurrTime, UTC, EST))
Debug.Print("Time in Chicago: " & TimeZoneInfo.ConvertTime(CurrTime, UTC, CST))