2020goog . provide ( 'goog.i18n.TimeZone' ) ;
2121
2222goog . require ( 'goog.array' ) ;
23+ /** @suppress {extraRequire} goog.date.DateLike represents a Date or a
24+ * goog.Date object. It is a parameter in the following methods:
25+ * - getDaylightAdjustment
26+ * - getGMTString
27+ * - getLongName
28+ * - getOffset
29+ * - getRFCTimeZoneString
30+ * - getShortName
31+ * - isDaylightTime
32+ * - getLongNameGMT
33+ * - getGenericLocation
34+ * Lint warns that this require is unnecessary but the closure compiler needs
35+ * it in order to accept a Date or a goog.Date object as a goog.date.DateLike
36+ * parameter in any of these methods. */
2337goog . require ( 'goog.date.DateLike' ) ;
38+ goog . require ( 'goog.object' ) ;
2439goog . require ( 'goog.string' ) ;
2540
2641
@@ -36,8 +51,8 @@ goog.require('goog.string');
3651 * In case only time zone offset is known, there is a decent fallback
3752 * that only use the time zone offset to create a TimeZone object.
3853 * A whole set of time zone information array was available under
39- * http://go/js_locale_data. It is generated based on CLDR and
40- * Olson time zone data base (through pytz) , and will be updated timely.
54+ * http://go/js_locale_data. It is generated based on CLDR/ICU and
55+ * Olson time zone data base, and will be updated timely.
4156 *
4257 * @constructor
4358 * @final
@@ -70,6 +85,25 @@ goog.i18n.TimeZone = function() {
7085 this . tzNames_ ;
7186
7287
88+ /**
89+ * An object of 2 to 4 elements. The STD_* are always available, while the
90+ * DST_* are only available when daylight saving time is available for this
91+ * time zone.
92+ * <ul>
93+ * <li>STD_LONG_NAME_GMT: long GMT name for standard time</li>
94+ * <li>STD_GENERIC_LOCATION: generic location for standard time</li>
95+ * <li>DST_LONG_NAME_GMT: long GMT for daylight saving time</li>
96+ * <li>DST_GENERIC_LOCATION: generic location for daylight saving time</li>
97+ * </ul>
98+ * @type { { STD_LONG_NAME_GMT:string, STD_GENERIC_LOCATION:string } |
99+ * { STD_LONG_NAME_GMT:string, STD_GENERIC_LOCATION:string,
100+ * DST_LONG_NAME_GMT:string, DST_GENERIC_LOCATION:string }
101+ * }
102+ * @private
103+ */
104+ this . tzNamesExt_ ;
105+
106+
73107 /**
74108 * This array specifies the Daylight Saving Time transitions for this time
75109 * zone. This is a flat array of numbers which are interpreted in pairs:
@@ -118,6 +152,9 @@ goog.i18n.TimeZone.NameType = {
118152 * <li>std_offset: The standard time zone offset in minutes EAST of UTC.
119153 * <li>names: An array of four names (standard short name, standard long
120154 * name, daylight short name, daylight long, name)
155+ * <li>names_ext: A hash of four fields (standard long name gmt, daylight
156+ * long name gmt, standard generic location, daylight generic
157+ * location)
121158 * <li>transitions: An array of numbers which are interpreted in pairs:
122159 * [time1, adjustment1, time2, adjustment2, ...] where each time is
123160 * a DST transition point given as a number of hours since 00:00 UTC,
@@ -135,6 +172,7 @@ goog.i18n.TimeZone.createTimeZone = function(timeZoneData) {
135172 tz . timeZoneId_ = timeZoneData [ 'id' ] ;
136173 tz . standardOffset_ = - timeZoneData [ 'std_offset' ] ;
137174 tz . tzNames_ = timeZoneData [ 'names' ] ;
175+ tz . tzNamesExt_ = timeZoneData [ 'names_ext' ] ;
138176 tz . transitions_ = timeZoneData [ 'transitions' ] ;
139177 return tz ;
140178} ;
@@ -154,7 +192,9 @@ goog.i18n.TimeZone.createSimpleTimeZone_ = function(timeZoneOffsetInMinutes) {
154192 tz . timeZoneId_ =
155193 goog . i18n . TimeZone . composePosixTimeZoneID_ ( timeZoneOffsetInMinutes ) ;
156194 var str = goog . i18n . TimeZone . composeUTCString_ ( timeZoneOffsetInMinutes ) ;
195+ var strGMT = goog . i18n . TimeZone . composeGMTString_ ( timeZoneOffsetInMinutes ) ;
157196 tz . tzNames_ = [ str , str ] ;
197+ tz . tzNamesExt_ = { STD_LONG_NAME_GMT : strGMT , STD_GENERIC_LOCATION : strGMT } ;
158198 tz . transitions_ = [ ] ;
159199 return tz ;
160200} ;
@@ -232,6 +272,7 @@ goog.i18n.TimeZone.prototype.getTimeZoneData = function() {
232272 'id' : this . timeZoneId_ ,
233273 'std_offset' : - this . standardOffset_ , // note createTimeZone flips the sign
234274 'names' : goog . array . clone ( this . tzNames_ ) , // avoid aliasing the array
275+ 'names_ext' : goog . object . clone ( this . tzNamesExt_ ) , // avoid aliasing
235276 'transitions' : goog . array . clone ( this . transitions_ ) // avoid aliasing
236277 } ;
237278} ;
@@ -339,3 +380,41 @@ goog.i18n.TimeZone.prototype.getTimeZoneId = function() {
339380goog . i18n . TimeZone . prototype . isDaylightTime = function ( date ) {
340381 return this . getDaylightAdjustment ( date ) > 0 ;
341382} ;
383+
384+
385+ /**
386+ * Get the long GMT time zone name for a given date/time.
387+ * @param {!goog.date.DateLike } date The time for which to retrieve
388+ * the long GMT time zone name.
389+ * @return {string } The long GMT time zone name.
390+ */
391+ goog . i18n . TimeZone . prototype . getLongNameGMT = function ( date ) {
392+ if ( this . isDaylightTime ( date ) ) {
393+ return ( goog . isDef ( this . tzNamesExt_ . DST_LONG_NAME_GMT ) ) ?
394+ this . tzNamesExt_ . DST_LONG_NAME_GMT :
395+ this . tzNamesExt_ [ 'DST_LONG_NAME_GMT' ] ;
396+ } else {
397+ return ( goog . isDef ( this . tzNamesExt_ . STD_LONG_NAME_GMT ) ) ?
398+ this . tzNamesExt_ . STD_LONG_NAME_GMT :
399+ this . tzNamesExt_ [ 'STD_LONG_NAME_GMT' ] ;
400+ }
401+ } ;
402+
403+
404+ /**
405+ * Get the generic location time zone name for a given date/time.
406+ * @param {!goog.date.DateLike } date The time for which to retrieve
407+ * the generic location time zone name.
408+ * @return {string } The generic location time zone name.
409+ */
410+ goog . i18n . TimeZone . prototype . getGenericLocation = function ( date ) {
411+ if ( this . isDaylightTime ( date ) ) {
412+ return ( goog . isDef ( this . tzNamesExt_ . DST_GENERIC_LOCATION ) ) ?
413+ this . tzNamesExt_ . DST_GENERIC_LOCATION :
414+ this . tzNamesExt_ [ 'DST_GENERIC_LOCATION' ] ;
415+ } else {
416+ return ( goog . isDef ( this . tzNamesExt_ . STD_GENERIC_LOCATION ) ) ?
417+ this . tzNamesExt_ . STD_GENERIC_LOCATION :
418+ this . tzNamesExt_ [ 'STD_GENERIC_LOCATION' ] ;
419+ }
420+ } ;
0 commit comments