为 jsTimezoneDetect js 库获取不同的时区 IE11 和 Chrome 浏览器

Getting different timezone IE11 and Chrome browser for jsTimezoneDetect js library

如果我们在系统中 select '(UTC +01:00) Brussels, Copenhagen, Madrid, Paris',那么两种浏览器都会得到不同的输出。

var tz = jstz.determine();
var tzName = tz.name();

输出:

IE11:Europe/Berlin
Chrome: Europe/Paris

来自 jsTimeZoneDetect 文档:

Limitations
This script does not do geo-location, nor does it care very much about historical time zones. So if you are unhappy with the time zone "Europe/Berlin" when the user is in fact in "Europe/Stockholm" - this script is not for you. They are both identical in modern time.

确实,如果我们仔细检查和比较 Berlin and Paris 的时区变化历史,我们会发现它们自 1980 年以来一直是相同的。因此,除非您的应用程序处理的是 1980 年之前的日期,否则无论您检测到 Europe/Berlin 还是 Europe/Paris.

都无关紧要

至于哪个更正确,CLDR windowsZones.xml 文件(Windows 和 IANA 时区之间的规范映射)contains the following:

<!-- (UTC+01:00) Brussels, Copenhagen, Madrid, Paris -->
<mapZone other="Romance Standard Time" territory="001" type="Europe/Paris"/>

Chrome 使用 Intl API,它在内部使用包含 CLDR 数据的 ICU。因此,Chrome 提供了更正确的答案。您应该在 FireFox、Edge 和其他现代网络浏览器中得到相同的答案。

Internet Explorer 较旧,不包含正确解决此问题所需的数据。因此,像 jsTimeZoneDetect(以及通过 moment.tz.guess() 的时刻时区)这样的库首先尝试使用 Intl 方法,但是当不可用时,他们通过测试各种已知时间点的 UTC 偏移量来做出有根据的猜测变化。由于只是猜测,所以有时会不准确。

如果您有兴趣,可以使用社区维护的 compatibility chart 来跟踪哪些浏览器支持“正确”时区检测过程。展开“DateTimeFormat”部分并检查标记为 “resolvedOptions().timeZone defaults to the host environment”的行