undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,148 @@
/*
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4955000 8347841
* @summary Make sure that a Date and a GregorianCalendar produce the
* same date/time. Both are new implementations in 1.5.
*/
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import static java.util.GregorianCalendar.*;
@SuppressWarnings("deprecation")
public class Bug4955000 {
// Tests for Date.UTC(), derived from JCK
// Date.miscTests.Date1025 and Date2015
public static void main(String[] args) {
TimeZone defaultTZ = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("Pacific/Auckland"));
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
// Date1025
int[] years1 = {
Integer.MIN_VALUE,
Integer.MIN_VALUE + 1,
gc.getMinimum(YEAR) - 1,
gc.getMaximum(YEAR) + 1,
Integer.MAX_VALUE - 1,
Integer.MAX_VALUE
};
for (int i = 0; i < years1.length; i++) {
gc.clear();
gc.set(years1[i], JANUARY, 1);
long t = gc.getTimeInMillis();
long utc = Date.UTC(years1[i] - 1900, 1 - 1, 1,
0, 0, 0); // Jan 1 00:00:00
if (t != utc) {
throw new RuntimeException("t (" + t + ") != utc (" + utc + ")");
}
}
// Date2015
int[] years = {
gc.getGreatestMinimum(YEAR),
gc.getGreatestMinimum(YEAR) + 1,
-1,
0,
1,
gc.getLeastMaximum(YEAR) - 1,
gc.getLeastMaximum(YEAR)
};
int[] months = {
gc.getMinimum(MONTH),
gc.getMinimum(MONTH) + 1,
gc.getMaximum(MONTH) - 1,
gc.getMaximum(MONTH)
};
int[] dates = {
gc.getMinimum(DAY_OF_MONTH),
gc.getMinimum(DAY_OF_MONTH) + 1,
gc.getMaximum(DAY_OF_MONTH) - 1,
gc.getMaximum(DAY_OF_MONTH)
};
int[] hs = {
gc.getMinimum(HOUR),
gc.getMinimum(HOUR) + 1,
gc.getMaximum(HOUR) - 1,
gc.getMaximum(HOUR)
};
int[] ms = {
gc.getMinimum(MINUTE),
gc.getMinimum(MINUTE) + 1,
gc.getMaximum(MINUTE) - 1,
gc.getMaximum(MINUTE)
};
int[] ss = {
gc.getMinimum(SECOND),
gc.getMinimum(SECOND) + 1,
gc.getMaximum(SECOND) - 1,
gc.getMaximum(SECOND)
};
for (int i = 0; i < years.length; i++) {
for (int j = 0; j < months.length; j++) {
for (int k = 0; k < dates.length; k++) {
for (int m = 0; m < hs.length; m++) {
for (int n = 0; n < ms.length; n++) {
for (int p = 0; p < ss.length; p++) {
int year = years[i] - 1900;
int month = months[j];
int date = dates[k];
int hours = hs[m];
int minutes = ms[n];
int seconds = ss[p];
long result = Date.UTC(year, month, date,
hours, minutes, seconds);
gc.clear();
gc.set(year + 1900, month, date, hours, minutes, seconds);
long expected = gc.getTime().getTime();
if (expected != result) {
throw new RuntimeException("expected (" + expected
+ ") != result (" + result + ")");
}
}
}
}
}
}
}
} finally {
TimeZone.setDefault(defaultTZ);
}
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8135055
* @modules java.sql
* @summary Test java.sql.TimeStamp instance should come after java.util.Date
* if Nanos component of TimeStamp is not equal to 0 milliseconds.
*/
import java.sql.Timestamp;
import java.util.Date;
public class Bug8135055 {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
Date d = new Date();
Timestamp ts = new Timestamp(d.getTime());
if (d.after(ts)) {
throw new RuntimeException("date with time " + d.getTime()
+ " should not be after TimeStamp , Nanos component of "
+ "TimeStamp is " +ts.getNanos());
}
Thread.sleep(1);
}
}
}

View file

