Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/DaylightTimeTest.java
38811 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 693635026* @summary Test case for TimeZone.observesDaylightTime()27*/2829import java.util.*;30import static java.util.GregorianCalendar.*;3132public class DaylightTimeTest {33private static final int ONE_HOUR = 60 * 60 * 1000; // one hour34private static final int INTERVAL = 24 * ONE_HOUR; // one day35private static final String[] ZONES = TimeZone.getAvailableIDs();36private static int errors = 0;3738public static void main(String[] args) {3940// Test default TimeZone41for (String id : ZONES) {42TimeZone tz = TimeZone.getTimeZone(id);43long now = System.currentTimeMillis();44boolean observes = tz.observesDaylightTime();45boolean found = findDSTTransition(tz, now);46if (observes != found) {47// There's a critical section. If DST ends after the48// System.currentTimeMills() call, there should be49// inconsistency in the determination. Try the same50// thing again to see the inconsistency was due to the51// critical section.52now = System.currentTimeMillis();53observes = tz.observesDaylightTime();54found = findDSTTransition(tz, now);55if (observes != found) {56System.err.printf("%s: observesDaylightTime() should return %s at %d%n",57tz.getID(), found, now);58errors++;59}60}61}6263// Test SimpleTimeZone in which observesDaylightTime() is64// equivalent to useDaylightTime().65testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "X",66APRIL, 1, -SUNDAY, 2*ONE_HOUR,67OCTOBER, -1, SUNDAY, 2*ONE_HOUR,681*ONE_HOUR));69testSimpleTimeZone(new SimpleTimeZone(-8*ONE_HOUR, "Y"));7071if (errors > 0) {72throw new RuntimeException("DaylightTimeTest: failed");73}74}7576/**77* Returns true if it's `now' in DST or there's any78* standard-to-daylight transition within 50 years after `now'.79*/80private static boolean findDSTTransition(TimeZone tz, long now) {81GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);82cal.setTimeInMillis(now);83cal.add(YEAR, 50);84long end = cal.getTimeInMillis();8586for (long t = now; t < end; t += INTERVAL) {87cal.setTimeInMillis(t);88if (cal.get(DST_OFFSET) > 0) {89return true;90}91}92return false;93}9495private static void testSimpleTimeZone(SimpleTimeZone stz) {96if (stz.useDaylightTime() != stz.observesDaylightTime()) {97System.err.printf("Failed: useDaylightTime=%b, observesDaylightTime()=%b%n\t%s%n",98stz.useDaylightTime(),stz.observesDaylightTime(), stz);99errors++;100}101}102}103104105