gpt4 book ai didi

javascript - FireFox 是否支持 Date.prototype.toLocaleString() 中的 IANA 时区?

转载 作者:数据小太阳 更新时间:2023-10-29 05:15:18 24 4
gpt4 key购买 nike

在执行以下代码时,我在 FireFox 38.0.1(在撰写本文时全新安装了最新版本)中遇到了一个令人惊讶的异常:

var d = new Date()
var formattingOptions = { timeZone: 'America/New_York', month: '2-digit', day: '2-digit', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
var formattedDate = d.toLocaleString('en-US', formattingOptions);

显然,FireFox 不喜欢我对 formattingOptions.timeZone 的使用,并做出如下响应:RangeError: invalid time zone in DateTimeFormat(): AMERICA/NEW_YORK

FireFox 是否在其日期格式化方法(例如 Date.prototype.toLocaleString、Intl.DateTimeFormat 等)的实现中不支持 IANA 时区?

最佳答案

请注意 ECMA-402使 IANA 时区成为纯粹的可选要求。看来 Firefox 目前还没有选择支持它们。

默认行为是拒绝除 12.1.1.1 中的 UTC 之外的任何 timeZone 值。 :

  1. Let tz be the result of calling the [[Get]] internal method of options with argument "timeZone".
  2. If tz is not undefined, then
    • a. Let tz be ToString(tz).
    • b. Convert tz to upper case as described in 6.1.
      NOTE: If an implementation accepts additional time zone values, as permitted under certain conditions by the Conformance clause, different casing rules apply.
    • c. If tz is not "UTC", then throw a RangeError exception.

但是,如 NOTE 中所述,除 UTC 之外的其他值可能也受支持:

A conforming implementation is permitted to accept additional values, and then have implementation-defined behaviour instead of throwing a RangeError, for the following properties of options arguments:

[...]

The options property timeZone in the DateTimeFormat constructor, provided that the additional acceptable input values are case-insensitive matches of Zone or Link identifiers in the IANA time zone database and are canonicalized to Zone identifiers in the casing used in the database for the timeZone property of the object returned by DateTimeFormat.resolvedOptions, except that "Etc/GMT" shall be canonicalized to "UTC".

如果我们查看 /js/src/builtin/Intl.js 中的 InitializeDateTimeFormat ,我们看到了 ECMA-402 12.1.1.1 中这些步骤的实现:

 // Steps 15-17.
var tz = options.timeZone;
if (tz !== undefined) {
tz = toASCIIUpperCase(ToString(tz));
if (tz !== "UTC")
ThrowRangeError(JSMSG_INVALID_TIME_ZONE, tz);
}

显然,这会拒绝 UTC 以外的任何 timeZone 值,因此我认为我们可以有把握地得出结论,Firefox 的 toLocaleString 尚不支持 IANA时区。

关于javascript - FireFox 是否支持 Date.prototype.toLocaleString() 中的 IANA 时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com