@ -0,0 +1,235 @@
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4614842
* @summary Make sure that a Date and a GregorianCalendar produce the same date/time. Both are new implementations in 1.5.
* @run main DateGregorianCalendarTest 15
*/
import java.util.*;
import static java.util.GregorianCalendar.*;
// Usage: java DateGregorianCalendarTest [duration]
@SuppressWarnings("deprecation")
public class DateGregorianCalendarTest {
static volatile boolean runrun = true;
static int nThreads;
public static void main(String[] args) {
int duration = 600;
if (args.length == 1) {
duration = Math.max(10, Integer.parseInt(args[0]));
}
TimeZone savedTZ = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Thread[] t = new Thread[10]; // for future bugs...
int index = 0;
t[index++] = new Thread(new Runnable() {
public void run() {
GregorianCalendar gc = new GregorianCalendar();
long delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);
long count = 0;
try {
for (long t = Long.MIN_VALUE; runrun && t < Long.MAX_VALUE-delta; t += delta) {
gc.setTimeInMillis(t);
Date date = new Date(t);
int y;
if (!((y = gc.get(YEAR)) == (date.getYear()+1900) &&
gc.get(MONTH) == date.getMonth() &&
gc.get(DAY_OF_MONTH) == date.getDate() &&
gc.get(HOUR_OF_DAY) == date.getHours() &&
gc.get(MINUTE) == date.getMinutes() &&
gc.get(SECOND) == date.getSeconds())) {
throw new RuntimeException("GregorinCalendar and Date returned different dates."
+" (millis=" + t + ")\n"
+"GC=" + gc + "\nDate=" + date);
}
++count;
if (y >= 1) {
delta = (long)(365.2422 * 24 * 60 * 60 * 1000);
}
if (y >= 1970) {
delta = (24 * 60 * 60 * 1000);
}
if (y >= 2039) {
delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);
}
}
if (runrun) {
System.out.println("Part I (count="+count+"): Passed");
} else {
System.out.println("Part I (count="+count+"): Incomplete");
}
} catch (RuntimeException e) {
System.out.println("Part I (count="+count+"): FAILED");
runrun = false;
throw e;
} finally {
decrementCounter();
}
}
});
t[index++] = new Thread(new Runnable() {
public void run() {
GregorianCalendar gc = new GregorianCalendar();
long count = 0;
int delta;
try {
for (long year = Integer.MIN_VALUE+1900;
runrun && year <= Integer.MAX_VALUE; year += delta) {
checkTimes(gc, year, JANUARY, 1, 0, 0, 0);
++count;
delta = getDelta((int)year);
}
for (long month = Integer.MIN_VALUE;
runrun && month <= Integer.MAX_VALUE; month += delta) {
checkTimes(gc, 1900, month, 1, 0, 0, 0);
++count;
delta = getDelta(gc.get(YEAR));
}
for (long dayOfMonth = Integer.MIN_VALUE;
runrun && dayOfMonth <= Integer.MAX_VALUE; dayOfMonth += delta) {
checkTimes(gc, 1900, JANUARY, dayOfMonth, 0, 0, 0);
++count;
delta = getDelta(gc.get(YEAR));
}
if (runrun) {
System.out.println("Part II (count="+count+"): Passed");
} else {
System.out.println("Part II (count="+count+"): Incomplete");
}
} catch (RuntimeException e) {
System.out.println("Part II (count="+count+"): FAILED");
runrun = false;
throw e;
} finally {
decrementCounter();
}
}
});
// t3 takes more than 10 minutes (on Ultra-60 450MHz) without
// the 4936355 fix due to getting the small delta.
t[index++] = new Thread(new Runnable() {
public void run() {
GregorianCalendar gc = new GregorianCalendar();
long count = 0;
int delta;
try {
for (long hourOfDay = Integer.MIN_VALUE;
runrun && hourOfDay <= Integer.MAX_VALUE; hourOfDay += delta) {
checkTimes(gc, 1970, JANUARY, 1, hourOfDay, 0, 0);
++count;
delta = getDelta(gc.get(YEAR));
}
for (long minutes = Integer.MIN_VALUE;
runrun && minutes <= Integer.MAX_VALUE; minutes += delta) {
checkTimes(gc, 1970, JANUARY, 1, 0, minutes, 0);
++count;
delta = getDelta(gc.get(YEAR)) * 60;
}
for (long seconds = Integer.MIN_VALUE;
runrun && seconds <= Integer.MAX_VALUE; seconds += delta) {
checkTimes(gc, 1970, JANUARY, 1, 0, 0, seconds);
++count;
delta = getDelta(gc.get(YEAR)) * 60 * 60;
}
if (runrun) {
System.out.println("Part III (count="+count+"): Passed");
} else {
System.out.println("Part III (count="+count+"): Incomplete");
}
} catch (RuntimeException e) {
System.out.println("Part III (count="+count+"): FAILED");
runrun = false;
throw e;
} finally {
decrementCounter();
}
}
});
for (int i = 0; i < index; i++) {
incrementCounter();
t[i].start();
}
try {
for (int i = 0; getCounter() > 0 && i < duration; i++) {
Thread.sleep(1000);
}
runrun = false;
for (int i = 0; i < index; i++) {
t[i].join();
}
} catch (InterruptedException e) {
}
} finally {
TimeZone.setDefault(savedTZ);
}
}
static void checkTimes(GregorianCalendar gc, long year, long month, long dayOfMonth,
long hourOfDay, long minutes, long seconds) {
gc.clear();
gc.set((int)year, (int)month, (int)dayOfMonth, (int)hourOfDay, (int)minutes, (int)seconds);
long time = gc.getTimeInMillis();
Date date = new Date((int)year - 1900, (int)month, (int)dayOfMonth,
(int)hourOfDay, (int)minutes, (int)seconds);
long time2 = date.getTime();
if (time != time2) {
throw new RuntimeException("GregorinCalendar and Date returned different values.\n"
+"year="+year+", month="+month+", dayOfMonth="+dayOfMonth
+"\nhourOfDay="+hourOfDay+", minutes="+minutes+", seconds="+seconds
+"\ntime=" + time + ", time2=" + time2
+"\nGC=" + gc + "\nDate=" + date);
}
}
static final int getDelta(int year) {
return (year >= 1970 && year <= 2039) ? 1 : 1<<13;
}
synchronized static void incrementCounter() {
nThreads++;
}
synchronized static void decrementCounter() {
nThreads--;
}
synchronized static int getCounter() {
return nThreads;
}
}

