[英]NodaTime IANA Timezone offsets not matching with Moment JS
I recently started using NodaTime and I must say it is a fantastic piece of work.我最近开始使用 NodaTime,我必须说这是一项了不起的工作。
However, just to test that I am doing things right, I took IANA timezone names from here and printed UTC Offsets for each using NodaTime.但是,为了测试我做的事情是否正确,我从这里获取了 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())}");
}
Now, for a particular timezone say Pacific/Apia
, offset was printed as -11
,现在,对于特定时区,例如
Pacific/Apia
,偏移量打印为-11
,
but when I tried same on Chrome browser (console) from the momentJS, I got +14
但是当我从momentJS开始在Chrome浏览器(控制台)上尝试相同时,我得到了
+14
moment.tz(new Date().toISOString(), "Pacific/Apia").format()
"2020-12-10T01:33:13+14:00"
Am I missing something?我错过了什么吗?
In the Noda Time code, you're using new Instant()
, which refers to the Unix epoch (1970-01-01T00:00:00Z).在 Noda Time 代码中,您使用的是
new Instant()
,它指的是 Unix 纪元 (1970-01-01T00:00:00Z)。 In the Javascript code you're using new Date()
which uses the current date and time.在 Javascript 代码中,您使用的是使用当前日期和时间的
new Date()
。
Change your Noda Time code to use ob.GetUtcOffset(SystemClock.Instance.GetCurrentInstant())
to make it behave like the Javascript code.更改您的 Noda 时间代码以使用
ob.GetUtcOffset(SystemClock.Instance.GetCurrentInstant())
使其行为类似于 Javascript 代码。
Note that you can get a full list of transitions with offsets for all time zones in terms of Noda Time results at https://nodatime.org/tzvalidate/generate - and you can specify a zone and/or IANA database version as well, eg https://nodatime.org/tzvalidate/generate?version=2020d&zone=Pacific/Apia to just show the Pacific/Apia results using the 2020d database.请注意,您可以在https://nodatime.org/tzvalidate/generate上获得所有时区偏移量的完整转换列表 - 您还可以指定区域和/或 IANA 数据库版本,例如https://nodatime.org/tzvalidate/generate?version=2020d&zone=Pacific/Apia仅显示使用 2020d 数据库的 Pacific/Apia 结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.