|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +/* @test |
| 25 | + * @bug 8285838 |
| 26 | + * @library /test/lib |
| 27 | + * @summary This test will ensure that daylight savings rules are followed |
| 28 | + * appropriately when setting a custom timezone ID via the TZ env variable. |
| 29 | + * @requires os.family != "windows" |
| 30 | + * @run main/othervm CustomTzIDCheckDST |
| 31 | + */ |
| 32 | +import java.util.Calendar; |
| 33 | +import java.util.Date; |
| 34 | +import java.util.List; |
| 35 | +import java.util.SimpleTimeZone; |
| 36 | +import java.time.DayOfWeek; |
| 37 | +import java.time.ZonedDateTime; |
| 38 | +import java.time.temporal.TemporalAdjusters; |
| 39 | +import jdk.test.lib.process.ProcessTools; |
| 40 | +import jdk.test.lib.process.OutputAnalyzer; |
| 41 | +public class CustomTzIDCheckDST { |
| 42 | + private static String CUSTOM_TZ = "MEZ-1MESZ,M3.5.0,M10.5.0"; |
| 43 | + public static void main(String args[]) throws Throwable { |
| 44 | + if (args.length == 0) { |
| 45 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(List.of("CustomTzIDCheckDST", "runTZTest")); |
| 46 | + pb.environment().put("TZ", CUSTOM_TZ); |
| 47 | + OutputAnalyzer output = ProcessTools.executeProcess(pb); |
| 48 | + output.shouldHaveExitValue(0); |
| 49 | + } else { |
| 50 | + runTZTest(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /* TZ code will always be set to "MEZ-1MESZ,M3.5.0,M10.5.0". |
| 55 | + * This ensures the transition periods for Daylights Savings should be at March's last |
| 56 | + * Sunday and October's last Sunday. |
| 57 | + */ |
| 58 | + private static void runTZTest() { |
| 59 | + Date time = new Date(); |
| 60 | + if (new SimpleTimeZone(3600000, "MEZ-1MESZ", Calendar.MARCH, -1, Calendar.SUNDAY, 0, |
| 61 | + Calendar.OCTOBER, -1, Calendar.SUNDAY, 0).inDaylightTime(time)) { |
| 62 | + // We are in Daylight savings period. |
| 63 | + if (time.toString().endsWith("GMT+02:00 " + Integer.toString(time.getYear() + 1900))) |
| 64 | + return; |
| 65 | + } else { |
| 66 | + if (time.toString().endsWith("GMT+01:00 " + Integer.toString(time.getYear() + 1900))) |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Reaching here means time zone did not match up as expected. |
| 71 | + throw new RuntimeException("Got unexpected timezone information: " + time); |
| 72 | + } |
| 73 | + |
| 74 | + private static ZonedDateTime getLastSundayOfMonth(ZonedDateTime date) { |
| 75 | + return date.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)); |
| 76 | + } |
| 77 | +} |
0 commit comments