View file

@ -0,0 +1,253 @@
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4023247 4027685 4032037 4072029 4073003 4118010 4120606 4133833
* 4136916 6274757 6314387 8347841
* @run junit DateRegression
*/
import java.util.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings("deprecation")
public class DateRegression {
/**
* @bug 4023247
*/
@Test
public void Test4023247() {
Date d1 = new Date(0);
Date d2 = new Date(0);
d1.setYear(96);
d1.setMonth(11);
d1.setDate(22);
d1.setHours(0);
d1.setMinutes(0);
d1.setSeconds(0);
d2.setYear(96);
d2.setMonth(11);
d2.setDate(22);
d2.setHours(0);
d2.setMinutes(0);
d2.setSeconds(0);
if (d1.hashCode() != d2.hashCode())
fail("Fail: Date hashCode misbehaves");
}
/**
* @bug 4027685
*/
@Test
public void Test4027685() {
// Should be 01/16/97 00:00:00
Date nite = new Date("16-JAN-97 12:00 AM");
// Should be 01/16/97 12:00:00
Date noon = new Date("16-JAN-97 12:00 PM");
System.out.println("Midnight = " + nite + ", Noon = " + noon);
if (!nite.equals(new Date(97, Calendar.JANUARY, 16, 0, 0)) ||
!noon.equals(new Date(97, Calendar.JANUARY, 16, 12, 0)))
fail("Fail: Nite/Noon confused");
}
/**
* @bug 4032037
*/
@Test
public void Test4032037() {
Date ref = new Date(97, 1, 10);
Date d = new Date(Date.parse("2/10/97"));
System.out.println("Date.parse(2/10/97) => " + d);
if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
d = new Date(Date.parse("10 feb 1997"));
System.out.println("Date.parse(10 feb 1997) => " + d);
if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
d = new Date("2/10/97");
System.out.println("Date(2/10/97) => " + d);
if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
d = new Date("10 feb 1997");
System.out.println("Date(10 feb 1997) => " + d);
if (!d.equals(ref)) fail("Fail: Want " + ref + " Got " + d);
}
/**
* @bug 4072029
*/
@Test
public void Test4072029() {
TimeZone saveZone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
Date now = new Date();
String s = now.toString();
Date now2 = new Date(now.toString());
String s2 = now2.toString(); // An hour's difference
if (!s.equals(s2) ||
Math.abs(now.getTime() - now2.getTime()) > 60000 /*one min*/) {
fail("Fail: Roundtrip toString/parse");
}
}
finally {
TimeZone.setDefault(saveZone);
}
}
/**
* @bug 4073003
*/
@Test
public void Test4073003() {
Date d = new Date(Date.parse("01/02/1984"));
if (!d.equals(new Date(84, 0, 2)))
fail("Fail: Want 1/2/1984 Got " + d);
d = new Date(Date.parse("02/03/2012"));
if (!d.equals(new Date(112, 1, 3)))
fail("Fail: Want 2/3/2012 Got " + d);
d = new Date(Date.parse("03/04/15"));
if (!d.equals(new Date(115, 2, 4)))
fail("Fail: Want 3/4/2015 Got " + d);
}
/**
* @bug 4118010
* Regress bug:
* Feb. 2000 has 29 days, but Date(2000, 1, 29) returns March 01, 2000
* NOTE: This turned out to be a user error (passing in 2000 instead
* of 2000-1900 to the Date constructor).
*/
@Test
public void Test4118010() {
Date d=new java.util.Date(2000-1900, Calendar.FEBRUARY, 29);
int m=d.getMonth();
int date=d.getDate();
if (m != Calendar.FEBRUARY ||
date != 29)
fail("Fail: Want Feb 29, got " + d);
}
/**
* @bug 4120606
* Date objects share state after cloning.
*/
@Test
public void Test4120606() {
Date d = new Date(98, Calendar.JUNE, 24);
d.setMonth(Calendar.MAY);
Date e = (Date)d.clone();
d.setMonth(Calendar.FEBRUARY);
if (e.getMonth() != Calendar.MAY) {
fail("Cloned Date objects share state");
}
}
/**
* @bug 4133833
* Date constructor crashes with parameters out of range, when it should
* normalize.
*/
@Test
public void Test4133833() {
Date date = new java.util.Date(12,15,19);
Date exp = new Date(1913-1900, Calendar.APRIL, 19);
if (!date.equals(exp))
fail("Fail: Want " + exp +
"; got " + date);
}
/**
* @bug 4136916
* Date.toString() throws exception in 1.2b4-E
* CANNOT REPRODUCE this bug
*/
@Test
public void Test4136916() {
Date time = new Date();
System.out.println(time.toString());
}
/**
* @bug 6274757
* Date getTime and toString interaction for some time values
*/
@Test
public void Test6274757() {
TimeZone savedTz = TimeZone.getDefault();
try {
// Use a time zone west of GMT.
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
TimeZone jdkGMT = TimeZone.getTimeZone("GMT");
Calendar jdkCal = Calendar.getInstance(jdkGMT);
jdkCal.clear();
jdkCal.set(1582, Calendar.OCTOBER, 15);
System.out.println("JDK time: " + jdkCal.getTime().getTime() );
System.out.println("JDK time (str): " + jdkCal.getTime() );
System.out.println("Day of month: " + jdkCal.get(Calendar.DAY_OF_MONTH));
Date co = jdkCal.getTime();
System.out.println("Change over (Oct 15 1582) = " + co + " (" +
co.getTime() + ")");
long a = jdkCal.getTime().getTime();
Date c = jdkCal.getTime();
c.toString();
long b = c.getTime();
if (a != b) {
fail("ERROR: " + a + " != " + b);
} else {
System.out.println(a + " = " + b);
}
} finally {
TimeZone.setDefault(savedTz);
}
}
/**
* @bug 6314387
* JCK6.0: api/java_util/Date/index.html#misc fails, mustang
*/
@Test
public void Test6314387() {
Date d = new Date(Long.MAX_VALUE);
int y = d.getYear();
if (y != 292277094) {
fail("yesr: got " + y + ", expected 292277094");
}
d = new Date(Long.MIN_VALUE);
y = d.getYear();
if (y != 292267155) {
fail("yesr: got " + y + ", expected 292267155");
}
}
}
//eof

