1 /*
2  * Copyright (c) 2011, 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 6936350
27  * @summary Test case for TimeZone.observesDaylightTime()
28  */
29 
30 import java.util.*;
31 import static java.util.GregorianCalendar.*;
32 
33 public class DaylightTimeTest {
34     private static final int ONE_HOUR = 60 * 60 * 1000; // one hour
35     private static final int INTERVAL = 24 * ONE_HOUR;  // one day
36     private static final String[] ZONES = TimeZone.getAvailableIDs();
37     private static int errors = 0;
38 
main(String[] args)39     public static void main(String[] args) {
40 
41         // Test default TimeZone
42         for (String id : ZONES) {
43             TimeZone tz = TimeZone.getTimeZone(id);
44             long now = System.currentTimeMillis();
45             boolean observes = tz.observesDaylightTime();
46             boolean found = findDSTTransition(tz, now);
47             if (observes != found) {
48                 // There's a critical section. If DST ends after the
49                 // System.currentTimeMills() call, there should be
50                 // inconsistency in the determination. Try the same
51                 // thing again to see the inconsistency was due to the
52                 // critical section.
53                 now = System.currentTimeMillis();
54                 observes = tz.observesDaylightTime();
55                 found = findDSTTransition(tz, now);
56                 if (observes != found) {
57                     System.err.printf("%s: observesDaylightTime() should return %s at %d%n",
58                                       tz.getID(), found, now);
59                     errors++;
60                 }
61             }
62         }
63 
64         // Test SimpleTimeZone in which observesDaylightTime() is
65         // equivalent to useDaylightTime().
66         testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "X",
67                                               APRIL, 1, -SUNDAY, 2*ONE_HOUR,
68                                               OCTOBER, -1, SUNDAY, 2*ONE_HOUR,
69                                               1*ONE_HOUR));
70         testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "Y"));
71 
72         if (errors > 0) {
73             throw new RuntimeException("DaylightTimeTest: failed");
74         }
75     }
76 
77     /**
78      * Returns true if it's `now' in DST or there's any
79      * standard-to-daylight transition within 50 years after `now'.
80      */
findDSTTransition(TimeZone tz, long now)81     private static boolean findDSTTransition(TimeZone tz, long now) {
82         GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
83         cal.setTimeInMillis(now);
84         cal.add(YEAR, 50);
85         long end = cal.getTimeInMillis();
86 
87         for (long t = now; t < end; t += INTERVAL) {
88             cal.setTimeInMillis(t);
89             if (cal.get(DST_OFFSET) > 0) {
90                 return true;
91             }
92         }
93         return false;
94     }
95 
testSimpleTimeZone(SimpleTimeZone stz)96     private static void testSimpleTimeZone(SimpleTimeZone stz) {
97         if (stz.useDaylightTime() != stz.observesDaylightTime()) {
98             System.err.printf("Failed: useDaylightTime=%b, observesDaylightTime()=%b%n\t%s%n",
99                               stz.useDaylightTime(),stz.observesDaylightTime(), stz);
100             errors++;
101         }
102     }
103 }
104