Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/TimeZoneTest.java
38821 views
1
/*
2
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4028006 4044013 4096694 4107276 4107570 4112869 4130885 7039469 7126465 7158483
27
* 8008577 8077685 8098547 8133321 8138716 8148446 8151876 8228469
28
* @library /java/text/testlib
29
* @summary test TimeZone
30
*/
31
32
import java.io.*;
33
import java.text.*;
34
import java.util.*;
35
import sun.util.resources.LocaleData;
36
37
public class TimeZoneTest extends IntlTest
38
{
39
static final int millisPerHour = 3600000;
40
41
public static void main(String[] args) throws Exception {
42
new TimeZoneTest().run(args);
43
}
44
45
/**
46
* Bug 4130885
47
* Certain short zone IDs, used since 1.1.x, are incorrect.
48
*
49
* The worst of these is:
50
*
51
* "CAT" (Central African Time) should be GMT+2:00, but instead returns a
52
* zone at GMT-1:00. The zone at GMT-1:00 should be called EGT, CVT, EGST,
53
* or AZOST, depending on which zone is meant, but in no case is it CAT.
54
*
55
* Other wrong zone IDs:
56
*
57
* ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
58
* GMT-5:00. European Central time is abbreviated CEST.
59
*
60
* SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
61
* GMT-11:00. Solomon Island time is SBT.
62
*
63
* NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
64
* Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
65
*
66
* AST (Alaska Standard Time) GMT-9:00. [This has already been noted in
67
* another bug.] It should be "AKST". AST is Atlantic Standard Time,
68
* GMT-4:00.
69
*
70
* PNT (Phoenix Time) GMT-7:00. PNT usually means Pitcairn Time,
71
* GMT-8:30. There is no standard abbreviation for Phoenix time, as distinct
72
* from MST with daylight savings.
73
*
74
* In addition to these problems, a number of zones are FAKE. That is, they
75
* don't match what people use in the real world.
76
*
77
* FAKE zones:
78
*
79
* EET (should be EEST)
80
* ART (should be EET)
81
* MET (should be IRST)
82
* NET (should be AMST)
83
* PLT (should be PKT)
84
* BST (should be BDT)
85
* VST (should be ICT)
86
* CTT (should be CST) +
87
* ACT (should be CST) +
88
* AET (should be EST) +
89
* MIT (should be WST) +
90
* IET (should be EST) +
91
* PRT (should be AST) +
92
* CNT (should be NST)
93
* AGT (should be ARST)
94
* BET (should be EST) +
95
*
96
* + A zone with the correct name already exists and means something
97
* else. E.g., EST usually indicates the US Eastern zone, so it cannot be
98
* used for Brazil (BET).
99
*/
100
public void TestShortZoneIDs() throws Exception {
101
102
ZoneDescriptor[] JDK_116_REFERENCE_LIST = {
103
new ZoneDescriptor("MIT", 780, true),
104
new ZoneDescriptor("HST", -600, false),
105
new ZoneDescriptor("AST", -540, true),
106
new ZoneDescriptor("PST", -480, true),
107
new ZoneDescriptor("PNT", -420, false),
108
new ZoneDescriptor("MST", -420, false),
109
new ZoneDescriptor("CST", -360, true),
110
new ZoneDescriptor("IET", -300, true),
111
new ZoneDescriptor("EST", -300, false),
112
new ZoneDescriptor("PRT", -240, false),
113
new ZoneDescriptor("CNT", -210, true),
114
new ZoneDescriptor("AGT", -180, false),
115
new ZoneDescriptor("BET", -180, false),
116
// new ZoneDescriptor("CAT", -60, false), // Wrong:
117
// As of bug 4130885, fix CAT (Central Africa)
118
new ZoneDescriptor("CAT", 120, false), // Africa/Harare
119
new ZoneDescriptor("GMT", 0, false),
120
new ZoneDescriptor("UTC", 0, false),
121
new ZoneDescriptor("ECT", 60, true),
122
new ZoneDescriptor("ART", 120, false),
123
new ZoneDescriptor("EET", 120, true),
124
new ZoneDescriptor("EAT", 180, false),
125
new ZoneDescriptor("MET", 60, true),
126
new ZoneDescriptor("NET", 240, false),
127
new ZoneDescriptor("PLT", 300, false),
128
new ZoneDescriptor("IST", 330, false),
129
new ZoneDescriptor("BST", 360, false),
130
new ZoneDescriptor("VST", 420, false),
131
new ZoneDescriptor("CTT", 480, false),
132
new ZoneDescriptor("JST", 540, false),
133
new ZoneDescriptor("ACT", 570, false),
134
new ZoneDescriptor("AET", 600, true),
135
new ZoneDescriptor("SST", 660, false),
136
// new ZoneDescriptor("NST", 720, false),
137
// As of bug 4130885, fix NST (New Zealand)
138
new ZoneDescriptor("NST", 720, true), // Pacific/Auckland
139
};
140
141
Map<String, ZoneDescriptor> hash = new HashMap<>();
142
143
String[] ids = TimeZone.getAvailableIDs();
144
for (String id : ids) {
145
if (id.length() == 3) {
146
hash.put(id, new ZoneDescriptor(TimeZone.getTimeZone(id)));
147
}
148
}
149
150
for (int i = 0; i < JDK_116_REFERENCE_LIST.length; ++i) {
151
ZoneDescriptor referenceZone = JDK_116_REFERENCE_LIST[i];
152
ZoneDescriptor currentZone = hash.get(referenceZone.getID());
153
if (referenceZone.equals(currentZone)) {
154
logln("ok " + referenceZone);
155
}
156
else {
157
errln("Fail: Expected " + referenceZone +
158
"; got " + currentZone);
159
}
160
}
161
}
162
163
/**
164
* A descriptor for a zone; used to regress the short zone IDs.
165
*/
166
static class ZoneDescriptor {
167
String id;
168
int offset; // In minutes
169
boolean daylight;
170
171
ZoneDescriptor(TimeZone zone) {
172
this.id = zone.getID();
173
this.offset = zone.getRawOffset() / 60000;
174
this.daylight = zone.useDaylightTime();
175
}
176
177
ZoneDescriptor(String id, int offset, boolean daylight) {
178
this.id = id;
179
this.offset = offset;
180
this.daylight = daylight;
181
}
182
183
public String getID() { return id; }
184
185
@Override
186
public boolean equals(Object o) {
187
ZoneDescriptor that = (ZoneDescriptor)o;
188
return that != null &&
189
id.equals(that.id) &&
190
offset == that.offset &&
191
daylight == that.daylight;
192
}
193
194
@Override
195
public int hashCode() {
196
return id.hashCode() ^ offset | (daylight ? 1 : 0);
197
}
198
199
@Override
200
public String toString() {
201
int min = offset;
202
char sign = '+';
203
if (min < 0) { sign = '-'; min = -min; }
204
205
return "Zone[\"" + id + "\", GMT" + sign + (min/60) + ':' +
206
(min%60<10?"0":"") + (min%60) + ", " +
207
(daylight ? "Daylight" : "Standard") + "]";
208
}
209
210
public static int compare(Object o1, Object o2) {
211
ZoneDescriptor i1 = (ZoneDescriptor)o1;
212
ZoneDescriptor i2 = (ZoneDescriptor)o2;
213
if (i1.offset > i2.offset) return 1;
214
if (i1.offset < i2.offset) return -1;
215
if (i1.daylight && !i2.daylight) return 1;
216
if (!i1.daylight && i2.daylight) return -1;
217
return i1.id.compareTo(i2.id);
218
}
219
}
220
221
static final String formatMinutes(int min) {
222
char sign = '+';
223
if (min < 0) { sign = '-'; min = -min; }
224
int h = min/60;
225
min = min%60;
226
return "" + sign + h + ":" + ((min<10) ? "0" : "") + min;
227
}
228
/**
229
* As part of the VM fix (see CCC approved RFE 4028006, bug
230
* 4044013), TimeZone.getTimeZone() has been modified to recognize
231
* generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
232
* GMT[+-]hh. Test this behavior here.
233
*
234
* Bug 4044013
235
*
236
* ID "Custom" is no longer used for TimeZone objects created with
237
* a custom time zone ID, such as "GMT-8". See 4322313.
238
*/
239
public void TestCustomParse() throws Exception {
240
Object[] DATA = {
241
// ID Expected offset in minutes
242
"GMT", null,
243
"GMT+0", new Integer(0),
244
"GMT+1", new Integer(60),
245
"GMT-0030", new Integer(-30),
246
"GMT+15:99", null,
247
"GMT+", null,
248
"GMT-", null,
249
"GMT+0:", null,
250
"GMT-:", null,
251
"GMT+0010", new Integer(10), // Interpret this as 00:10
252
"GMT-10", new Integer(-10*60),
253
"GMT+30", null,
254
"GMT-3:30", new Integer(-(3*60+30)),
255
"GMT-230", new Integer(-(2*60+30)),
256
};
257
for (int i=0; i<DATA.length; i+=2) {
258
String id = (String)DATA[i];
259
Integer exp = (Integer)DATA[i+1];
260
TimeZone zone = TimeZone.getTimeZone(id);
261
if (zone.getID().equals("GMT")) {
262
logln(id + " -> generic GMT");
263
// When TimeZone.getTimeZone() can't parse the id, it
264
// returns GMT -- a dubious practice, but required for
265
// backward compatibility.
266
if (exp != null) {
267
throw new Exception("Expected offset of " + formatMinutes(exp.intValue()) +
268
" for " + id + ", got parse failure");
269
}
270
}
271
else {
272
int ioffset = zone.getRawOffset()/60000;
273
String offset = formatMinutes(ioffset);
274
logln(id + " -> " + zone.getID() + " GMT" + offset);
275
if (exp == null) {
276
throw new Exception("Expected parse failure for " + id +
277
", got offset of " + offset +
278
", id " + zone.getID());
279
}
280
else if (ioffset != exp.intValue()) {
281
throw new Exception("Expected offset of " + formatMinutes(exp.intValue()) +
282
", id Custom, for " + id +
283
", got offset of " + offset +
284
", id " + zone.getID());
285
}
286
}
287
}
288
}
289
290
/**
291
* Test the basic functionality of the getDisplayName() API.
292
*
293
* Bug 4112869
294
* Bug 4028006
295
*
296
* See also API change request A41.
297
*
298
* 4/21/98 - make smarter, so the test works if the ext resources
299
* are present or not.
300
*/
301
public void TestDisplayName() {
302
TimeZone zone = TimeZone.getTimeZone("PST");
303
String name = zone.getDisplayName(Locale.ENGLISH);
304
logln("PST->" + name);
305
if (!name.equals("Pacific Standard Time"))
306
errln("Fail: Expected \"Pacific Standard Time\"");
307
308
//*****************************************************************
309
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
310
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
311
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
312
//*****************************************************************
313
Object[] DATA = {
314
new Boolean(false), new Integer(TimeZone.SHORT), "PST",
315
new Boolean(true), new Integer(TimeZone.SHORT), "PDT",
316
new Boolean(false), new Integer(TimeZone.LONG), "Pacific Standard Time",
317
new Boolean(true), new Integer(TimeZone.LONG), "Pacific Daylight Time",
318
};
319
320
for (int i=0; i<DATA.length; i+=3) {
321
name = zone.getDisplayName(((Boolean)DATA[i]).booleanValue(),
322
((Integer)DATA[i+1]).intValue(),
323
Locale.ENGLISH);
324
if (!name.equals(DATA[i+2]))
325
errln("Fail: Expected " + DATA[i+2] + "; got " + name);
326
}
327
328
// Make sure that we don't display the DST name by constructing a fake
329
// PST zone that has DST all year long.
330
SimpleTimeZone zone2 = new SimpleTimeZone(0, "PST");
331
zone2.setStartRule(Calendar.JANUARY, 1, 0);
332
zone2.setEndRule(Calendar.DECEMBER, 31, 0);
333
logln("Modified PST inDaylightTime->" + zone2.inDaylightTime(new Date()));
334
name = zone2.getDisplayName(Locale.ENGLISH);
335
logln("Modified PST->" + name);
336
if (!name.equals("Pacific Standard Time"))
337
errln("Fail: Expected \"Pacific Standard Time\"");
338
339
// Make sure we get the default display format for Locales
340
// with no display name data.
341
Locale zh_CN = Locale.SIMPLIFIED_CHINESE;
342
name = zone.getDisplayName(zh_CN);
343
//*****************************************************************
344
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
345
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
346
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
347
//*****************************************************************
348
logln("PST(zh_CN)->" + name);
349
350
// Now be smart -- check to see if zh resource is even present.
351
// If not, we expect the en fallback behavior.
352
ResourceBundle enRB = LocaleData.getBundle("sun.util.resources.TimeZoneNames",
353
Locale.ENGLISH);
354
ResourceBundle zhRB = LocaleData.getBundle("sun.util.resources.TimeZoneNames",
355
zh_CN);
356
357
boolean noZH = enRB == zhRB;
358
359
if (noZH) {
360
logln("Warning: Not testing the zh_CN behavior because resource is absent");
361
if (!name.equals("Pacific Standard Time"))
362
errln("Fail: Expected Pacific Standard Time");
363
}
364
else if (!name.equals("Pacific Standard Time") &&
365
!name.equals("\u592a\u5e73\u6d0b\u6807\u51c6\u65f6\u95f4") &&
366
!name.equals("GMT-08:00") &&
367
!name.equals("GMT-8:00") &&
368
!name.equals("GMT-0800") &&
369
!name.equals("GMT-800")) {
370
errln("Fail: Expected GMT-08:00 or something similar");
371
errln("************************************************************");
372
errln("THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED");
373
errln("************************************************************");
374
}
375
376
// Now try a non-existent zone
377
zone2 = new SimpleTimeZone(90*60*1000, "xyzzy");
378
name = zone2.getDisplayName(Locale.ENGLISH);
379
logln("GMT+90min->" + name);
380
if (!name.equals("GMT+01:30") &&
381
!name.equals("GMT+1:30") &&
382
!name.equals("GMT+0130") &&
383
!name.equals("GMT+130"))
384
errln("Fail: Expected GMT+01:30 or something similar");
385
}
386
387
public void TestGenericAPI() {
388
String id = "NewGMT";
389
int offset = 12345;
390
391
SimpleTimeZone zone = new SimpleTimeZone(offset, id);
392
if (zone.useDaylightTime()) {
393
errln("FAIL: useDaylightTime should return false");
394
}
395
396
TimeZone zoneclone = (TimeZone)zone.clone();
397
if (!zoneclone.equals(zone)) {
398
errln("FAIL: clone or operator== failed");
399
}
400
zoneclone.setID("abc");
401
if (zoneclone.equals(zone)) {
402
errln("FAIL: clone or operator!= failed");
403
}
404
405
zoneclone = (TimeZone)zone.clone();
406
if (!zoneclone.equals(zone)) {
407
errln("FAIL: clone or operator== failed");
408
}
409
zoneclone.setRawOffset(45678);
410
if (zoneclone.equals(zone)) {
411
errln("FAIL: clone or operator!= failed");
412
}
413
414
TimeZone saveDefault = TimeZone.getDefault();
415
try {
416
TimeZone.setDefault(zone);
417
TimeZone defaultzone = TimeZone.getDefault();
418
if (defaultzone == zone) {
419
errln("FAIL: Default object is identical, not clone");
420
}
421
if (!defaultzone.equals(zone)) {
422
errln("FAIL: Default object is not equal");
423
}
424
}
425
finally {
426
TimeZone.setDefault(saveDefault);
427
}
428
}
429
430
@SuppressWarnings("deprecation")
431
public void TestRuleAPI()
432
{
433
// ErrorCode status = ZERO_ERROR;
434
435
int offset = (int)(60*60*1000*1.75); // Pick a weird offset
436
SimpleTimeZone zone = new SimpleTimeZone(offset, "TestZone");
437
if (zone.useDaylightTime()) errln("FAIL: useDaylightTime should return false");
438
439
// Establish our expected transition times. Do this with a non-DST
440
// calendar with the (above) declared local offset.
441
GregorianCalendar gc = new GregorianCalendar(zone);
442
gc.clear();
443
gc.set(1990, Calendar.MARCH, 1);
444
long marchOneStd = gc.getTime().getTime(); // Local Std time midnight
445
gc.clear();
446
gc.set(1990, Calendar.JULY, 1);
447
long julyOneStd = gc.getTime().getTime(); // Local Std time midnight
448
449
// Starting and ending hours, WALL TIME
450
int startHour = (int)(2.25 * 3600000);
451
int endHour = (int)(3.5 * 3600000);
452
453
zone.setStartRule(Calendar.MARCH, 1, 0, startHour);
454
zone.setEndRule (Calendar.JULY, 1, 0, endHour);
455
456
gc = new GregorianCalendar(zone);
457
// if (failure(status, "new GregorianCalendar")) return;
458
459
long marchOne = marchOneStd + startHour;
460
long julyOne = julyOneStd + endHour - 3600000; // Adjust from wall to Std time
461
462
long expMarchOne = 636251400000L;
463
if (marchOne != expMarchOne)
464
{
465
errln("FAIL: Expected start computed as " + marchOne +
466
" = " + new Date(marchOne));
467
logln(" Should be " + expMarchOne +
468
" = " + new Date(expMarchOne));
469
}
470
471
long expJulyOne = 646793100000L;
472
if (julyOne != expJulyOne)
473
{
474
errln("FAIL: Expected start computed as " + julyOne +
475
" = " + new Date(julyOne));
476
logln(" Should be " + expJulyOne +
477
" = " + new Date(expJulyOne));
478
}
479
480
testUsingBinarySearch(zone, new Date(90, Calendar.JANUARY, 1).getTime(),
481
new Date(90, Calendar.JUNE, 15).getTime(), marchOne);
482
testUsingBinarySearch(zone, new Date(90, Calendar.JUNE, 1).getTime(),
483
new Date(90, Calendar.DECEMBER, 31).getTime(), julyOne);
484
485
if (zone.inDaylightTime(new Date(marchOne - 1000)) ||
486
!zone.inDaylightTime(new Date(marchOne)))
487
errln("FAIL: Start rule broken");
488
if (!zone.inDaylightTime(new Date(julyOne - 1000)) ||
489
zone.inDaylightTime(new Date(julyOne)))
490
errln("FAIL: End rule broken");
491
492
zone.setStartYear(1991);
493
if (zone.inDaylightTime(new Date(marchOne)) ||
494
zone.inDaylightTime(new Date(julyOne - 1000)))
495
errln("FAIL: Start year broken");
496
497
// failure(status, "TestRuleAPI");
498
// delete gc;
499
// delete zone;
500
}
501
502
void testUsingBinarySearch(SimpleTimeZone tz, long min, long max, long expectedBoundary)
503
{
504
// ErrorCode status = ZERO_ERROR;
505
boolean startsInDST = tz.inDaylightTime(new Date(min));
506
// if (failure(status, "SimpleTimeZone::inDaylightTime")) return;
507
if (tz.inDaylightTime(new Date(max)) == startsInDST) {
508
logln("Error: inDaylightTime(" + new Date(max) + ") != " + (!startsInDST));
509
return;
510
}
511
// if (failure(status, "SimpleTimeZone::inDaylightTime")) return;
512
while ((max - min) > INTERVAL) {
513
long mid = (min + max) / 2;
514
if (tz.inDaylightTime(new Date(mid)) == startsInDST) {
515
min = mid;
516
}
517
else {
518
max = mid;
519
}
520
// if (failure(status, "SimpleTimeZone::inDaylightTime")) return;
521
}
522
logln("Binary Search Before: " + min + " = " + new Date(min));
523
logln("Binary Search After: " + max + " = " + new Date(max));
524
long mindelta = expectedBoundary - min;
525
long maxdelta = max - expectedBoundary;
526
if (mindelta >= 0 &&
527
mindelta <= INTERVAL &&
528
mindelta >= 0 &&
529
mindelta <= INTERVAL)
530
logln("PASS: Expected bdry: " + expectedBoundary + " = " + new Date(expectedBoundary));
531
else
532
errln("FAIL: Expected bdry: " + expectedBoundary + " = " + new Date(expectedBoundary));
533
}
534
535
static final int INTERVAL = 100;
536
537
// Bug 006; verify the offset for a specific zone.
538
public void TestPRTOffset()
539
{
540
TimeZone tz = TimeZone.getTimeZone( "PRT" );
541
if( tz == null ) {
542
errln( "FAIL: TimeZone(PRT) is null" );
543
}
544
else{
545
if (tz.getRawOffset() != (-4*millisPerHour))
546
errln("FAIL: Offset for PRT should be -4");
547
}
548
549
}
550
551
// Test various calls
552
@SuppressWarnings("deprecation")
553
public void TestVariousAPI518()
554
{
555
TimeZone time_zone = TimeZone.getTimeZone("PST");
556
Date d = new Date(97, Calendar.APRIL, 30);
557
558
logln("The timezone is " + time_zone.getID());
559
560
if (time_zone.inDaylightTime(d) != true)
561
errln("FAIL: inDaylightTime returned false");
562
563
if (time_zone.useDaylightTime() != true)
564
errln("FAIL: useDaylightTime returned false");
565
566
if (time_zone.getRawOffset() != -8*millisPerHour)
567
errln( "FAIL: getRawOffset returned wrong value");
568
569
GregorianCalendar gc = new GregorianCalendar();
570
gc.setTime(d);
571
if (time_zone.getOffset(gc.AD, gc.get(gc.YEAR), gc.get(gc.MONTH),
572
gc.get(gc.DAY_OF_MONTH),
573
gc.get(gc.DAY_OF_WEEK), 0)
574
!= -7*millisPerHour)
575
errln("FAIL: getOffset returned wrong value");
576
}
577
578
// Test getAvailableID API
579
public void TestGetAvailableIDs913()
580
{
581
StringBuffer buf = new StringBuffer("TimeZone.getAvailableIDs() = { ");
582
String[] s = TimeZone.getAvailableIDs();
583
for (int i=0; i<s.length; ++i)
584
{
585
if (i > 0) buf.append(", ");
586
buf.append(s[i]);
587
}
588
buf.append(" };");
589
logln(buf.toString());
590
591
buf.setLength(0);
592
buf.append("TimeZone.getAvailableIDs(GMT+02:00) = { ");
593
s = TimeZone.getAvailableIDs(+2 * 60 * 60 * 1000);
594
for (int i=0; i<s.length; ++i)
595
{
596
if (i > 0) buf.append(", ");
597
buf.append(s[i]);
598
}
599
buf.append(" };");
600
logln(buf.toString());
601
602
TimeZone tz = TimeZone.getTimeZone("PST");
603
if (tz != null)
604
logln("getTimeZone(PST) = " + tz.getID());
605
else
606
errln("FAIL: getTimeZone(PST) = null");
607
608
tz = TimeZone.getTimeZone("America/Los_Angeles");
609
if (tz != null)
610
logln("getTimeZone(America/Los_Angeles) = " + tz.getID());
611
else
612
errln("FAIL: getTimeZone(PST) = null");
613
614
// Bug 4096694
615
tz = TimeZone.getTimeZone("NON_EXISTENT");
616
if (tz == null)
617
errln("FAIL: getTimeZone(NON_EXISTENT) = null");
618
else if (!tz.getID().equals("GMT"))
619
errln("FAIL: getTimeZone(NON_EXISTENT) = " + tz.getID());
620
}
621
622
/**
623
* Bug 4107276
624
*/
625
public void TestDSTSavings() {
626
// It might be better to find a way to integrate this test into the main TimeZone
627
// tests above, but I don't have time to figure out how to do this (or if it's
628
// even really a good idea). Let's consider that a future. --rtg 1/27/98
629
SimpleTimeZone tz = new SimpleTimeZone(-5 * millisPerHour, "dstSavingsTest",
630
Calendar.MARCH, 1, 0, 0, Calendar.SEPTEMBER, 1, 0, 0,
631
(int)(0.5 * millisPerHour));
632
633
if (tz.getRawOffset() != -5 * millisPerHour)
634
errln("Got back a raw offset of " + (tz.getRawOffset() / millisPerHour) +
635
" hours instead of -5 hours.");
636
if (!tz.useDaylightTime())
637
errln("Test time zone should use DST but claims it doesn't.");
638
if (tz.getDSTSavings() != 0.5 * millisPerHour)
639
errln("Set DST offset to 0.5 hour, but got back " + (tz.getDSTSavings() /
640
millisPerHour) + " hours instead.");
641
642
int offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JANUARY, 1,
643
Calendar.THURSDAY, 10 * millisPerHour);
644
if (offset != -5 * millisPerHour)
645
errln("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got "
646
+ (offset / millisPerHour) + " hours.");
647
648
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 1, Calendar.MONDAY,
649
10 * millisPerHour);
650
if (offset != -4.5 * millisPerHour)
651
errln("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got "
652
+ (offset / millisPerHour) + " hours.");
653
654
tz.setDSTSavings(millisPerHour);
655
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JANUARY, 1,
656
Calendar.THURSDAY, 10 * millisPerHour);
657
if (offset != -5 * millisPerHour)
658
errln("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got "
659
+ (offset / millisPerHour) + " hours.");
660
661
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 1, Calendar.MONDAY,
662
10 * millisPerHour);
663
if (offset != -4 * millisPerHour)
664
errln("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got "
665
+ (offset / millisPerHour) + " hours.");
666
}
667
668
/**
669
* Bug 4107570
670
*/
671
public void TestAlternateRules() {
672
// Like TestDSTSavings, this test should probably be integrated somehow with the main
673
// test at the top of this class, but I didn't have time to figure out how to do that.
674
// --rtg 1/28/98
675
676
SimpleTimeZone tz = new SimpleTimeZone(-5 * millisPerHour, "alternateRuleTest");
677
678
// test the day-of-month API
679
tz.setStartRule(Calendar.MARCH, 10, 12 * millisPerHour);
680
tz.setEndRule(Calendar.OCTOBER, 20, 12 * millisPerHour);
681
682
int offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 5,
683
Calendar.THURSDAY, 10 * millisPerHour);
684
if (offset != -5 * millisPerHour)
685
errln("The offset for 10AM, 3/5/98 should have been -5 hours, but we got "
686
+ (offset / millisPerHour) + " hours.");
687
688
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 15,
689
Calendar.SUNDAY, 10 * millisPerHour);
690
if (offset != -4 * millisPerHour)
691
errln("The offset for 10AM, 3/15/98 should have been -4 hours, but we got "
692
+ (offset / millisPerHour) + " hours.");
693
694
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 15,
695
Calendar.THURSDAY, 10 * millisPerHour);
696
if (offset != -4 * millisPerHour)
697
errln("The offset for 10AM, 10/15/98 should have been -4 hours, but we got "
698
+ (offset / millisPerHour) + " hours.");
699
700
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 25,
701
Calendar.SUNDAY, 10 * millisPerHour);
702
if (offset != -5 * millisPerHour)
703
errln("The offset for 10AM, 10/25/98 should have been -5 hours, but we got "
704
+ (offset / millisPerHour) + " hours.");
705
706
// test the day-of-week-after-day-in-month API
707
tz.setStartRule(Calendar.MARCH, 10, Calendar.FRIDAY, 12 * millisPerHour, true);
708
tz.setEndRule(Calendar.OCTOBER, 20, Calendar.FRIDAY, 12 * millisPerHour, false);
709
710
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 11,
711
Calendar.WEDNESDAY, 10 * millisPerHour);
712
if (offset != -5 * millisPerHour)
713
errln("The offset for 10AM, 3/11/98 should have been -5 hours, but we got "
714
+ (offset / millisPerHour) + " hours.");
715
716
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.MARCH, 14,
717
Calendar.SATURDAY, 10 * millisPerHour);
718
if (offset != -4 * millisPerHour)
719
errln("The offset for 10AM, 3/14/98 should have been -4 hours, but we got "
720
+ (offset / millisPerHour) + " hours.");
721
722
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 15,
723
Calendar.THURSDAY, 10 * millisPerHour);
724
if (offset != -4 * millisPerHour)
725
errln("The offset for 10AM, 10/15/98 should have been -4 hours, but we got "
726
+ (offset / millisPerHour) + " hours.");
727
728
offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.OCTOBER, 17,
729
Calendar.SATURDAY, 10 * millisPerHour);
730
if (offset != -5 * millisPerHour)
731
errln("The offset for 10AM, 10/17/98 should have been -5 hours, but we got "
732
+ (offset / millisPerHour) + " hours.");
733
}
734
}
735
736
//eof
737
738