timezonedetection.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Functions for detecting user's time zone.
  16. * This work is based on Charlie Luo and Hong Yan's time zone detection work
  17. * for CBG.
  18. */
  19. goog.provide('goog.locale.timeZoneDetection');
  20. goog.require('goog.locale.TimeZoneFingerprint');
  21. /**
  22. * Array of time instances for checking the time zone offset.
  23. * @type {Array<number>}
  24. * @private
  25. */
  26. goog.locale.timeZoneDetection.TZ_POKE_POINTS_ = [
  27. 1109635200, 1128902400, 1130657000, 1143333000, 1143806400, 1145000000,
  28. 1146380000, 1152489600, 1159800000, 1159500000, 1162095000, 1162075000,
  29. 1162105500
  30. ];
  31. /**
  32. * Calculates time zone fingerprint by poking time zone offsets for 13
  33. * preselected time points.
  34. * See {@link goog.locale.timeZoneDetection.TZ_POKE_POINTS_}
  35. * @param {Date} date Date for calculating the fingerprint.
  36. * @return {number} Fingerprint of user's time zone setting.
  37. */
  38. goog.locale.timeZoneDetection.getFingerprint = function(date) {
  39. var hash = 0;
  40. var stdOffset;
  41. var isComplex = false;
  42. for (var i = 0; i < goog.locale.timeZoneDetection.TZ_POKE_POINTS_.length;
  43. i++) {
  44. date.setTime(goog.locale.timeZoneDetection.TZ_POKE_POINTS_[i] * 1000);
  45. var offset = date.getTimezoneOffset() / 30 + 48;
  46. if (i == 0) {
  47. stdOffset = offset;
  48. } else if (stdOffset != offset) {
  49. isComplex = true;
  50. }
  51. hash = (hash << 2) ^ offset;
  52. }
  53. return isComplex ? hash : /** @type {number} */ (stdOffset);
  54. };
  55. /**
  56. * Detects browser's time zone setting. If user's country is known, a better
  57. * time zone choice could be guessed.
  58. * @param {string=} opt_country Two-letter ISO 3166 country code.
  59. * @param {Date=} opt_date Date for calculating the fingerprint. Defaults to the
  60. * current date.
  61. * @return {string} Time zone ID of best guess.
  62. */
  63. goog.locale.timeZoneDetection.detectTimeZone = function(opt_country, opt_date) {
  64. var date = opt_date || new Date();
  65. var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);
  66. var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];
  67. // Timezones in goog.locale.TimeZoneDetection.TimeZoneMap are in the format
  68. // US-America/Los_Angeles. Country code needs to be stripped before a
  69. // timezone is returned.
  70. if (timeZoneList) {
  71. if (opt_country) {
  72. for (var i = 0; i < timeZoneList.length; ++i) {
  73. if (timeZoneList[i].indexOf(opt_country) == 0) {
  74. return timeZoneList[i].substring(3);
  75. }
  76. }
  77. }
  78. return timeZoneList[0].substring(3);
  79. }
  80. return '';
  81. };
  82. /**
  83. * Returns an array of time zones that are consistent with user's platform
  84. * setting. If user's country is given, only the time zone for that country is
  85. * returned.
  86. * @param {string=} opt_country 2 letter ISO 3166 country code. Helps in making
  87. * a better guess for user's time zone.
  88. * @param {Date=} opt_date Date for retrieving timezone list. Defaults to the
  89. * current date.
  90. * @return {!Array<string>} Array of time zone IDs.
  91. */
  92. goog.locale.timeZoneDetection.getTimeZoneList = function(
  93. opt_country, opt_date) {
  94. var date = opt_date || new Date();
  95. var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);
  96. var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];
  97. if (!timeZoneList) {
  98. return [];
  99. }
  100. var chosenList = [];
  101. for (var i = 0; i < timeZoneList.length; i++) {
  102. if (!opt_country || timeZoneList[i].indexOf(opt_country) == 0) {
  103. chosenList.push(timeZoneList[i].substring(3));
  104. }
  105. }
  106. return chosenList;
  107. };