首页 文章

使用TimeZoneInfo对象查找Ireland的时区

提问于
浏览
1

我需要获得(IST)爱尔兰标准时间的TimeZoneInfo . 我执行了以下语句但以异常结束 . 我在这做错了什么?

TimeZoneInfo tmz = TimeZoneInfo.FindSystemTimeZoneById("Ireland Standard Time");

例外说

在本地计算机上找不到时区ID“爱尔兰标准时间” .

7 回答

  • 0

    来自MSDN:

    FindSystemTimeZoneById尝试将id与HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Time Zones的子项名称匹配

    在我的本地电脑(Windows 7)我没有找到ID“爱尔兰标准时间”

    MSDN TimeZoneInfo

  • 0

    似乎没有任何这样的时区 .

    您可以使用 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
    ...
    
  • 2

    我想你得到 TimeZoneNotFoundException

    找不到由id指定的时区标识符 . 这意味着名称与id匹配的注册表项不存在,或者该键存在但不包含任何时区数据 .

    Link to msdn

    使用的注册表项是:

    HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Time Zones

  • 2

    TimeZoneInfo 支持的时区列表位于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones 中 . 我的Windows 7副本没有爱尔兰的任何信息 .

  • 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))
    
  • 1

    爱尔兰的时区ID为"GMT Standard Time" . 这个适合我使用 TimeZoneInfo.FindSystemTimeZoneById 一个Windows 7 .

  • 6

    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);
    

相关问题