123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- goog.provide('goog.i18n.TimeZone');
- goog.require('goog.array');
- goog.require('goog.date.DateLike');
- goog.require('goog.object');
- goog.require('goog.string');
- goog.i18n.TimeZone = function() {
-
- this.timeZoneId_;
-
- this.standardOffset_;
-
- this.tzNames_;
-
- this.tzNamesExt_;
-
- this.transitions_;
- };
- goog.i18n.TimeZone.MILLISECONDS_PER_HOUR_ = 3600 * 1000;
- goog.i18n.TimeZone.NameType = {
- STD_SHORT_NAME: 0,
- STD_LONG_NAME: 1,
- DLT_SHORT_NAME: 2,
- DLT_LONG_NAME: 3
- };
- goog.i18n.TimeZone.createTimeZone = function(timeZoneData) {
- if (typeof timeZoneData == 'number') {
- return goog.i18n.TimeZone.createSimpleTimeZone_(timeZoneData);
- }
- var tz = new goog.i18n.TimeZone();
- tz.timeZoneId_ = timeZoneData['id'];
- tz.standardOffset_ = -timeZoneData['std_offset'];
- tz.tzNames_ = timeZoneData['names'];
- tz.tzNamesExt_ = timeZoneData['names_ext'];
- tz.transitions_ = timeZoneData['transitions'];
- return tz;
- };
- goog.i18n.TimeZone.createSimpleTimeZone_ = function(timeZoneOffsetInMinutes) {
- var tz = new goog.i18n.TimeZone();
- tz.standardOffset_ = timeZoneOffsetInMinutes;
- tz.timeZoneId_ =
- goog.i18n.TimeZone.composePosixTimeZoneID_(timeZoneOffsetInMinutes);
- var str = goog.i18n.TimeZone.composeUTCString_(timeZoneOffsetInMinutes);
- var strGMT = goog.i18n.TimeZone.composeGMTString_(timeZoneOffsetInMinutes);
- tz.tzNames_ = [str, str];
- tz.tzNamesExt_ = {STD_LONG_NAME_GMT: strGMT, STD_GENERIC_LOCATION: strGMT};
- tz.transitions_ = [];
- return tz;
- };
- goog.i18n.TimeZone.composeGMTString_ = function(offset) {
- var parts = ['GMT'];
- parts.push(offset <= 0 ? '+' : '-');
- offset = Math.abs(offset);
- parts.push(
- goog.string.padNumber(Math.floor(offset / 60) % 100, 2), ':',
- goog.string.padNumber(offset % 60, 2));
- return parts.join('');
- };
- goog.i18n.TimeZone.composePosixTimeZoneID_ = function(offset) {
- if (offset == 0) {
- return 'Etc/GMT';
- }
- var parts = ['Etc/GMT', offset < 0 ? '-' : '+'];
- offset = Math.abs(offset);
- parts.push(Math.floor(offset / 60) % 100);
- offset = offset % 60;
- if (offset != 0) {
- parts.push(':', goog.string.padNumber(offset, 2));
- }
- return parts.join('');
- };
- goog.i18n.TimeZone.composeUTCString_ = function(offset) {
- if (offset == 0) {
- return 'UTC';
- }
- var parts = ['UTC', offset < 0 ? '+' : '-'];
- offset = Math.abs(offset);
- parts.push(Math.floor(offset / 60) % 100);
- offset = offset % 60;
- if (offset != 0) {
- parts.push(':', offset);
- }
- return parts.join('');
- };
- goog.i18n.TimeZone.prototype.getTimeZoneData = function() {
- return {
- 'id': this.timeZoneId_,
- 'std_offset': -this.standardOffset_,
- 'names': goog.array.clone(this.tzNames_),
- 'names_ext': goog.object.clone(this.tzNamesExt_),
- 'transitions': goog.array.clone(this.transitions_)
- };
- };
- goog.i18n.TimeZone.prototype.getDaylightAdjustment = function(date) {
- var timeInMs = Date.UTC(
- date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
- date.getUTCHours(), date.getUTCMinutes());
- var timeInHours = timeInMs / goog.i18n.TimeZone.MILLISECONDS_PER_HOUR_;
- var index = 0;
- while (index < this.transitions_.length &&
- timeInHours >= this.transitions_[index]) {
- index += 2;
- }
- return (index == 0) ? 0 : this.transitions_[index - 1];
- };
- goog.i18n.TimeZone.prototype.getGMTString = function(date) {
- return goog.i18n.TimeZone.composeGMTString_(this.getOffset(date));
- };
- goog.i18n.TimeZone.prototype.getLongName = function(date) {
- return this.tzNames_[this.isDaylightTime(date) ?
- goog.i18n.TimeZone.NameType.DLT_LONG_NAME :
- goog.i18n.TimeZone.NameType.STD_LONG_NAME];
- };
- goog.i18n.TimeZone.prototype.getOffset = function(date) {
- return this.standardOffset_ - this.getDaylightAdjustment(date);
- };
- goog.i18n.TimeZone.prototype.getRFCTimeZoneString = function(date) {
- var offset = -this.getOffset(date);
- var parts = [offset < 0 ? '-' : '+'];
- offset = Math.abs(offset);
- parts.push(
- goog.string.padNumber(Math.floor(offset / 60) % 100, 2),
- goog.string.padNumber(offset % 60, 2));
- return parts.join('');
- };
- goog.i18n.TimeZone.prototype.getShortName = function(date) {
- return this.tzNames_[this.isDaylightTime(date) ?
- goog.i18n.TimeZone.NameType.DLT_SHORT_NAME :
- goog.i18n.TimeZone.NameType.STD_SHORT_NAME];
- };
- goog.i18n.TimeZone.prototype.getTimeZoneId = function() {
- return this.timeZoneId_;
- };
- goog.i18n.TimeZone.prototype.isDaylightTime = function(date) {
- return this.getDaylightAdjustment(date) > 0;
- };
- goog.i18n.TimeZone.prototype.getLongNameGMT = function(date) {
- if (this.isDaylightTime(date)) {
- return (goog.isDef(this.tzNamesExt_.DST_LONG_NAME_GMT)) ?
- this.tzNamesExt_.DST_LONG_NAME_GMT :
- this.tzNamesExt_['DST_LONG_NAME_GMT'];
- } else {
- return (goog.isDef(this.tzNamesExt_.STD_LONG_NAME_GMT)) ?
- this.tzNamesExt_.STD_LONG_NAME_GMT :
- this.tzNamesExt_['STD_LONG_NAME_GMT'];
- }
- };
- goog.i18n.TimeZone.prototype.getGenericLocation = function(date) {
- if (this.isDaylightTime(date)) {
- return (goog.isDef(this.tzNamesExt_.DST_GENERIC_LOCATION)) ?
- this.tzNamesExt_.DST_GENERIC_LOCATION :
- this.tzNamesExt_['DST_GENERIC_LOCATION'];
- } else {
- return (goog.isDef(this.tzNamesExt_.STD_GENERIC_LOCATION)) ?
- this.tzNamesExt_.STD_GENERIC_LOCATION :
- this.tzNamesExt_['STD_GENERIC_LOCATION'];
- }
- };
|