使用Moment或Internationalization API时,正确的时区列表

时间:2018-07-20 05:11:01

标签: javascript php internationalization timezone momentjs

当我使用MomentJS或国际化API时,Chrome中出现了Asia/KatmanduAsia/Calcutta等过时的时区!这些应改为输出为Asia/KathmanduAsia/Kolkata

我该如何解决?

3 个答案:

答案 0 :(得分:1)

在这里,我编写了一个很小的代码进行比较,这可能对您有所帮助。我使用未缩小的数据文件并提取了全部数据。此列表将向您显示新数据和过时数据之间的比较。

$("#div1").load("https://momentjs.com/downloads/moment-timezone-with-data.js", function(responseTxt, statusTxt, xhr) {
  if (statusTxt == "success") {
    //console.log("External content loaded successfully!");
    keepMeUpdated();
  }
  if (statusTxt == "error")
    console.log("Error: " + xhr.status + ": " + xhr.statusText);
});

function keepMeUpdated() {
  var str = $("#div1").html();
  str = str.slice(str.lastIndexOf("loadData"), str.length - 1);
  str = str.slice(0, str.indexOf(')'));
  str = str.replace('loadData(', '');
  $("#div1").html(str);
  compareNow();
}

function compareNow() {
  var str = $("#div1").html();
  var obj = JSON.parse(str);
  var links = obj.links;
  var counter = 0;

  var te = "There are " + obj.zones.length + " timezones available. From these timezones " + links.length + " have recently got updated."
  $("#head").html(te);
  for (var i = 0; i < links.length; i++) {
    var ele = links[i];
    var spl = ele.split('|');
    var isSame = spl[0] == spl[1];
    if (!isSame) {
      counter++;
      var text = '<tr><td>' + counter + "</td><td>" + spl[1] + "</td><td> " + spl[0] + '</td></tr>';
      $("#mm tbody").append(text);
    }
  }
}
@import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css");
#div1 {
  display: none;
}

#head {
  font-size: 18px;
  font-weight: bold;
  margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id='head'></div>


<div>
  <table id='mm' class="table table-striped">
    <thead>
      <th>Sr</th>
      <th>Obsolete Data</th>
      <th>Updated Data</th>
    </thead>
    <tbody>
    </tbody>
  </table>
</div>
<div id='div1'></div>

答案 1 :(得分:0)

我列出了过时的时区,您可以用它们来比较错误的时区和正确的时区,并输出正确的时区。

P.S。实际上,如果您使用的是PHP,那么在进行date_default_timezone_set()时可能会遇到很大的问题。

所以,这是列表:

(左手是错误的,右手是右的)

