![](/img/trans.png)
[英]Noda Timezone issue with IDateTimeZoneSource.MapTimeZoneId
[英]What's the Noda Time 2.0 equivalent of MapTimeZoneId?
我一直在使用以下代码而没有缺陷:
internal static string WindowsToIana(string windowsZoneId)
{
if (windowsZoneId.Equals("UTC", StringComparison.Ordinal))
return "Etc/UTC";
var tzdbSource = NodaTime.TimeZones.TzdbDateTimeZoneSource.Default;
var tzi = TimeZoneInfo.FindSystemTimeZoneById(windowsZoneId);
if (tzi == null) return null;
var tzid = tzdbSource.MapTimeZoneId(tzi);
if (tzid == null) return null;
return tzdbSource.CanonicalIdMap[tzid];
}
将NodaTime升级到2.0版时,我现在得到一个编译时错误,说MapTimeZoneId
不再存在。 如何让这个功能再次运行?
目前,您需要Noda Time内部存在的相同代码,但它并不是很多:
internal static string WindowsToIana(string windowsZoneId)
{
// Avoid UTC being mapped to Etc/GMT, which is the mapping in CLDR
if (windowsZoneId == "UTC")
{
return "Etc/UTC";
}
var source = TzdbDateTimeZoneSource.Default;
string result;
// If there's no such mapping, result will be null.
source.WindowsMapping.PrimaryMapping.TryGetValue(windowsZoneId, out result);
// Canonicalize
if (result != null)
{
result = source.CanonicalIdMap[result];
}
return result;
}
笔记:
TimeZoneInfo.Local.Id
可能已经是IANA ID,因此在大多数情况下此代码将返回null
。 我已经提交了一个问题来处理迁移指南中没有提到的事实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.