What is the equivalent of Noda Time 2.0 MapTimeZoneId?

I used the following code without flaws:

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 that it MapTimeZoneIdno longer exists. How to make this function work again?

+4
source share
1 answer

Currently, you need the same code that exists in the gut of Noda Time, but that is not very much:

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 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 identifiers that for some reason are not present on your system but are present in the CLDR
  • If this is all you do with the Time Node, it is recommended that you use TimeZoneConverter instead
  • .NET Core , Windows, TimeZoneInfo.Local.Id, , IANA, null.

, , .

+6

Source: https://habr.com/ru/post/1676420/


All Articles