在我的本地计算机上,我正在尝试使用 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/