Skip to content

Commit fabd3f5

Browse files
committed
Closure i18n/timezone API update.
Added the LONG_GMT and GENERIC_LOCATION time zone display names: goog.i18n.TimeZone.getLongNameGMT goog.i18n.TimeZone.getGenericLocation Additionally, goog.i18n.DateTimeFormat was updated to accept 'V' in the date formatting pattern which adds the GENERIC_LOCATION. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=99387254
1 parent 686a14e commit fabd3f5

File tree

5 files changed

+241
-11
lines changed

5 files changed

+241
-11
lines changed

closure/goog/deps.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

closure/goog/i18n/datetimeformat.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ goog.i18n.DateTimeFormat.TOKENS_ = [
151151
//quote string
152152
/^\'(?:[^\']|\'\')*\'/,
153153
// pattern chars
154-
/^(?:G+|y+|M+|k+|S+|E+|a+|h+|K+|H+|c+|L+|Q+|d+|m+|s+|v+|w+|z+|Z+)/,
154+
/^(?:G+|y+|M+|k+|S+|E+|a+|h+|K+|H+|c+|L+|Q+|d+|m+|s+|v+|V+|w+|z+|Z+)/,
155155
// and all the other chars
156-
/^[^\'GyMkSEahKHcLQdmsvwzZ]+/ // and all the other chars
156+
/^[^\'GyMkSEahKHcLQdmsvVwzZ]+/ // and all the other chars
157157
];
158158

159159

@@ -786,6 +786,24 @@ goog.i18n.DateTimeFormat.prototype.formatTimeZoneId_ =
786786
};
787787

788788

789+
/**
790+
* Generate localized, location dependent time zone id
791+
* @param {number} count Number of time pattern char repeats, it controls
792+
* how a field should be formatted.
793+
* @param {!goog.date.DateLike} date Whose value being evaluated.
794+
* @param {goog.i18n.TimeZone=} opt_timeZone This holds current time zone info.
795+
* @return {string} GMT timeZone string.
796+
* @private
797+
*/
798+
goog.i18n.DateTimeFormat.prototype.formatTimeZoneLocationId_ =
799+
function(count, date, opt_timeZone) {
800+
opt_timeZone = opt_timeZone ||
801+
goog.i18n.TimeZone.createTimeZone(date.getTimezoneOffset());
802+
return count <= 2 ? opt_timeZone.getTimeZoneId() :
803+
opt_timeZone.getGenericLocation(date);
804+
};
805+
806+
789807
/**
790808
* Formatting one date field.
791809
* @param {string} patternStr The pattern string for the field being formatted.
@@ -819,6 +837,7 @@ goog.i18n.DateTimeFormat.prototype.formatField_ =
819837
case 'm': return this.formatMinutes_(count, dateForTime);
820838
case 's': return this.formatSeconds_(count, dateForTime);
821839
case 'v': return this.formatTimeZoneId_(date, opt_timeZone);
840+
case 'V': return this.formatTimeZoneLocationId_(count, date, opt_timeZone);
822841
case 'w': return this.formatWeekOfYear_(count, dateForTime);
823842
case 'z': return this.formatTimeZone_(count, date, opt_timeZone);
824843
case 'Z': return this.formatTimeZoneRFC_(count, date, opt_timeZone);

closure/goog/i18n/datetimeformat_test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ var americaLosAngelesData = {
108108
571450, 60, 577161, 0, 580186, 60, 585897, 0, 588922, 60, 594633, 0
109109
],
110110
'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
111+
'names_ext': {
112+
STD_LONG_NAME_GMT: 'GMT-08:00',
113+
STD_GENERIC_LOCATION: 'Los Angeles Time',
114+
DST_LONG_NAME_GMT: 'GMT-07:00',
115+
DST_GENERIC_LOCATION: 'Los Angeles Time'
116+
},
111117
'id': 'America/Los_Angeles',
112118
'std_offset': -480
113119
};
@@ -136,6 +142,12 @@ var europeBerlinData = {
136142
589417, 60, 594457, 0],
137143
'names': ['MEZ', 'Mitteleurop\u00e4ische Zeit',
138144
'MESZ', 'Mitteleurop\u00e4ische Sommerzeit'],
145+
'names_ext': {
146+
STD_LONG_NAME_GMT: 'GMT+01:00',
147+
STD_GENERIC_LOCATION: 'Deutschland Zeit',
148+
DST_LONG_NAME_GMT: 'GMT+02:00',
149+
DST_GENERIC_LOCATION: 'Deutschland Zeit'
150+
},
139151
'id': 'Europe/Berlin',
140152
'std_offset': 60
141153
};
@@ -510,6 +522,39 @@ function testMMddyyyyHHmmssvSimpleTimeZone() {
510522
assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
511523
}
512524

525+
function testMMddyyyyHHmmssVCommonTimeZone() {
526+
goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
527+
goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
528+
var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
529+
var timeZone = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
530+
var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss V');
531+
assertEquals('07/27/2006 06:10:10 America/Los_Angeles',
532+
fmt.format(date, timeZone));
533+
fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VV');
534+
assertEquals('07/27/2006 06:10:10 America/Los_Angeles',
535+
fmt.format(date, timeZone));
536+
fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVV');
537+
assertEquals('07/27/2006 06:10:10 Los Angeles Time',
538+
fmt.format(date, timeZone));
539+
fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVVV');
540+
assertEquals('07/27/2006 06:10:10 Los Angeles Time',
541+
fmt.format(date, timeZone));
542+
}
543+
544+
function testMMddyyyyHHmmssVSimpleTimeZone() {
545+
goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;
546+
goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_de;
547+
var date = new Date(Date.UTC(2006, 6, 27, 13, 10, 10));
548+
var timeZone = goog.i18n.TimeZone.createTimeZone(420);
549+
var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss V');
550+
assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
551+
fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VV');
552+
assertEquals('07/27/2006 06:10:10 Etc/GMT+7', fmt.format(date, timeZone));
553+
fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVV');
554+
assertEquals('07/27/2006 06:10:10 GMT-07:00', fmt.format(date, timeZone));
555+
var fmt = new goog.i18n.DateTimeFormat('MM/dd/yyyy HH:mm:ss VVVV');
556+
assertEquals('07/27/2006 06:10:10 GMT-07:00', fmt.format(date, timeZone));
557+
}
513558

514559
function test_yyyyMMddG() {
515560
goog.i18n.DateTimePatterns = goog.i18n.DateTimePatterns_de;

closure/goog/i18n/timezone.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@
2020
goog.provide('goog.i18n.TimeZone');
2121

2222
goog.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. */
2337
goog.require('goog.date.DateLike');
38+
goog.require('goog.object');
2439
goog.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() {
339380
goog.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

Comments
 (0)