Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/TransitionTest.java
38821 views
/*1* Copyright (c) 2002, 2016, 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 4278609 476169626* @library /java/text/testlib27* @summary Make sure to handle DST transition ending at 0:00 January 1.28*/2930import java.text.SimpleDateFormat;31import java.util.Calendar;32import java.util.Date;33import java.util.GregorianCalendar;34import java.util.Locale;35import java.util.SimpleTimeZone;36import java.util.TimeZone;3738public class TransitionTest extends IntlTest {3940public static void main(String[] args) throws Exception {41new TransitionTest().run(args);42}4344public void Test4278609() {45SimpleTimeZone tz = new SimpleTimeZone(0, "MyTimeZone",46/* DST start day: August, 1, 0:00 */47Calendar.AUGUST, 1, 0, 0,48/* DST end day: January, 1, 0:00 (wall-clock)*/49Calendar.JANUARY, 1, 0, 0,5060 * 60 * 1000);5152Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));5354// setting a date using GMT zone just after the end rule of tz zone55cal.clear();56cal.set(Calendar.ERA, GregorianCalendar.AD);57cal.set(1998, Calendar.DECEMBER, 31, 23, 01, 00);5859Date date = cal.getTime();6061int millis = cal.get(Calendar.HOUR_OF_DAY) * 360000062+ cal.get(Calendar.MINUTE) * 6000063+ cal.get(Calendar.SECOND) * 100064+ cal.get(Calendar.MILLISECOND);65/* we must use standard local time */66millis += tz.getRawOffset();6768int offset = tz.getOffset(cal.get(Calendar.ERA),69cal.get(Calendar.YEAR),70cal.get(Calendar.MONTH),71cal.get(Calendar.DATE),72cal.get(Calendar.DAY_OF_WEEK),73millis);7475if (offset != 0) {76SimpleDateFormat format = new SimpleDateFormat("dd MMM HH:mm:ss zzz",77Locale.US);78format.setTimeZone(tz);79errln("Wrong DST transition: " + tz80+ "\na date just after DST = " + format.format(date)81+ "\ngetOffset = " + offset);82}83}8485/*86* 4761696: Rewrite SimpleTimeZone to support correct DST transitions87*88* Derived from JCK test cases some of which specify wrong day of week values.89*/90public void Test4761696() {91GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));9293// test#194int rawOffset = -43200000;95int saving = 1800000;96int timeOfDay = 84600001;97SimpleTimeZone tz = new SimpleTimeZone(rawOffset, "stz",98Calendar.JULY, 1, 0, 0,99Calendar.JANUARY, 1, 0, 0,100saving);101int year = Integer.MIN_VALUE;102tz.setStartYear(year);103int offset = tz.getOffset(GregorianCalendar.AD,104year,105Calendar.DECEMBER,10631,1071, // should be SATURDAY108timeOfDay);109int y = (int) mod((long)year, 28L); // 28-year cycle110cal.clear();111cal.set(cal.ERA, cal.AD);112cal.set(y, Calendar.DECEMBER, 31);113cal.set(cal.MILLISECOND, timeOfDay);114long localtime = cal.getTimeInMillis() + rawOffset; // local standard time115116cal.clear();117cal.set(cal.ERA, cal.AD);118cal.set(y + 1, Calendar.JANUARY, 1);119cal.set(cal.MILLISECOND, -saving);120long endTime = cal.getTimeInMillis() + rawOffset;121long expectedOffset = (localtime < endTime) ? rawOffset + saving : rawOffset;122if (offset != expectedOffset) {123errln("test#1: wrong offset: got "+offset+", expected="+expectedOffset);124}125126// test#2127saving = 1;128timeOfDay = 0;129tz = new SimpleTimeZone(rawOffset, "stz",130Calendar.JULY, 1, 0, 0,131Calendar.JANUARY, 1, 0, 0,132saving);133tz.setStartYear(year);134offset = tz.getOffset(GregorianCalendar.AD,135year,136Calendar.AUGUST,13715,1381, // should be MONDAY139timeOfDay);140y = (int) mod((long)year, 28L); // 28-year cycle141cal.clear();142cal.set(y, Calendar.AUGUST, 15);143cal.set(cal.MILLISECOND, timeOfDay);144localtime = cal.getTimeInMillis() + rawOffset; // local standard time145146cal.clear();147cal.set(y + 1, Calendar.JANUARY, 1);148cal.set(cal.MILLISECOND, -saving);149endTime = cal.getTimeInMillis() + rawOffset;150expectedOffset = (localtime < endTime) ? rawOffset + saving : rawOffset;151if (offset != expectedOffset) {152errln("Wrong offset: got "+offset+", expected="+expectedOffset);153}154155rawOffset = 43200000;156saving = 1;157timeOfDay = 3599998;158tz = new SimpleTimeZone(rawOffset, "stz",159Calendar.JULY, 1, 0, 3600000,160Calendar.JANUARY, 1, 0, 3600000,161saving);162tz.setStartYear(year);163offset = tz.getOffset(GregorianCalendar.AD,164year,165Calendar.JANUARY,1661,1671,168timeOfDay);169y = (int) mod((long)year, 28L); // 28-year cycle170cal.clear();171cal.set(y, Calendar.JANUARY, 1);172cal.set(cal.MILLISECOND, timeOfDay);173localtime = cal.getTimeInMillis() + rawOffset; // local standard time174175cal.clear();176cal.set(y + 1, Calendar.JANUARY, 1);177cal.set(cal.MILLISECOND, 3600000-saving);178endTime = cal.getTimeInMillis() + rawOffset;179expectedOffset = (localtime < endTime) ? rawOffset + saving : rawOffset;180if (offset != expectedOffset) {181errln("test#2: wrong offset: got "+offset+", expected="+expectedOffset);182}183184// test#3185rawOffset = -43200000;186saving = 1800000;187timeOfDay = 84600001;188tz = new SimpleTimeZone(rawOffset, "stz",189Calendar.SEPTEMBER, 1, 0, 0,190Calendar.MARCH, 1, 0, 0,191saving);192tz.setStartYear(year);193offset = tz.getOffset(GregorianCalendar.AD,194year,195Calendar.FEBRUARY,19628,1971,198timeOfDay);199y = (int) mod((long)year, 28L); // 28-year cycle200cal.clear();201cal.set(y, Calendar.FEBRUARY, 28);202cal.set(cal.MILLISECOND, timeOfDay);203localtime = cal.getTimeInMillis() + rawOffset; // local standard time204205cal.clear();206cal.set(y, Calendar.MARCH, 1);207cal.set(cal.MILLISECOND, -saving);208endTime = cal.getTimeInMillis() + rawOffset;209expectedOffset = (localtime < endTime) ? rawOffset + saving : rawOffset;210if (offset != expectedOffset) {211errln("test#3: wrong offset: got "+offset+", expected="+expectedOffset);212}213214// test#4215rawOffset = -43200000;216saving = 1;217timeOfDay = 0;218tz = new SimpleTimeZone(rawOffset, "stz",219Calendar.JANUARY, -4, 1, 3600000,220Calendar.JULY, -4, 1, 3600000,221saving);222tz.setStartYear(year);223offset = tz.getOffset(GregorianCalendar.AD,224year,225Calendar.JANUARY,22610,2272, // should be 1 (SUNDAY)228timeOfDay);229y = (int) mod((long)year, 28L); // 28-year cycle230cal.clear();231cal.set(y, Calendar.JANUARY, 10);232cal.set(cal.MILLISECOND, timeOfDay);233localtime = cal.getTimeInMillis() + rawOffset; // local standard time234235cal.clear();236cal.set(cal.YEAR, y);237cal.set(cal.MONTH, Calendar.JANUARY);238cal.set(cal.DAY_OF_MONTH, 8);239cal.set(cal.WEEK_OF_MONTH, cal.getActualMaximum(cal.WEEK_OF_MONTH)-4+1);240cal.set(cal.DAY_OF_WEEK, 1);241cal.set(cal.MILLISECOND, 3600000-saving);242long startTime = cal.getTimeInMillis() + rawOffset;243expectedOffset = (localtime >= startTime) ? rawOffset + saving : rawOffset;244if (offset != expectedOffset) {245errln("test#4: wrong offset: got "+offset+", expected="+expectedOffset);246}247248// test#5249rawOffset = 0;250saving = 3600000;251timeOfDay = 7200000;252year = 1982;253tz = new SimpleTimeZone(rawOffset, "stz",254Calendar.APRIL, 1, 0, 7200000,255Calendar.OCTOBER, 10, 0, 7200000,256saving);257offset = tz.getOffset(GregorianCalendar.AD,258year,259Calendar.OCTOBER,26010,2611,262timeOfDay);263cal.clear();264cal.set(year, Calendar.OCTOBER, 10);265cal.set(cal.MILLISECOND, timeOfDay);266localtime = cal.getTimeInMillis() + rawOffset; // local standard time267268cal.clear();269cal.set(year, Calendar.OCTOBER, 10);270cal.set(cal.MILLISECOND, 7200000-saving);271endTime = cal.getTimeInMillis() + rawOffset;272expectedOffset = (localtime < endTime) ? rawOffset + saving : rawOffset;273if (offset != expectedOffset) {274errln("test#5: wrong offset: got "+offset+", expected="+expectedOffset);275}276}277278public static final long floorDivide(long n, long d) {279return ((n >= 0) ?280(n / d) : (((n + 1L) / d) - 1L));281}282283public static final long mod(long x, long y) {284return (x - y * floorDivide(x, y));285}286}287288289