NodaTime IANA 时区偏移量与 Moment JS 不匹配

NodaTime IANA Timezone offsets not matching with Moment JS

我最近开始使用 NodaTime,我必须说这是一项了不起的工作。

但是,为了测试我做的事情是否正确,我从 here 中获取了 IANA 时区名称,并使用 NodaTime 为每个名称打印了 UTC 偏移量。

/* string[] ALL_ZONE_NAMES = list of zone names */
foreach(var timezone in ALL_ZONE_NAMES)
        {
            var ob = DateTimeZoneProviders.Tzdb[timezone];
            Console.WriteLine($"{timezone} --> {ob.GetUtcOffset(new Instant())}");
        }

现在,对于特定时区,例如 Pacific/Apia,偏移量打印为 -11

但是当我从 momentJS 开始在 Chrome 浏览器(控制台)上尝试相同时,我得到了 +14

moment.tz(new Date().toISOString(), "Pacific/Apia").format()
"2020-12-10T01:33:13+14:00"

我是不是遗漏了什么?

在 Noda 时间代码中,您使用的是 new Instant(),它指的是 Unix 纪元 (1970-01-01T00:00:00Z)。在 Javascript 代码中,您使用的 new Date() 使用当前日期和时间。

更改您的 Noda Time 代码以使用 ob.GetUtcOffset(SystemClock.Instance.GetCurrentInstant()) 使其表现得像 Javascript 代码。

请注意,您可以在 https://nodatime.org/tzvalidate/generate - and you can specify a zone and/or IANA database version as well, e.g. https://nodatime.org/tzvalidate/generate?version=2020d&zone=Pacific/Apia 获得所有时区偏移量的完整转换列表,以野田时间结果为准,仅显示使用 2020d 数据库的 Pacific/Apia 结果。