c# - NodaTime .NET Core 实现中的 BCL DateTimeZoneProvider 在哪里?

标签 c# timezone .net-core nodatime base-class-library

我使用 NodaTime 在 ical.net 中进行时区转换,因为它比之前尝试使用 VTIMEZONE 元素来处理时间更改和时区转换的实现要好得多。

在幕后,这种方法对性能非常重要:它将测试套件的运行时间从大约 6 秒减少到大约 2.5 秒。

public static DateTimeZone GetZone(string tzId)
{
    // IANA lookup attempt

    zone = DateTimeZoneProviders.Bcl.GetZoneOrNull(tzId);
    if (zone != null)
    {
        return zone;
    }

    // Serialization lookup attempt
    // Some other tricks to find a reasonable time zone, etc.
}

NodaTime 的 .NET Core 实现没有 Bcl 作为 DateTimeZoneProvider。 (它仍然有 TzdbSerialization。)我在 NodaTime 源代码中搜索了一下,但我不确定替换是什么,如果有的话。

在 NodaTime 的 .NET Core 端口中,我们应该使用什么进行 BCL 时区查找?

最佳答案

What should we be using for BCL time zone lookups in the .NET Core port of NodaTime?

不幸的是,Noda Time 1.x 支持的 PCL 配置文件中没有很好的 TimeZoneInfo 支持 - 即使是 FindSystemTimeZoneById()Id 属性没有提供。正如评论中所指出的,有令人鼓舞的迹象表明它们可能在 .NET Core 本身中——我需要看看。

你可以使用 TzdbDateTimeZoneSource.WindowsMapping获得最近的等效 IANA/TZDB 时区...但理想情况下,如果您可以在整个代码中使用 IANA/TZDB 时区 ID 会更好。 (这些对于其他平台也将更便携。)

即使 .NET Core 支持 FindSystemTimeZoneById,如果您使用的是 Linux,它也不会提供 Windows 时区 - 它使用 zoneinfo 文件。因此,如果您需要 Windows 时区可移植性,我认为 WindowsMapping 确实是最佳选择。

更新:再看一遍,我们绝对不能在 .NET Core 中支持 TimeZoneInfo,因为它不公开调整规则:(

关于c# - NodaTime .NET Core 实现中的 BCL DateTimeZoneProvider 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190911/

相关文章:

c# - MVC 5、EF 6 Barebones 无效的列名称 'Model'

r - 时区、期间。使用 R 在 xts 中应用

json - 使用 Powershell 查找和替换嵌套的 JSON 值

.net-core - 如何强制Swashbuckle.Aspnetcore.Cli使用netcoreapp3.1?

c# - 如何通过以下方式制作 Resharper 格式的换行三元运算符

c# - 将自定义动词添加到 ToolStrip 设计器,而无需重新实现 ToolStripDesigner

c# - 使用 Moq 测试接受委托(delegate)的方法

Java 时区数据库与 IANA 数据

c# - 有没有办法在 C# (ASP.NET MVC3) 中更改请求的时区?

.net-core - 在 .netcore web api 项目中将 null 值传递给 web api 调用中的可为空属性时出现问题