Africa/Asmera = Africa/Asmara
Africa/Timbuktu = Africa/Abidjan
America/Argentina/ComodRivadavia = America/Argentina/Catamarca
America/Atka = America/Adak
America/Buenos_Aires = America/Argentina/Buenos_Aires
America/Catamarca = America/Argentina/Catamarca
America/Coral_Harbour = America/Atikokan
America/Cordoba = America/Argentina/Cordoba
America/Ensenada = America/Tijuana
America/Fort_Wayne = America/Indiana/Indianapolis
America/Indianapolis = America/Indiana/Indianapolis
America/Jujuy = America/Argentina/Jujuy
America/Knox_IN = America/Indiana/Knox
America/Louisville = America/Kentucky/Louisville
America/Mendoza = America/Argentina/Mendoza
America/Montreal = America/Toronto
America/Porto_Acre = America/Rio_Branco
America/Rosario = America/Argentina/Cordoba
America/Santa_Isabel = America/Tijuana
America/Shiprock = America/Denver
America/Virgin = America/Port_of_Spain
Antarctica/South_Pole = Pacific/Auckland
Asia/Ashkhabad = Asia/Ashgabat
Asia/Calcutta = Asia/Kolkata
Asia/Chongqing = Asia/Shanghai
Asia/Chungking = Asia/Shanghai
Asia/Dacca = Asia/Dhaka
Asia/Harbin = Asia/Shanghai
Asia/Istanbul = Europe/Istanbul
Asia/Kashgar = Asia/Urumqi
Asia/Katmandu = Asia/Kathmandu
Asia/Macao = Asia/Macau
Asia/Rangoon = Asia/Yangon
Asia/Saigon = Asia/Ho_Chi_Minh
Asia/Tel_Aviv = Asia/Jerusalem
Asia/Thimbu = Asia/Thimphu
Asia/Ujung_Pandang = Asia/Makassar
Asia/Ulan_Bator = Asia/Ulaanbaatar
Atlantic/Faeroe = Atlantic/Faroe
Atlantic/Jan_Mayen = Europe/Oslo
Australia/ACT = Australia/Sydney
Australia/Canberra = Australia/Sydney
Australia/LHI = Australia/Lord_Howe
Australia/North = Australia/Darwin
Australia/NSW = Australia/Sydney
Australia/Queensland = Australia/Brisbane
Australia/South = Australia/Adelaide
Australia/Tasmania = Australia/Hobart
Australia/Victoria = Australia/Melbourne
Australia/West = Australia/Perth
Australia/Yancowinna = Australia/Broken_Hill
Brazil/Acre = America/Rio_Branco
Brazil/DeNoronha = America/Noronha
Brazil/East = America/Sao_Paulo
Brazil/West = America/Manaus
Canada/Atlantic = America/Halifax
Canada/Central = America/Winnipeg
Canada/Eastern = America/Toronto
Canada/Mountain = America/Edmonton
Canada/Newfoundland = America/St_Johns
Canada/Pacific = America/Vancouver
Canada/Saskatchewan = America/Regina
Canada/Yukon = America/Whitehorse
Chile/Continental = America/Santiago
Chile/EasterIsland = Pacific/Easter
Cuba = America/Havana
Egypt = Africa/Cairo
Eire = Europe/Dublin
Europe/Belfast = Europe/London
Europe/Tiraspol = Europe/Chisinau
GB = Europe/London
GB-Eire = Europe/London
Hongkong = Asia/Hong_Kong
Iceland = Atlantic/Reykjavik
Iran = Asia/Tehran
Israel = Asia/Jerusalem
Jamaica = America/Jamaica
Japan = Asia/Tokyo
Kwajalein = Pacific/Kwajalein
Libya = Africa/Tripoli
Mexico/BajaNorte = America/Tijuana
Mexico/BajaSur = America/Mazatlan
Mexico/General = America/Mexico_City
Navajo = America/Denver
NZ = Pacific/Auckland
NZ-CHAT = Pacific/Chatham
Pacific/Johnston = Pacific/Honolulu
Pacific/Ponape = Pacific/Pohnpei
Pacific/Samoa = Pacific/Pago_Pago
Pacific/Truk = Pacific/Chuuk
Pacific/Yap =  Pacific/Chuuk
Poland = Europe/Warsaw
Portugal = Europe/Lisbon
PRC = Asia/Shanghai
ROC = Asia/Taipei
ROK = Asia/Seoul
Singapore = Asia/Singapore
Turkey = Europe/Istanbul
US/Alaska = America/Anchorage
US/Aleutian = America/Adak
US/Arizona = America/Phoenix
US/Central = America/Chicago
US/Eastern = America/New_York
US/East-Indiana = America/Indiana/Indianapolis
US/Hawaii = Pacific/Honolulu
US/Indiana-Starke =America/Indiana/Knox
US/Michigan = America/Detroit
US/Mountain = America/Denver
US/Pacific = America/Los_Angeles
US/Pacific-New = America/Los_Angeles
US/Samoa = Pacific/Pago_Pago
W-SU = Europe/Moscow
Zulu = Etc/UTC
UTC = Etc/UTC
UCT = Etc/UCT
Universal = Etc/UTC
GMT = Etc/GMT
GMT+0 = Etc/GMT
GMT0 = Etc/GMT
GMT−0 = Etc/GMT
Greenwich = Etc/GMT
Etc/Greenwich = Etc/GMT
Etc/Universal = Etc/UTC
Etc/Zulu = Etc/UTC
Etc/GMT0 = Etc/GMT
Etc/GMT-0 = Etc/GMT
Etc/GMT+0 = Etc/GMT

此列表可能仍不完整,但对大多数人有帮助。如果您发现更多不正确的内容,请将其注释掉。

答案 2 :(得分:0)

您的问题有点误入歧途,因为tzdb中通常不会出现“已过时”的时区。在 general 中,一旦tzdb引入了标识符,它将继续无限期地支持它-作为ZoneLink

唯一的例外是Canada/East-Saskatchewan,该变量在2017c版本中已从tzdb中删除,因为它是错误的用语,并超过了tzdb维护者设置的14个字符的限制。该区域的所有用法都应更新为America/Regina

您提供的示例和列表表明您想要规范的Zone条目,而不是它们的别名Link条目。尽管两者均有效,并且所有tzdb实现均应正确解析链接,但实际上通常应优先考虑。因此,我可以理解您希望解决所有链接的问题。

也就是说,当前的时区中的数据文件构建器实现在构建数据时不会区分TZDB区域和链接。相反,它为遇到的第一个唯一数据创建自己的区域条目,然后为随后的所有匹配项创建自己的链接条目。这样做的缺点是将tzdb规范区域放在前面和中间(这会影响较旧的浏览器中的moment.tz.guess()之类的API),而且还具有能够链接在数据文件覆盖的期间内相同的区域的优点。

例如,如果查看当前的moment-timezone-with-data-2012-2022.js文件,您会发现Europe/Paris是大多数使用CET / CEST的地方的唯一区域条目。其余的都是返回到此文件中Europe/Paris的链接,即使是那些具有单独的规范tzdb区域的链接,例如Europe/AmsterdamEurope/Berlin等。

这种权衡是力矩时区的原始作者的设计决定,通常不会改变。这样可以使数据文件保持较小的大小。

此外,如果您针对的是现代浏览器(和/或Node.js),我强烈建议改用Luxon。这是Moment团队提供的更新的库,它的优点是无需提供数据文件即可支持时区(因为大多数现代环境都支持Intl API)。

还值得注意的是,Intl API通常是通过ICU实现的,该ICU从TZDB中获取一些数据,而从CLDR中获取一些数据。这两个项目对“规范”的概念略有不同。基本上,CLDR中的规范区域是最先引入的区域,并且即使TZDB决定降级为链接并用新名称替换,它也不会改变。例如,您可能从某些Intl实现中将Asia/Calcutta作为印度的默认时区,因为CLDR认为它是规范的。这是另一个原因,尽管重要的是所有实现都必须等效地支持两种形式的标识符。