[英]What's the Noda Time 2.0 equivalent of MapTimeZoneId?
I have been using the following code without flaw for a while now: 我一直在使用以下代码而没有缺陷:
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];
}
When upgrading NodaTime to version 2.0, I now get a compile-time error saying MapTimeZoneId
no longer exists. 将NodaTime升级到2.0版时,我现在得到一个编译时错误,说
MapTimeZoneId
不再存在。 How do I get this function working again? 如何让这个功能再次运行?
Currently, you need the same code that exists within the gut of Noda Time, but it's not very much: 目前,您需要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;
}
Notes: 笔记:
TimeZoneInfo.Local.Id
will probably already be an IANA ID, so this code would return null
in most cases. TimeZoneInfo.Local.Id
可能已经是IANA ID,因此在大多数情况下此代码将返回null
。 I've filed an issue to handle the fact that this isn't mentioned in the migration guide. 我已经提交了一个问题来处理迁移指南中没有提到的事实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.