在NodaTime中无法获得Asia / Kolkata WindowsMap

问题描述

简单地说,我有一个点网Web应用程序,它需要记录用户的时区信息(以便在电子邮件中发送正确的时间)。

using NodaTime.TimeZones;
var winmap = TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones
        .FirstOrDefault(x => x.TzdbIds.Contains(tzinfo));
if (winmap == null) throw new Exception("Invalid timezone");

NodaTime 2.4.8

https://nodatime.org/2.4.x/api/NodaTime.TimeZones.TzdbDateTimeZoneSource.html#NodaTime_TimeZones_TzdbDateTimeZoneSource_WindowsMapping

“亚洲/加尔各答”时区似乎不存在,而且我不确定要使其工作需要什么。有没有更好的方法来实现这一目标?

解决方法

问题在于Windows / TZDB映射文件(example)不包含“亚洲/加尔各答”,而包含“亚洲/加尔各答”。

在用户代码中对此进行解释比较棘手,这就是为什么在NodaTime 3.0中我们引入了TzdbDateTimeZoneSource.TzdbToWindowsIds

更新到3.0后,您可以使用:

if (!TzdbDateTimeZoneSource.Default.TzdbToWindowsIds.TryGetValue(tzinfo,out var windowsZoneId))
{
   throw new Exception($"Unmapped time zone ID '{tzinfo}'");
}
// Use windowsZoneId here

如果您真的需要坚持使用2.4.8,则可以规范化tzinfoTzdbDateTimeZoneSource.Default.WindowsMapping.MapZones.TzdbIds中的所有条目,但这通常会更糟。

(对于大多数用户,从2.4.8到3.0.0的更新应该是无缝的。由于删除了二进制序列化,这是主要的重大更改,我希望您不要使用... )

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...