Debian Codegraph @

closure-library/closure/goog/i18n/timezone_test.js

raw
1 // Copyright 2006 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 goog.provide('goog.i18n.TimeZoneTest');
16 goog.setTestOnly('goog.i18n.TimeZoneTest');
17
18 goog.require('goog.i18n.TimeZone');
19 goog.require('goog.testing.jsunit');
20
21 // Where could such data be found
22 // In js_i18n_data in http://go/i18n_dir, we have a bunch of files with names
23 // like TimeZoneConstant__<locale>.js
24 // We strongly discourage you to use them directly as those data can make
25 // your client code bloated. You should try to provide this data from server
26 // in a selective manner. In typical scenario, user's time zone is retrieved
27 // and only data for that time zone should be provided.
28 // This piece of data is in JSON format. It requires double quote.
29 var americaLosAngelesData = {
30 'transitions': [
31 2770, 60, 7137, 0, 11506, 60, 16041, 0, 20410, 60, 24777, 0,
32 29146, 60, 33513, 0, 35194, 60, 42249, 0, 45106, 60, 50985, 0,
33 55354, 60, 59889, 0, 64090, 60, 68625, 0, 72994, 60, 77361, 0,
34 81730, 60, 86097, 0, 90466, 60, 94833, 0, 99202, 60, 103569, 0,
35 107938, 60, 112473, 0, 116674, 60, 121209, 0, 125578, 60, 129945, 0,
36 134314, 60, 138681, 0, 143050, 60, 147417, 0, 151282, 60, 156153, 0,
37 160018, 60, 165057, 0, 168754, 60, 173793, 0, 177490, 60, 182529, 0,
38 186394, 60, 191265, 0, 195130, 60, 200001, 0, 203866, 60, 208905, 0,
39 212602, 60, 217641, 0, 221338, 60, 226377, 0, 230242, 60, 235113, 0,
40 238978, 60, 243849, 0, 247714, 60, 252585, 0, 256450, 60, 261489, 0,
41 265186, 60, 270225, 0, 273922, 60, 278961, 0, 282826, 60, 287697, 0,
42 291562, 60, 296433, 0, 300298, 60, 305337, 0, 309034, 60, 314073, 0,
43 317770, 60, 322809, 0, 326002, 60, 331713, 0, 334738, 60, 340449, 0,
44 343474, 60, 349185, 0, 352378, 60, 358089, 0, 361114, 60, 366825, 0,
45 369850, 60, 375561, 0, 378586, 60, 384297, 0, 387322, 60, 393033, 0,
46 396058, 60, 401769, 0, 404962, 60, 410673, 0, 413698, 60, 419409, 0,
47 422434, 60, 428145, 0, 431170, 60, 436881, 0, 439906, 60, 445617, 0,
48 448810, 60, 454521, 0, 457546, 60, 463257, 0, 466282, 60, 471993, 0,
49 475018, 60, 480729, 0, 483754, 60, 489465, 0, 492490, 60, 498201, 0,
50 501394, 60, 507105, 0, 510130, 60, 515841, 0, 518866, 60, 524577, 0,
51 527602, 60, 533313, 0, 536338, 60, 542049, 0, 545242, 60, 550953, 0,
52 553978, 60, 559689, 0, 562714, 60, 568425, 0, 571450, 60, 577161, 0,
53 580186, 60, 585897, 0, 588922, 60, 594633, 0
54 ],
55 'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
56 'names_ext': {
57 STD_LONG_NAME_GMT: 'GMT-08:00',
58 STD_GENERIC_LOCATION: 'Los Angeles Time',
59 DST_LONG_NAME_GMT: 'GMT-07:00',
60 DST_GENERIC_LOCATION: 'Los Angeles Time'
61 },
62 'id': 'America/Los_Angeles',
63 'std_offset': -480
64 };
65
66 var americaLosAngelesDataStringKeys = {
67 'transitions': [
68 2770, 60, 7137, 0, 11506, 60, 16041, 0, 20410, 60, 24777, 0,
69 29146, 60, 33513, 0, 35194, 60, 42249, 0, 45106, 60, 50985, 0,
70 55354, 60, 59889, 0, 64090, 60, 68625, 0, 72994, 60, 77361, 0,
71 81730, 60, 86097, 0, 90466, 60, 94833, 0, 99202, 60, 103569, 0,
72 107938, 60, 112473, 0, 116674, 60, 121209, 0, 125578, 60, 129945, 0,
73 134314, 60, 138681, 0, 143050, 60, 147417, 0, 151282, 60, 156153, 0,
74 160018, 60, 165057, 0, 168754, 60, 173793, 0, 177490, 60, 182529, 0,
75 186394, 60, 191265, 0, 195130, 60, 200001, 0, 203866, 60, 208905, 0,
76 212602, 60, 217641, 0, 221338, 60, 226377, 0, 230242, 60, 235113, 0,
77 238978, 60, 243849, 0, 247714, 60, 252585, 0, 256450, 60, 261489, 0,
78 265186, 60, 270225, 0, 273922, 60, 278961, 0, 282826, 60, 287697, 0,
79 291562, 60, 296433, 0, 300298, 60, 305337, 0, 309034, 60, 314073, 0,
80 317770, 60, 322809, 0, 326002, 60, 331713, 0, 334738, 60, 340449, 0,
81 343474, 60, 349185, 0, 352378, 60, 358089, 0, 361114, 60, 366825, 0,
82 369850, 60, 375561, 0, 378586, 60, 384297, 0, 387322, 60, 393033, 0,
83 396058, 60, 401769, 0, 404962, 60, 410673, 0, 413698, 60, 419409, 0,
84 422434, 60, 428145, 0, 431170, 60, 436881, 0, 439906, 60, 445617, 0,
85 448810, 60, 454521, 0, 457546, 60, 463257, 0, 466282, 60, 471993, 0,
86 475018, 60, 480729, 0, 483754, 60, 489465, 0, 492490, 60, 498201, 0,
87 501394, 60, 507105, 0, 510130, 60, 515841, 0, 518866, 60, 524577, 0,
88 527602, 60, 533313, 0, 536338, 60, 542049, 0, 545242, 60, 550953, 0,
89 553978, 60, 559689, 0, 562714, 60, 568425, 0, 571450, 60, 577161, 0,
90 580186, 60, 585897, 0, 588922, 60, 594633, 0
91 ],
92 'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
93 'names_ext': {
94 'STD_LONG_NAME_GMT': 'GMT-08:00',
95 'STD_GENERIC_LOCATION': 'Los Angeles Time',
96 'DST_LONG_NAME_GMT': 'GMT-07:00',
97 'DST_GENERIC_LOCATION': 'Los Angeles Time'
98 },
99 'id': 'America/Los_Angeles',
100 'std_offset': -480
101 };
102
103 function testIsDaylightTime() {
104 var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
105 var dt = new Date(2007, 7 - 1, 1);
106 assertTrue(usPacific.isDaylightTime(dt));
107 // 2007/03/11 2:00am has daylight change. We set time through UTC so that
108 // this test won't be affected by browser's local time handling.
109 dt = new Date(2007, 3 - 1, 11);
110 // The date above is created with a timezone of the computer it is run on.
111 // Therefore the UTC day has to be set explicitly to be sure that the date
112 // is correct for timezones east of Greenwich where 2007/3/11 0:00 is still
113 // 2007/03/10 in UTC.
114 dt.setUTCDate(11);
115 dt.setUTCHours(2 + 8);
116 dt.setUTCMinutes(1);
117 assertTrue(usPacific.isDaylightTime(dt));
118 dt.setUTCHours(1 + 8);
119 dt.setUTCMinutes(59);
120 assertTrue(!usPacific.isDaylightTime(dt));
121
122 dt = new Date(2007, 11 - 1, 4);
123 // Set the UTC day explicitly to make it work in timezones east of
124 // Greenwich.
125 dt.setUTCDate(4);
126 dt.setUTCHours(2 + 7);
127 dt.setUTCMinutes(1);
128 assertTrue(!usPacific.isDaylightTime(dt));
129
130 // there seems to be a browser bug. local time 1:59am should still be PDT.
131 dt.setUTCHours(0 + 7);
132 dt.setUTCMinutes(59);
133 assertTrue(usPacific.isDaylightTime(dt));
134 }
135
136 function testGetters() {
137 var date = new Date();
138 var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
139 assertEquals('America/Los_Angeles', usPacific.getTimeZoneId());
140 assertObjectEquals(americaLosAngelesData, usPacific.getTimeZoneData());
141 }
142
143 function testNames() {
144 var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
145 var dt = new Date(2007, 7 - 1, 1);
146 assertTrue(usPacific.isDaylightTime(dt));
147 assertEquals('PDT', usPacific.getShortName(dt));
148 assertEquals('Pacific Daylight Time', usPacific.getLongName(dt));
149
150 dt = new Date(2007, 12 - 1, 1);
151 assertTrue(!usPacific.isDaylightTime(dt));
152 assertEquals('PST', usPacific.getShortName(dt));
153 assertEquals('Pacific Standard Time', usPacific.getLongName(dt));
154 }
155
156 function testNamesExt() {
157 var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
158 var dt = new Date(2007, 7 - 1, 1);
159 assertTrue(usPacific.isDaylightTime(dt));
160 assertEquals('GMT-07:00', usPacific.getLongNameGMT(dt));
161 assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
162
163 dt = new Date(2007, 12 - 1, 1);
164 assertTrue(!usPacific.isDaylightTime(dt));
165 assertEquals('GMT-08:00', usPacific.getLongNameGMT(dt));
166 assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
167 }
168
169 function testNamesExtStringKeys() {
170 var usPacific =
171 goog.i18n.TimeZone.createTimeZone(americaLosAngelesDataStringKeys);
172 var dt = new Date(2007, 7 - 1, 1);
173 assertTrue(usPacific.isDaylightTime(dt));
174 assertEquals('GMT-07:00', usPacific.getLongNameGMT(dt));
175 assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
176
177 dt = new Date(2007, 12 - 1, 1);
178 assertTrue(!usPacific.isDaylightTime(dt));
179 assertEquals('GMT-08:00', usPacific.getLongNameGMT(dt));
180 assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
181 }
182
183 function testGeneratedData() {
184 var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
185 var dt = new Date(2007, 7 - 1, 1);
186 assertTrue(usPacific.isDaylightTime(dt));
187 assertEquals(420, usPacific.getOffset(dt));
188 assertEquals('GMT-07:00', usPacific.getGMTString(dt));
189 assertEquals('-0700', usPacific.getRFCTimeZoneString(dt));
190
191 dt = new Date(2007, 12 - 1, 1);
192 assertTrue(!usPacific.isDaylightTime(dt));
193 assertEquals(480, usPacific.getOffset(dt));
194 assertEquals('GMT-08:00', usPacific.getGMTString(dt));
195 assertEquals('-0800', usPacific.getRFCTimeZoneString(dt));
196 }
197
198 function testSimpleTimeZonePositive() {
199 var date = new Date();
200 var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(480);
201 assertEquals(480, simpleTimeZone.getOffset(date));
202 assertEquals('GMT-08:00', simpleTimeZone.getGMTString(date));
203 assertEquals('Etc/GMT+8', simpleTimeZone.getTimeZoneId());
204 assertEquals('UTC-8', simpleTimeZone.getLongName(date));
205 assertEquals('UTC-8', simpleTimeZone.getShortName(date));
206 assertEquals('-0800', simpleTimeZone.getRFCTimeZoneString(date));
207 assertEquals('GMT-08:00', simpleTimeZone.getLongNameGMT(date));
208 assertEquals('GMT-08:00', simpleTimeZone.getGenericLocation(date));
209 assertEquals(false, simpleTimeZone.isDaylightTime(date));
210
211 simpleTimeZone = goog.i18n.TimeZone.createTimeZone(630);
212 assertEquals(630, simpleTimeZone.getOffset(date));
213 assertEquals('GMT-10:30', simpleTimeZone.getGMTString(date));
214 assertEquals('Etc/GMT+10:30', simpleTimeZone.getTimeZoneId());
215 assertEquals('UTC-10:30', simpleTimeZone.getLongName(date));
216 assertEquals('UTC-10:30', simpleTimeZone.getShortName(date));
217 assertEquals('-1030', simpleTimeZone.getRFCTimeZoneString(date));
218 assertEquals('GMT-10:30', simpleTimeZone.getLongNameGMT(date));
219 assertEquals('GMT-10:30', simpleTimeZone.getGenericLocation(date));
220 assertEquals(false, simpleTimeZone.isDaylightTime(date));
221 }
222
223 function testSimpleTimeZoneNegative() {
224 var date = new Date();
225 var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(-480);
226 assertEquals(-480, simpleTimeZone.getOffset(date));
227 assertEquals('GMT+08:00', simpleTimeZone.getGMTString(date));
228 assertEquals('Etc/GMT-8', simpleTimeZone.getTimeZoneId());
229 assertEquals('UTC+8', simpleTimeZone.getLongName(date));
230 assertEquals('UTC+8', simpleTimeZone.getShortName(date));
231 assertEquals('+0800', simpleTimeZone.getRFCTimeZoneString(date));
232 assertEquals('GMT+08:00', simpleTimeZone.getLongNameGMT(date));
233 assertEquals('GMT+08:00', simpleTimeZone.getGenericLocation(date));
234 assertEquals(false, simpleTimeZone.isDaylightTime(date));
235 }
236
237 function testSimpleTimeZoneZero() {
238 var date = new Date();
239 var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(0);
240 assertEquals(0, simpleTimeZone.getOffset(date));
241 assertEquals('GMT+00:00', simpleTimeZone.getGMTString(date));
242 assertEquals('Etc/GMT', simpleTimeZone.getTimeZoneId());
243 assertEquals('UTC', simpleTimeZone.getLongName(date));
244 assertEquals('UTC', simpleTimeZone.getShortName(date));
245 assertEquals('+0000', simpleTimeZone.getRFCTimeZoneString(date));
246 assertEquals('GMT+00:00', simpleTimeZone.getLongNameGMT(date));
247 assertEquals('GMT+00:00', simpleTimeZone.getGenericLocation(date));
248 assertEquals(false, simpleTimeZone.isDaylightTime(date));
249 }