Path: blob/trunk/third_party/closure/goog/locale/timezonedetection.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Functions for detecting user's time zone.16* This work is based on Charlie Luo and Hong Yan's time zone detection work17* for CBG.18*/19goog.provide('goog.locale.timeZoneDetection');2021goog.require('goog.locale.TimeZoneFingerprint');222324/**25* Array of time instances for checking the time zone offset.26* @type {Array<number>}27* @private28*/29goog.locale.timeZoneDetection.TZ_POKE_POINTS_ = [301109635200, 1128902400, 1130657000, 1143333000, 1143806400, 1145000000,311146380000, 1152489600, 1159800000, 1159500000, 1162095000, 1162075000,32116210550033];343536/**37* Calculates time zone fingerprint by poking time zone offsets for 1338* preselected time points.39* See {@link goog.locale.timeZoneDetection.TZ_POKE_POINTS_}40* @param {Date} date Date for calculating the fingerprint.41* @return {number} Fingerprint of user's time zone setting.42*/43goog.locale.timeZoneDetection.getFingerprint = function(date) {44var hash = 0;45var stdOffset;46var isComplex = false;47for (var i = 0; i < goog.locale.timeZoneDetection.TZ_POKE_POINTS_.length;48i++) {49date.setTime(goog.locale.timeZoneDetection.TZ_POKE_POINTS_[i] * 1000);50var offset = date.getTimezoneOffset() / 30 + 48;51if (i == 0) {52stdOffset = offset;53} else if (stdOffset != offset) {54isComplex = true;55}56hash = (hash << 2) ^ offset;57}58return isComplex ? hash : /** @type {number} */ (stdOffset);59};606162/**63* Detects browser's time zone setting. If user's country is known, a better64* time zone choice could be guessed.65* @param {string=} opt_country Two-letter ISO 3166 country code.66* @param {Date=} opt_date Date for calculating the fingerprint. Defaults to the67* current date.68* @return {string} Time zone ID of best guess.69*/70goog.locale.timeZoneDetection.detectTimeZone = function(opt_country, opt_date) {71var date = opt_date || new Date();72var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);73var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];74// Timezones in goog.locale.TimeZoneDetection.TimeZoneMap are in the format75// US-America/Los_Angeles. Country code needs to be stripped before a76// timezone is returned.77if (timeZoneList) {78if (opt_country) {79for (var i = 0; i < timeZoneList.length; ++i) {80if (timeZoneList[i].indexOf(opt_country) == 0) {81return timeZoneList[i].substring(3);82}83}84}85return timeZoneList[0].substring(3);86}87return '';88};899091/**92* Returns an array of time zones that are consistent with user's platform93* setting. If user's country is given, only the time zone for that country is94* returned.95* @param {string=} opt_country 2 letter ISO 3166 country code. Helps in making96* a better guess for user's time zone.97* @param {Date=} opt_date Date for retrieving timezone list. Defaults to the98* current date.99* @return {!Array<string>} Array of time zone IDs.100*/101goog.locale.timeZoneDetection.getTimeZoneList = function(102opt_country, opt_date) {103var date = opt_date || new Date();104var fingerprint = goog.locale.timeZoneDetection.getFingerprint(date);105var timeZoneList = goog.locale.TimeZoneFingerprint[fingerprint];106if (!timeZoneList) {107return [];108}109var chosenList = [];110for (var i = 0; i < timeZoneList.length; i++) {111if (!opt_country || timeZoneList[i].indexOf(opt_country) == 0) {112chosenList.push(timeZoneList[i].substring(3));113}114}115return chosenList;116};117118119