Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/CalendarLimitTest.java
47182 views
1
/*
2
* Copyright (c) 1997, 2016, 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
/**
25
* @test
26
* @bug 4033662
27
* @library /java/text/testlib
28
* @summary test for limit on Calendar
29
* @run main CalendarLimitTest -verbose
30
*/
31
32
import java.util.*;
33
import java.text.*;
34
35
/**
36
* This test verifies the behavior of Calendar around the very earliest limits
37
* which it can handle. It also verifies the behavior for large values of millis.
38
*
39
* Note: There used to be a limit, due to a bug, for early times. There is
40
* currently no limit.
41
*
42
* March 17, 1998: Added code to make sure big + dates are big + AD years, and
43
* big - dates are big + BC years.
44
*/
45
public class CalendarLimitTest extends IntlTest
46
{
47
// This number determined empirically; this is the old limit,
48
// which we test for to make sure it isn't there anymore.
49
static final long EARLIEST_SUPPORTED_MILLIS = -210993120000000L;
50
51
static final int EPOCH_JULIAN_DAY = 2440588; // Jaunary 1, 1970 (Gregorian)
52
static final int JAN_1_1_JULIAN_DAY = 1721426; // January 1, year 1 (Gregorian)
53
54
// Useful millisecond constants
55
static final int ONE_SECOND = 1000;
56
static final int ONE_MINUTE = 60*ONE_SECOND;
57
static final int ONE_HOUR = 60*ONE_MINUTE;
58
static final int ONE_DAY = 24*ONE_HOUR;
59
static final int ONE_WEEK = 7*ONE_DAY;
60
static final long ONE_YEAR = (long)(365.2425 * ONE_DAY);
61
62
static long ORIGIN; // This is the *approximate* point at which BC switches to AD
63
64
public static void main(String argv[]) throws Exception {
65
new CalendarLimitTest().run(argv);
66
}
67
68
/**
69
* Converts Julian day to time as milliseconds.
70
* @param julian the given Julian day number.
71
* @return time as milliseconds.
72
*/
73
private static final long julianDayToMillis(long julian) {
74
return (julian - EPOCH_JULIAN_DAY) * ONE_DAY;
75
}
76
77
/**
78
* Verify that the given time is processed without problem.
79
* @return the adjust year, with 0 = 1 BC, -1 = 2 BC, etc.
80
*/
81
int test(long millis, Calendar cal, DateFormat fmt)
82
{
83
Exception exception = null;
84
String theDate = "";
85
try {
86
Date d= new Date(millis);
87
cal.setTime(d);
88
theDate = fmt.format(d);
89
}
90
catch (IllegalArgumentException e) {
91
exception = e;
92
}
93
String s = "0x" + Long.toHexString(millis) + " " + theDate;
94
95
int era=cal.get(Calendar.ERA), year=cal.get(Calendar.YEAR),
96
dom=cal.get(Calendar.DATE), mon=cal.get(Calendar.MONTH);
97
98
cal.clear();
99
cal.set(year, mon, dom);
100
cal.set(Calendar.ERA, era);
101
Date rt = cal.getTime();
102
103
boolean ok = true;
104
if (exception != null) {
105
errln("FAIL: Exception " + s);
106
ok = false;
107
}
108
if (((millis >= ORIGIN) && (era != GregorianCalendar.AD)) ||
109
((millis < ORIGIN) && (era != GregorianCalendar.BC)) ||
110
(year < 1)) {
111
errln("FAIL: Bad year/era " + s);
112
ok = false;
113
}
114
if (dom<1 || dom>31) {
115
errln("FAIL: Bad DOM " + s);
116
ok = false;
117
}
118
if (Math.abs(millis - rt.getTime()) > ONE_DAY) {
119
errln("FAIL: RT fail " + s + " -> 0x" +
120
Long.toHexString(rt.getTime()) + " " +
121
fmt.format(rt));
122
ok = false;
123
}
124
if (ok) logln(s);
125
if (era==GregorianCalendar.BC) year = 1-year;
126
return year;
127
}
128
129
public void TestCalendarLimit()
130
{
131
ORIGIN = julianDayToMillis(JAN_1_1_JULIAN_DAY);
132
133
Calendar cal = Calendar.getInstance();
134
// You must set the time zone to GMT+0 or the edge cases like
135
// Long.MIN_VALUE, Long.MAX_VALUE, and right around the threshold
136
// won't work, since before converting to fields the calendar code
137
// will add the offset for the zone.
138
cal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));
139
140
DateFormat dateFormat = DateFormat.getDateInstance();
141
dateFormat.setCalendar(cal); // Make sure you do this -- same reason as above
142
((SimpleDateFormat)dateFormat).applyPattern("MMM d, yyyy G");
143
144
// Don't expect any failure for positive longs
145
int lastYear=0;
146
boolean first=true;
147
for (long m = Long.MAX_VALUE; m > 0; m >>= 1)
148
{
149
int y = test(m, cal, dateFormat);
150
if (!first && y > lastYear)
151
errln("FAIL: Years should be decreasing " + lastYear + " " + y);
152
first = false;
153
lastYear = y;
154
}
155
156
// Expect failures for negative millis below threshold
157
first = true;
158
for (long m = Long.MIN_VALUE; m < 0; m /= 2) // Don't use m >>= 1
159
{
160
int y = test(m, cal, dateFormat);
161
if (!first && y < lastYear)
162
errln("FAIL: Years should be increasing " + lastYear + " " + y);
163
first = false;
164
lastYear = y;
165
}
166
167
// Test right around the threshold
168
test(EARLIEST_SUPPORTED_MILLIS, cal, dateFormat);
169
test(EARLIEST_SUPPORTED_MILLIS-1, cal, dateFormat);
170
171
// Test a date that should work
172
test(Long.MIN_VALUE + ONE_DAY, cal, dateFormat);
173
174
// Try hours in the earliest day or two
175
// JUST FOR DEBUGGING:
176
if (false) {
177
((SimpleDateFormat)dateFormat).applyPattern("H:mm MMM d, yyyy G");
178
for (int dom=2; dom<=3; ++dom) {
179
for (int h=0; h<24; ++h) {
180
cal.clear();
181
cal.set(Calendar.ERA, GregorianCalendar.BC);
182
cal.set(292269055, Calendar.DECEMBER, dom, h, 0);
183
Date d = cal.getTime();
184
cal.setTime(d);
185
logln("" + h + ":00 Dec "+dom+", 292269055 BC -> " +
186
Long.toHexString(d.getTime()) + " -> " +
187
dateFormat.format(cal.getTime()));
188
}
189
}
190
// Other way
191
long t = 0x80000000018c5c00L; // Dec 3, 292269055 BC
192
while (t<0) {
193
cal.setTime(new Date(t));
194
logln("0x" + Long.toHexString(t) + " -> " +
195
dateFormat.format(cal.getTime()));
196
t -= ONE_HOUR;
197
}
198
}
199
}
200
}
201
202
//eof
203
204