View file

@ -0,0 +1,242 @@
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4143459 8347841
* @summary test Date
* @run junit DateTest
*/
import java.text.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings("deprecation")
public class DateTest
{
/**
* @bug 4143459
* Warning: Use TestDefaultZone() for complete testing of this bug.
*/
@Test
public void TestDefaultZoneLite() {
// Note: This test is redundant with TestDefaultZone(). It was added by
// request to provide a short&sweet test for this bug. It does not test
// all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE
// PRESENT. Use TestDefaultZone() to be sure.
TimeZone save = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Date d = new Date();
d.setYear(98);
d.setMonth(Calendar.JANUARY);
d.setDate(1);
d.setHours(6);
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
if (d.getHours() != 22) {
fail("Fail: Date.setHours()/getHours() ignoring default zone");
}
}
finally { TimeZone.setDefault(save); }
}
/**
* @bug 4143459
*/
@Test
public void TestDefaultZone() {
// Various problems can creep up, with the current implementation of Date,
// when the default zone is changed.
TimeZone saveZone = TimeZone.getDefault();
try {
Date d = new Date(); // Trigger static init
Date ref = new Date(883634400000L); // This is Thu Jan 1 1998 6:00 am GMT
String refstr = "Jan 1 1998 6:00";
TimeZone GMT = TimeZone.getTimeZone("GMT");
TimeZone PST = TimeZone.getTimeZone("America/Los_Angeles");
String[] names = { "year", "month", "date", "day of week", "hour", "offset" };
int[] GMT_EXP = { 98, Calendar.JANUARY, 1, Calendar.THURSDAY - Calendar.SUNDAY, 6, 0 };
int[] PST_EXP = { 97, Calendar.DECEMBER, 31, Calendar.WEDNESDAY - Calendar.SUNDAY, 22, 480 };
// There are two cases to consider: a Date object with no Calendar
// sub-object (most Date objects), and a Date object with a Calendar
// sub-object. We make two passes to cover the two cases.
for (int pass=0; pass<2; ++pass) {
System.out.println(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");
TimeZone.setDefault(GMT);
d = new Date(refstr);
if (pass == 1) {
// Force creation of Calendar sub-object
d.setYear(d.getYear());
}
if (d.getTime() != ref.getTime()) {
fail("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d +
" " + d.getTime() + " ms");
}
int[] fields = { d.getYear(), d.getMonth(), d.getDate(),
d.getDay(), d.getHours(), d.getTimezoneOffset() };
for (int i=0; i<fields.length; ++i) {
if (fields[i] != GMT_EXP[i]) {
fail("FAIL: GMT Expected " + names[i] + " of " + GMT_EXP[i] +
", got " + fields[i]);
}
}
TimeZone.setDefault(PST);
int[] fields2 = { d.getYear(), d.getMonth(), d.getDate(),
d.getDay(), d.getHours(), d.getTimezoneOffset() };
for (int i=0; i<fields2.length; ++i) {
if (fields2[i] != PST_EXP[i]) {
fail("FAIL: PST Expected " + names[i] + " of " + PST_EXP[i] +
", got " + fields2[i]);
}
}
}
}
finally {
TimeZone.setDefault(saveZone);
}
}
// Test the performance of Date
@Test
public void TestPerformance592()
{
int REPS = 500;
// Do timing test with Date
long start = new Date().getTime();
for (int i=0; i<REPS; ++i)
{
Date d = new Date();
int y = d.getYear();
}
long ms = new Date().getTime() - start;
double perLoop = ((double)ms) / REPS;
System.out.println(REPS + " iterations at " + perLoop + " ms/loop");
if (perLoop > PER_LOOP_LIMIT)
System.out.println("WARNING: Date constructor/getYear slower than " +
PER_LOOP_LIMIT + " ms");
}
static double PER_LOOP_LIMIT = 3.0;
/**
* Verify that the Date(String) constructor works.
*/
@Test
public void TestParseOfGMT()
{
Date OUT = null;
/* Input values */
String stringVal = "Jan 01 00:00:00 GMT 1900";
long expectedVal = -2208988800000L;
OUT = new Date( stringVal );
if( OUT.getTime( ) == expectedVal ) {
// logln("PASS");
}
else {
fail( "Expected: " +
new Date( expectedVal ) +
": " +
expectedVal +
" Received: " +
OUT.toString() +
": " +
OUT.getTime() );
}
}
// Check out Date's behavior with large negative year values; bug 664
// As of the fix to bug 4056585, Date should work correctly with
// large negative years.
@Test
public void TestDateNegativeYears()
{
Date d1= new Date(80,-1,2);
System.out.println(d1.toString());
d1= new Date(-80,-1,2);
System.out.println(d1.toString());
boolean e = false;
try {
d1= new Date(-800000,-1,2);
System.out.println(d1.toString());
}
catch (IllegalArgumentException ex) {
e = true;
}
if (e) fail("FAIL: Saw exception for year -800000");
else System.out.println("Pass: No exception for year -800000");
}
// Verify the behavior of Date
@Test
public void TestDate480()
{
TimeZone save = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
Date d1=new java.util.Date(97,8,13,10,8,13);
System.out.println("d = "+d1);
Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
System.out.println("d+20h = "+d2);
double delta = (d2.getTime() - d1.getTime()) / 3600000;
System.out.println("delta = " + delta + "h");
if (delta != 20.0) fail("Expected delta of 20; got " + delta);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(1997,8,13,10,8,13);
Date t1 = cal.getTime();
System.out.println("d = "+t1);
cal.clear();
cal.set(1997,8,13,30,8,13); // 20 hours later
Date t2 = cal.getTime();
System.out.println("d+20h = "+t2);
double delta2 = (t2.getTime() - t1.getTime()) / 3600000;
System.out.println("delta = " + delta2 + "h");
if (delta != 20.0) fail("Expected delta of 20; got " + delta2);
}
finally {
TimeZone.setDefault(save);
}
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4108737
* @summary java.util.Date doesn't fail if current TimeZone is changed
*/
import java.util.TimeZone;
import java.util.Date;
public class TZ {
public static void main(String args[]) {
TimeZone tz = TimeZone.getDefault();
try {
testMain();
} finally {
TimeZone.setDefault(tz);
}
}
static void testMain() {
String expectedResult = "Sat Feb 01 00:00:00 PST 1997";
// load the java.util.Date class in the GMT timezone
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
new Date(); // load the class (to run static initializers)
// use the class in different timezone
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
@SuppressWarnings("deprecation")
Date date = new Date(97, 1, 1);
if (!date.toString().equals(expectedResult)) {
throw new RuntimeException("Regression bug id #4108737 - Date fails if default time zone changed");
}
}
}

View file

@ -0,0 +1,135 @@
/*
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 5008227
* @summary Make sure that changes to the Date class don't break java.sql.Timestamp.
* @modules java.sql
* @run junit TimestampTest
*/
import java.util.*;
import java.sql.Timestamp;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
public class TimestampTest {
/**
* 5008227: java.sql.Timestamp.after() is not returning correct result
*
* Test before(), after(), equals(), compareTo() and getTime().
*/
@Test
public void Test5008227() {
long t = System.currentTimeMillis();
Timestamp ts1 = new Timestamp(t), ts2 = new Timestamp(t);
ts1.setNanos(999999999);
ts2.setNanos( 1000000);
compareTimestamps(ts1, ts2, 1);
ts1.setTime(t + 1000);
ts2.setTime(t);
ts1.setNanos( 999999);
ts2.setNanos(999999999);
compareTimestamps(ts1, ts2, 1);
ts1.setTime(t);
ts2.setTime(t);
ts1.setNanos(123456789);
ts2.setNanos(123456789);
compareTimestamps(ts1, ts2, 0);
ts1.setTime(t);
ts2.setTime(t);
ts1.setNanos(1);
ts2.setNanos(2);
compareTimestamps(ts1, ts2, -1);
ts1.setTime(t);
ts2.setTime(t+1000);
ts1.setNanos(999999);
ts2.setNanos( 0);
compareTimestamps(ts1, ts2, -1);
}
/**
* Compares two Timestamps with the expected result.
*
* @param ts1 the first Timestamp
* @param ts2 the second Timestamp
* @param expect the expected relation between ts1 and ts2; 0 if
* ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
* before ts2.
*/
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
boolean expectedResult = expected > 0;
boolean result = ts1.after(ts2);
if (result != expectedResult) {
fail("ts1.after(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected < 0;
result = ts1.before(ts2);
if (result != expectedResult) {
fail("ts1.before(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
expectedResult = expected == 0;
result = ts1.equals(ts2);
if (result != expectedResult) {
fail("ts1.equals(ts2) returned " + result
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
int x = ts1.compareTo(ts2);
int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
if (y != expected) {
fail("ts1.compareTo(ts2) returned " + x + ", expected "
+ relation(expected, "") + "0"
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
long t1 = ts1.getTime();
long t2 = ts2.getTime();
int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
if (z == 0) {
int n1 = ts1.getNanos();
int n2 = ts2.getNanos();
z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
}
if (z != expected) {
fail("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
+ relation(expected, "==")
+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
}
}
private static String relation(int x, String whenEqual) {
return (x > 0) ? ">" : (x < 0) ? "<" : whenEqual;
}
}