在我的本地计算机中,我尝试使用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.'
当我使用相同的命令在电源shell上尝试相同的时候,
PS C:\ Users \ SajeetharanS> [System.TimeZoneInfo] :: FindSystemTimeZoneById(“阿留申标准时间”)使用“1”参数调用“FindSystemTimeZoneById”的异常:“时区ID'阿留申标准时间'不是在本地电脑上找到 . “在行:1 char:1 [System.TimeZoneInfo] :: FindSystemTimeZoneById(“阿留申标准时间...... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分类信息:未指定:(:) [ ],MethodInvocationException FullyQualifiedErrorId:TimeZoneNotFoundException
为什么缺少一些时区信息?我该怎么办呢?
1 回答
特定时区
"Sudan Standard Time"
只是released by Microsoft on December 12, 2017 . 运行代码的计算机可能还没有此更新 . 请参阅the support document的"How to get this update"部分 . (基本上,运行Windows Update) .至于
"Aleutian Standard Time"
,那是released in June 2016 . 因此,如果计算机没有那个,那么它就会落后于更新 .总的来说,时区是一个不断变化的目标,因为世界各国政府都在改变其标准抵消和DST政策 . 为了获得准确的当地时间,保持最新状态是必不可少的 .
我正在使用的TimeZoneConverter库假设计算机是最新的 . 我推送更新,因为Microsoft和IANA发布了时区更改,但我不会尝试协调特定计算机是否安装了时区 .
如果您尝试列出计算机上的所有时区,最好使用内置的
TimeZoneInfo.GetSystemTimeZones()
方法,然后使用每个时区的.Id
属性 .此外,在大多数情况下,您不希望使用
.StandardName
,因为.StandardName
,.DaylightName
和.DisplayName
属性已针对操作系统上安装的语言进行了本地化 . 有时.Id
与.StandardName
匹配,但这不保证 - 即使是英语 .