| 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 |
/** |
| 16 |
* @fileoverview Functions for detecting user's time zone. |
| 17 |
* This work is based on Charlie Luo and Hong Yan's time zone detection work |
| 18 |
* for CBG. |
| 19 |
*/ |
| 20 |
goog.provide('goog.locale.timeZoneDetection'); |
| 21 |
|
| 22 |
goog.require('goog.locale.TimeZoneFingerprint'); |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Array of time instances for checking the time zone offset. |
| 27 |
* @type {Array<number>} |
| 28 |
* @private |
| 29 |
*/ |
| 30 |
goog.locale.timeZoneDetection.TZ_POKE_POINTS_ = [ |
| 31 |
1109635200, 1128902400, 1130657000, 1143333000, 1143806400, 1145000000, |
| 32 |
1146380000, 1152489600, 1159800000, 1159500000, 1162095000, 1162075000, |
| 33 |
1162105500 |
| 34 |
]; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Calculates time zone fingerprint by poking time zone offsets for 13 |
| 39 |
* preselected time points. |
| 40 |
* See {@link goog.locale.timeZoneDetection.TZ_POKE_POINTS_} |
| 41 |
* @param {Date} date Date for calculating the fingerprint. |
| 42 |
* @return {number} Fingerprint of user's time zone setting. |
| 43 |
*/ |
| 44 |
goog.locale.timeZoneDetection.getFingerprint = function(date) { |
| 45 |
var hash = 0; |
| 46 |
var stdOffset; |
| 47 |
var isComplex = false; |
| 48 |
for (var i = 0; i < goog.locale.timeZoneDetection.TZ_POKE_POINTS_.length; |
| 49 |
i++) { |
| 50 |
date.setTime(goog.locale.timeZoneDetection.TZ_POKE_POINTS_[i] * 1000); |
| 51 |
var offset = date.getTimezoneOffset() / 30 + 48; |
| 52 |
if (i == 0) { |
| 53 |
stdOffset = offset; |
| 54 |
} else if (stdOffset != offset) { |
| 55 |
isComplex = true; |
| 56 |
} |
| 57 |
hash = (hash << 2) ^ offset; |
| 58 |
} |
| 59 |
return isComplex ? hash : /** @type {number} */ (stdOffset); |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Detects browser's time zone setting. If user's country is known, a better |
| 65 |
* time zone choice could be guessed. |
| 66 |
* @param {string=} opt_country Two-letter ISO 3166 country code. |
| 67 |
* @param {Date=} opt_date Date for calculating the fingerprint. Defaults to the |
| 68 |
* current date. |
| 69 |
* @return {string} Time zone ID of best guess. |
| 70 |
*/ |
| 71 |
goog.locale.timeZoneDetection.detectTimeZone = function(opt_country, opt_date) { |
| 72 |
var date = opt_date || new Date(); |
| 73 |
var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date); |
| 74 |
var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint]; |
| 75 |
// Timezones in goog.locale.TimeZoneDetection.TimeZoneMap are in the format |
| 76 |
// US-America/Los_Angeles. Country code needs to be stripped before a |
| 77 |
// timezone is returned. |
| 78 |
if (timeZoneList) { |
| 79 |
if (opt_country) { |
| 80 |
for (var i = 0; i < timeZoneList.length; ++i) { |
| 81 |
if (timeZoneList[i].indexOf(opt_country) == 0) { |
| 82 |
return timeZoneList[i].substring(3); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
return timeZoneList[0].substring(3); |
| 87 |
} |
| 88 |
return ''; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Returns an array of time zones that are consistent with user's platform |
| 94 |
* setting. If user's country is given, only the time zone for that country is |
| 95 |
* returned. |
| 96 |
* @param {string=} opt_country 2 letter ISO 3166 country code. Helps in making |
| 97 |
* a better guess for user's time zone. |
| 98 |
* @param {Date=} opt_date Date for retrieving timezone list. Defaults to the |
| 99 |
* current date. |
| 100 |
* @return {!Array<string>} Array of time zone IDs. |
| 101 |
*/ |
| 102 |
goog.locale.timeZoneDetection.getTimeZoneList = function( |
| 103 |
opt_country, opt_date) { |
| 104 |
var date = opt_date || new Date(); |
| 105 |
var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date); |
| 106 |
var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint]; |
| 107 |
if (!timeZoneList) { |
| 108 |
return []; |
| 109 |
} |
| 110 |
var chosenList = []; |
| 111 |
for (var i = 0; i < timeZoneList.length; i++) { |
| 112 |
if (!opt_country || timeZoneList[i].indexOf(opt_country) == 0) { |
| 113 |
chosenList.push(timeZoneList[i].substring(3)); |
| 114 |
} |
| 115 |
} |
| 116 |
return chosenList; |
| 117 |
}; |