c# - 为什么 Windows 10 上缺少某些时区信息?

标签 c# powershell timezone timezone-offset iana

在我的本地计算机上,我正在尝试使用 converter 访问 IANA 时区的偏移值如下,

foreach (var timeZone in TZConvert.KnownIanaTimeZoneNames)
{
  var tzi = TZConvert.GetTimeZoneInfo(timeZone.ToString());
  TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);
  .......
}

TZConvert.GetTimeZoneInfo(timeZone.ToString()) 行有时会抛出错误提示

System.TimeZoneNotFoundException: 'The time zone ID 'Sudan Standard Time' was not found on the local computer.'

当我在 power shell 上使用相同的命令尝试相同的操作时,

PS C:\Users\SajeetharanS> [System.TimeZoneInfo]::FindSystemTimeZoneById("Aleutian Standard Time") Exception calling "FindSystemTimeZoneById" with "1" argument(s): "The time zone ID 'Aleutian Standard Time' was not found on the local computer." At line:1 char:1 + [System.TimeZoneInfo]::FindSystemTimeZoneById("Aleutian Standard Time ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TimeZoneNotFoundException

为什么缺少一些时区信息?我该如何解决?

最佳答案

特定时区,“苏丹标准时间” 只是 released by Microsoft on December 12, 2017 .运行代码的计算机可能还没有此更新。请参阅 the support document 的“如何获取此更新”部分. (基本上,运行 Windows 更新)。

“阿留申标准时间”而言,那是released in June 2016 .因此,如果计算机没有,那么它的更新相当落后。

总的来说,时区是一个移动的目标,因为世界各国政府都在改变他们的标准偏移量和 DST 政策。为了获得准确的本地时间,及时了解更新至关重要。

您正在使用的我的 TimeZoneConverter 库假设计算机是最新的。我会在 Microsoft 和 IANA 发布时区更改时推送更新,但我不会尝试协调特定计算机是否安装了时区。

如果您试图列出计算机上的所有时区,最好使用内置的 TimeZoneInfo.GetSystemTimeZones() 方法,然后使用 .Id 每个属性。

此外,在大多数情况下,您不想使用 .StandardName,因为 .StandardName.DaylightName.DisplayName 属性针对操作系统上安装的语言进行了本地化。有时 .Id.StandardName 匹配,但这并不能保证 - 即使是英文。

关于c# - 为什么 Windows 10 上缺少某些时区信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47824619/

相关文章:

c# - TPL 数据流加速?

c# - 为动画目的简化对对象成员的引用的创建?

c# - 为 EntitySet<T>、IQueryable<T> 和 IEnumerable<T> 创建可重用谓词

c# - System.Data.sqlClient 只会在服务器上创建默认数据库

powershell - 是否可以使用 powershell 查询所有返回任何上次运行结果不是 (0x0) 的作业的计划任务?

windows - 如何获得 Windows 版本?

powershell - 列出 cosmos 数据库的连接字符串不会返回结果,但列出键可以工作

c# - 如何让客户的时区偏移他的IP地址

iOS:将 UTC 时区转换为设备本地时区

javascript - Angular-Ui Bootstrap Popup DatePicker 的 ngModelOptions 的时区属性