使用TimeZoneInfo对象查找爱尔兰的时区
本文关键字:时区 爱尔兰 查找 TimeZoneInfo 对象 使用 | 更新日期: 2023-09-27 18:02:07
我需要获取(IST(爱尔兰标准时间的TimeZoneInfo。我执行了以下语句,但以异常结束。我在这里做错了什么?
TimeZoneInfo tmz = TimeZoneInfo.FindSystemTimeZoneById("Ireland Standard Time");
例外情况是
在本地找不到时区ID"爱尔兰标准时间"计算机
爱尔兰的时区Id为"GMT标准时间"。这一款适用于TimeZoneInfo.FindSystemTimeZoneById
one Windows 7。
似乎没有这样的时区。
您可以使用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
...
我想您会得到TimeZoneNotFoundException
找不到id指定的时区标识符。这意味着名称与id匹配的注册表项不存在,或者键存在,但不包含任何时区数据。
链接到msdn
使用的注册表项是:
HKEY_LOCAL_MACHINE''Software''Microsoft''Windows NT''CurrentVersion''Time区域
TimeZoneInfo
支持的时区列表在HKEY_LOCAL_MACHINE'SOFTWARE'Microsoft'Windows NT'CurrentVersion'Time Zones
中。我的Windows7没有任何关于爱尔兰的信息。
来自MSDN:
FindSystemTimeZoneById尝试将id与HKEY_LOCAL_MACHINE''Software''Microsoft''Windows NT''CurrentVersion''Time Zones 的子项名称匹配
在我的本地电脑(Windows7(上,我没有找到ID"爱尔兰标准时间">
MSDN时区信息
从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);
我试图在VB.NET中找到这个解决方案https://msdn.microsoft.com/en-us/library/bb397784aspx,我已经想出了一个解决方案。希望这能让你达到这样一个地步,你只需要一个好的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))