简体   繁体   English

什么是Noda Time 2.0相当于MapTimeZoneId?

[英]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: 笔记:

  • This code will work with time zone IDs that aren't present on your system for whatever reason, but are present in CLDR 此代码将使用系统中出现的时区ID,无论出于何种原因,但在CLDR中都存在
  • If this is all you're doing with Noda Time, consider using TimeZoneConverter instead 如果这是正在与野田的时间做,可以考虑使用TimeZoneConverter代替
  • If you run on .NET Core on non-Windows systems, TimeZoneInfo.Local.Id will probably already be an IANA ID, so this code would return null in most cases. 如果您在非Windows系统上运行.NET Core,则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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM