Skip to content

Commit

Permalink
8299194: CustomTzIDCheckDST.java may fail at future date
Browse files Browse the repository at this point in the history
Backport-of: 5e2de89628aaf6acb8e458fb417426ca5e477bea
  • Loading branch information
Ichiroh Takiguchi committed Feb 4, 2023
1 parent 2fa09bb commit 0ed3689
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions test/jdk/java/util/TimeZone/CustomTzIDCheckDST.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,48 @@
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
public class CustomTzIDCheckDST {
private static String CUSTOM_TZ = "MEZ-1MESZ,M3.5.0,M10.5.0";
// Northern Hemisphere
private static String CUSTOM_TZ = "MEZ-1MESZ,M3.5.0,M10.5.0/3";
// Simulate Southern Hemisphere
private static String CUSTOM_TZ2 = "MEZ-1MESZ,M10.5.0,M3.5.0/3";
public static void main(String args[]) throws Throwable {
if (args.length == 0) {
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(List.of("CustomTzIDCheckDST", "runTZTest"));
pb.environment().put("TZ", CUSTOM_TZ);
OutputAnalyzer output = ProcessTools.executeProcess(pb);
output.shouldHaveExitValue(0);
pb.environment().put("TZ", CUSTOM_TZ2);
output = ProcessTools.executeProcess(pb);
output.shouldHaveExitValue(0);
} else {
runTZTest();
}
}

/* TZ code will always be set to "MEZ-1MESZ,M3.5.0,M10.5.0".
/* TZ is set to "MEZ-1MESZ,M3.5.0,M10.5.0/3", it will be the northern hemisphere.
* This ensures the transition periods for Daylights Savings should be at March's last
* Sunday and October's last Sunday.
*/
private static void runTZTest() {
Date time = new Date();
if (new SimpleTimeZone(3600000, "MEZ-1MESZ", Calendar.MARCH, -1, Calendar.SUNDAY, 0,
Calendar.OCTOBER, -1, Calendar.SUNDAY, 0).inDaylightTime(time)) {
String tzStr = System.getenv("TZ");
if (tzStr == null)
throw new RuntimeException("Got unexpected timezone information: TZ is null");
boolean nor = CUSTOM_TZ.equals(tzStr);
TimeZone tz = new SimpleTimeZone(3600000, tzStr,
nor ? Calendar.MARCH : Calendar.OCTOBER, -1,
Calendar.SUNDAY, 3600000, SimpleTimeZone.UTC_TIME,
nor ? Calendar.OCTOBER : Calendar.MARCH, -1,
Calendar.SUNDAY, 3600000, SimpleTimeZone.UTC_TIME,
3600000);
if (tz.inDaylightTime(time)) {
// We are in Daylight savings period.
if (time.toString().endsWith("GMT+02:00 " + Integer.toString(time.getYear() + 1900)))
return;
Expand All @@ -68,7 +84,7 @@ private static void runTZTest() {
}

// Reaching here means time zone did not match up as expected.
throw new RuntimeException("Got unexpected timezone information: " + time);
throw new RuntimeException("Got unexpected timezone information: " + tzStr + " " + time);
}

private static ZonedDateTime getLastSundayOfMonth(ZonedDateTime date) {
Expand Down

1 comment on commit 0ed3689

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.