1 /*
  2  * Copyright (c) 1997, 2023, 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  * @summary test for limit on Calendar
 28  * @library /java/text/testlib
 29  * @run junit CalendarLimitTest
 30  */
 31 
 32 import java.util.*;
 33 import java.text.*;
 34 
 35 import org.junit.jupiter.api.Test;
 36 
 37 import static org.junit.jupiter.api.Assertions.fail;
 38 
 39 /**
 40  * This test verifies the behavior of Calendar around the very earliest limits
 41  * which it can handle.  It also verifies the behavior for large values of millis.
 42  *
 43  * Note: There used to be a limit, due to a bug, for early times.  There is
 44  * currently no limit.
 45  *
 46  * March 17, 1998: Added code to make sure big + dates are big + AD years, and
 47  * big - dates are big + BC years.
 48  */
 49 public class CalendarLimitTest
 50 {
 51     // This number determined empirically; this is the old limit,
 52     // which we test for to make sure it isn't there anymore.
 53     static final long EARLIEST_SUPPORTED_MILLIS = -210993120000000L;
 54 
 55     static final int EPOCH_JULIAN_DAY   = 2440588; // Jaunary 1, 1970 (Gregorian)
 56     static final int JAN_1_1_JULIAN_DAY = 1721426; // January 1, year 1 (Gregorian)
 57 
 58     // Useful millisecond constants
 59     static final int  ONE_SECOND = 1000;
 60     static final int  ONE_MINUTE = 60*ONE_SECOND;
 61     static final int  ONE_HOUR   = 60*ONE_MINUTE;
 62     static final int  ONE_DAY    = 24*ONE_HOUR;
 63     static final int  ONE_WEEK   = 7*ONE_DAY;
 64     static final long ONE_YEAR   = (long)(365.2425 * ONE_DAY);
 65 
 66     static long ORIGIN; // This is the *approximate* point at which BC switches to AD
 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             fail("FAIL: Exception " + s);
106             ok = false;
107         }
108         if (((millis >= ORIGIN) && (era != GregorianCalendar.AD)) ||
109                  ((millis < ORIGIN) && (era != GregorianCalendar.BC)) ||
110                  (year < 1)) {
111             fail("FAIL: Bad year/era " + s);
112             ok = false;
113         }
114         if (dom<1 || dom>31) {
115             fail("FAIL: Bad DOM " + s);
116             ok = false;
117         }
118         if (Math.abs(millis - rt.getTime()) > ONE_DAY) {
119             fail("FAIL: RT fail " + s + " -> 0x" +
120                   Long.toHexString(rt.getTime()) + " " +
121                   fmt.format(rt));
122             ok = false;
123         }
124         if (ok) System.out.println(s);
125         if (era==GregorianCalendar.BC) year = 1-year;
126         return year;
127     }
128 
129     @Test
130     public void TestCalendarLimit()
131     {
132         Locale locale = Locale.getDefault();
133         if (!TestUtils.usesGregorianCalendar(locale)) {
134             System.out.println("Skipping this test because locale is " + locale);
135             return;
136         }
137         ORIGIN = julianDayToMillis(JAN_1_1_JULIAN_DAY);
138 
139         Calendar cal = Calendar.getInstance();
140         // You must set the time zone to GMT+0 or the edge cases like
141         // Long.MIN_VALUE, Long.MAX_VALUE, and right around the threshold
142         // won't work, since before converting to fields the calendar code
143         // will add the offset for the zone.
144         cal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));
145 
146         DateFormat dateFormat = DateFormat.getDateInstance();
147         dateFormat.setCalendar(cal); // Make sure you do this -- same reason as above
148         ((SimpleDateFormat)dateFormat).applyPattern("MMM d, yyyy G");
149 
150         // Don't expect any failure for positive longs
151         int lastYear=0;
152         boolean first=true;
153         for (long m = Long.MAX_VALUE; m > 0; m >>= 1)
154         {
155             int y = test(m, cal, dateFormat);
156             if (!first && y > lastYear)
157                 fail("FAIL: Years should be decreasing " + lastYear + " " + y);
158             first = false;
159             lastYear = y;
160         }
161 
162         // Expect failures for negative millis below threshold
163         first = true;
164         for (long m = Long.MIN_VALUE; m < 0; m /= 2) // Don't use m >>= 1
165         {
166             int y = test(m, cal, dateFormat);
167             if (!first && y < lastYear)
168                 fail("FAIL: Years should be increasing " + lastYear + " " + y);
169             first = false;
170             lastYear = y;
171         }
172 
173         // Test right around the threshold
174         test(EARLIEST_SUPPORTED_MILLIS,   cal, dateFormat);
175         test(EARLIEST_SUPPORTED_MILLIS-1, cal, dateFormat);
176 
177         // Test a date that should work
178         test(Long.MIN_VALUE + ONE_DAY,    cal, dateFormat);
179 
180         // Try hours in the earliest day or two
181         // JUST FOR DEBUGGING:
182         if (false) {
183             ((SimpleDateFormat)dateFormat).applyPattern("H:mm MMM d, yyyy G");
184             for (int dom=2; dom<=3; ++dom) {
185                 for (int h=0; h<24; ++h) {
186                     cal.clear();
187                     cal.set(Calendar.ERA, GregorianCalendar.BC);
188                     cal.set(292269055, Calendar.DECEMBER, dom, h, 0);
189                     Date d = cal.getTime();
190                     cal.setTime(d);
191                     System.out.println("" + h + ":00 Dec "+dom+", 292269055 BC -> " +
192                           Long.toHexString(d.getTime()) + " -> " +
193                           dateFormat.format(cal.getTime()));
194                 }
195             }
196             // Other way
197             long t = 0x80000000018c5c00L; // Dec 3, 292269055 BC
198             while (t<0) {
199                 cal.setTime(new Date(t));
200                 System.out.println("0x" + Long.toHexString(t) + " -> " +
201                       dateFormat.format(cal.getTime()));
202                 t -= ONE_HOUR;
203             }
204         }
205     }
206 }
207 
208 //eof