15289
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
|
32 |
* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package build.tools.tzdb;
|
|
63 |
|
|
64 |
import static build.tools.tzdb.Utils.*;
|
|
65 |
import static build.tools.tzdb.LocalTime.SECONDS_PER_DAY;
|
|
66 |
import static build.tools.tzdb.ChronoField.DAY_OF_MONTH;
|
|
67 |
import static build.tools.tzdb.ChronoField.MONTH_OF_YEAR;
|
|
68 |
import static build.tools.tzdb.ChronoField.YEAR;
|
|
69 |
|
|
70 |
import java.util.Objects;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* A date without a time-zone in the ISO-8601 calendar system,
|
|
74 |
* such as {@code 2007-12-03}.
|
|
75 |
*
|
|
76 |
* @since 1.8
|
|
77 |
*/
|
|
78 |
final class LocalDate {
|
|
79 |
|
|
80 |
/**
|
|
81 |
* The minimum supported {@code LocalDate}, '-999999999-01-01'.
|
|
82 |
* This could be used by an application as a "far past" date.
|
|
83 |
*/
|
|
84 |
public static final LocalDate MIN = new LocalDate(YEAR_MIN_VALUE, 1, 1);
|
|
85 |
/**
|
|
86 |
* The maximum supported {@code LocalDate}, '+999999999-12-31'.
|
|
87 |
* This could be used by an application as a "far future" date.
|
|
88 |
*/
|
|
89 |
public static final LocalDate MAX = new LocalDate(YEAR_MAX_VALUE, 12, 31);
|
|
90 |
|
|
91 |
/**
|
|
92 |
* The number of days in a 400 year cycle.
|
|
93 |
*/
|
|
94 |
private static final int DAYS_PER_CYCLE = 146097;
|
|
95 |
/**
|
|
96 |
* The number of days from year zero to year 1970.
|
|
97 |
* There are five 400 year cycles from year zero to 2000.
|
|
98 |
* There are 7 leap years from 1970 to 2000.
|
|
99 |
*/
|
|
100 |
static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L);
|
|
101 |
|
|
102 |
/**
|
|
103 |
* The year.
|
|
104 |
*/
|
|
105 |
private final int year;
|
|
106 |
/**
|
|
107 |
* The month-of-year.
|
|
108 |
*/
|
|
109 |
private final short month;
|
|
110 |
/**
|
|
111 |
* The day-of-month.
|
|
112 |
*/
|
|
113 |
private final short day;
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Obtains an instance of {@code LocalDate} from a year, month and day.
|
|
117 |
* <p>
|
|
118 |
* The day must be valid for the year and month, otherwise an exception will be thrown.
|
|
119 |
*
|
|
120 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
|
|
121 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December)
|
|
122 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31
|
|
123 |
* @return the local date, not null
|
|
124 |
* @throws DateTimeException if the value of any field is out of range
|
|
125 |
* @throws DateTimeException if the day-of-month is invalid for the month-year
|
|
126 |
*/
|
|
127 |
public static LocalDate of(int year, int month, int dayOfMonth) {
|
|
128 |
YEAR.checkValidValue(year);
|
|
129 |
MONTH_OF_YEAR.checkValidValue(month);
|
|
130 |
DAY_OF_MONTH.checkValidValue(dayOfMonth);
|
|
131 |
if (dayOfMonth > 28 && dayOfMonth > lengthOfMonth(month, isLeapYear(year))) {
|
|
132 |
if (dayOfMonth == 29) {
|
|
133 |
throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
|
|
134 |
} else {
|
|
135 |
throw new DateTimeException("Invalid date '" + month + " " + dayOfMonth + "'");
|
|
136 |
}
|
|
137 |
}
|
|
138 |
return new LocalDate(year, month, dayOfMonth);
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Constructor, previously validated.
|
|
143 |
*
|
|
144 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
|
|
145 |
* @param month the month-of-year to represent, not null
|
|
146 |
* @param dayOfMonth the day-of-month to represent, valid for year-month, from 1 to 31
|
|
147 |
*/
|
|
148 |
private LocalDate(int year, int month, int dayOfMonth) {
|
|
149 |
this.year = year;
|
|
150 |
this.month = (short) month;
|
|
151 |
this.day = (short) dayOfMonth;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Gets the year field.
|
|
156 |
* <p>
|
|
157 |
* This method returns the primitive {@code int} value for the year.
|
|
158 |
* <p>
|
|
159 |
* The year returned by this method is proleptic as per {@code get(YEAR)}.
|
|
160 |
* To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.
|
|
161 |
*
|
|
162 |
* @return the year, from MIN_YEAR to MAX_YEAR
|
|
163 |
*/
|
|
164 |
public int getYear() {
|
|
165 |
return year;
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Gets the month-of-year field as an int from 1 to 12.
|
|
170 |
*
|
|
171 |
* @return the month-of-year
|
|
172 |
*/
|
|
173 |
public int getMonth() {
|
|
174 |
return month;
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Gets the day-of-month field.
|
|
179 |
* <p>
|
|
180 |
* This method returns the primitive {@code int} value for the day-of-month.
|
|
181 |
*
|
|
182 |
* @return the day-of-month, from 1 to 31
|
|
183 |
*/
|
|
184 |
public int getDayOfMonth() {
|
|
185 |
return day;
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Gets the day-of-week field, which is an int from 1 to 7.
|
|
190 |
*
|
|
191 |
* @return the day-of-week
|
|
192 |
*/
|
|
193 |
public int getDayOfWeek() {
|
|
194 |
return (int)floorMod(toEpochDay() + 3, 7) + 1;
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Returns a copy of this {@code LocalDate} with the specified number of days added.
|
|
199 |
* <p>
|
|
200 |
* This method adds the specified amount to the days field incrementing the
|
|
201 |
* month and year fields as necessary to ensure the result remains valid.
|
|
202 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
203 |
* <p>
|
|
204 |
* For example, 2008-12-31 plus one day would result in 2009-01-01.
|
|
205 |
* <p>
|
|
206 |
* This instance is immutable and unaffected by this method call.
|
|
207 |
*
|
|
208 |
* @param daysToAdd the days to add, may be negative
|
|
209 |
* @return a {@code LocalDate} based on this date with the days added, not null
|
|
210 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
211 |
*/
|
|
212 |
public LocalDate plusDays(long daysToAdd) {
|
|
213 |
if (daysToAdd == 0) {
|
|
214 |
return this;
|
|
215 |
}
|
|
216 |
long mjDay = addExact(toEpochDay(), daysToAdd);
|
|
217 |
return LocalDate.ofEpochDay(mjDay);
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Returns a copy of this {@code LocalDate} with the specified number of days subtracted.
|
|
222 |
* <p>
|
|
223 |
* This method subtracts the specified amount from the days field decrementing the
|
|
224 |
* month and year fields as necessary to ensure the result remains valid.
|
|
225 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
226 |
* <p>
|
|
227 |
* For example, 2009-01-01 minus one day would result in 2008-12-31.
|
|
228 |
* <p>
|
|
229 |
* This instance is immutable and unaffected by this method call.
|
|
230 |
*
|
|
231 |
* @param daysToSubtract the days to subtract, may be negative
|
|
232 |
* @return a {@code LocalDate} based on this date with the days subtracted, not null
|
|
233 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
234 |
*/
|
|
235 |
public LocalDate minusDays(long daysToSubtract) {
|
|
236 |
return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
|
|
237 |
}
|
|
238 |
|
|
239 |
/**
|
|
240 |
* Obtains an instance of {@code LocalDate} from the epoch day count.
|
|
241 |
* <p>
|
|
242 |
* The Epoch Day count is a simple incrementing count of days
|
|
243 |
* where day 0 is 1970-01-01. Negative numbers represent earlier days.
|
|
244 |
*
|
|
245 |
* @param epochDay the Epoch Day to convert, based on the epoch 1970-01-01
|
|
246 |
* @return the local date, not null
|
|
247 |
* @throws DateTimeException if the epoch days exceeds the supported date range
|
|
248 |
*/
|
|
249 |
public static LocalDate ofEpochDay(long epochDay) {
|
|
250 |
long zeroDay = epochDay + DAYS_0000_TO_1970;
|
|
251 |
// find the march-based year
|
|
252 |
zeroDay -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle
|
|
253 |
long adjust = 0;
|
|
254 |
if (zeroDay < 0) {
|
|
255 |
// adjust negative years to positive for calculation
|
|
256 |
long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
|
|
257 |
adjust = adjustCycles * 400;
|
|
258 |
zeroDay += -adjustCycles * DAYS_PER_CYCLE;
|
|
259 |
}
|
|
260 |
long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
|
|
261 |
long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
|
|
262 |
if (doyEst < 0) {
|
|
263 |
// fix estimate
|
|
264 |
yearEst--;
|
|
265 |
doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
|
|
266 |
}
|
|
267 |
yearEst += adjust; // reset any negative year
|
|
268 |
int marchDoy0 = (int) doyEst;
|
|
269 |
|
|
270 |
// convert march-based values back to january-based
|
|
271 |
int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
|
|
272 |
int month = (marchMonth0 + 2) % 12 + 1;
|
|
273 |
int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
|
|
274 |
yearEst += marchMonth0 / 10;
|
|
275 |
|
|
276 |
// check year now we are certain it is correct
|
|
277 |
int year = YEAR.checkValidValue((int)yearEst);
|
|
278 |
return new LocalDate(year, month, dom);
|
|
279 |
}
|
|
280 |
|
|
281 |
public long toEpochDay() {
|
|
282 |
long y = year;
|
|
283 |
long m = month;
|
|
284 |
long total = 0;
|
|
285 |
total += 365 * y;
|
|
286 |
if (y >= 0) {
|
|
287 |
total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;
|
|
288 |
} else {
|
|
289 |
total -= y / -4 - y / -100 + y / -400;
|
|
290 |
}
|
|
291 |
total += ((367 * m - 362) / 12);
|
|
292 |
total += day - 1;
|
|
293 |
if (m > 2) {
|
|
294 |
total--;
|
|
295 |
if (isLeapYear(year) == false) {
|
|
296 |
total--;
|
|
297 |
}
|
|
298 |
}
|
|
299 |
return total - DAYS_0000_TO_1970;
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* Compares this date to another date.
|
|
304 |
* <p>
|
|
305 |
* The comparison is primarily based on the date, from earliest to latest.
|
|
306 |
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
307 |
* <p>
|
|
308 |
* If all the dates being compared are instances of {@code LocalDate},
|
|
309 |
* then the comparison will be entirely based on the date.
|
|
310 |
* If some dates being compared are in different chronologies, then the
|
|
311 |
* chronology is also considered, see {@link java.time.temporal.ChronoLocalDate#compareTo}.
|
|
312 |
*
|
|
313 |
* @param other the other date to compare to, not null
|
|
314 |
* @return the comparator value, negative if less, positive if greater
|
|
315 |
*/
|
|
316 |
public int compareTo(LocalDate otherDate) {
|
|
317 |
int cmp = (year - otherDate.year);
|
|
318 |
if (cmp == 0) {
|
|
319 |
cmp = (month - otherDate.month);
|
|
320 |
if (cmp == 0) {
|
|
321 |
cmp = (day - otherDate.day);
|
|
322 |
}
|
|
323 |
}
|
|
324 |
return cmp;
|
|
325 |
}
|
|
326 |
|
|
327 |
/**
|
|
328 |
* Checks if this date is equal to another date.
|
|
329 |
* <p>
|
|
330 |
* Compares this {@code LocalDate} with another ensuring that the date is the same.
|
|
331 |
* <p>
|
|
332 |
* Only objects of type {@code LocalDate} are compared, other types return false.
|
|
333 |
* To compare the dates of two {@code TemporalAccessor} instances, including dates
|
|
334 |
* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
|
|
335 |
*
|
|
336 |
* @param obj the object to check, null returns false
|
|
337 |
* @return true if this is equal to the other date
|
|
338 |
*/
|
|
339 |
@Override
|
|
340 |
public boolean equals(Object obj) {
|
|
341 |
if (this == obj) {
|
|
342 |
return true;
|
|
343 |
}
|
|
344 |
if (obj instanceof LocalDate) {
|
|
345 |
return compareTo((LocalDate) obj) == 0;
|
|
346 |
}
|
|
347 |
return false;
|
|
348 |
}
|
|
349 |
|
|
350 |
/**
|
|
351 |
* A hash code for this date.
|
|
352 |
*
|
|
353 |
* @return a suitable hash code
|
|
354 |
*/
|
|
355 |
@Override
|
|
356 |
public int hashCode() {
|
|
357 |
int yearValue = year;
|
|
358 |
int monthValue = month;
|
|
359 |
int dayValue = day;
|
|
360 |
return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
|
|
361 |
}
|
|
362 |
|
|
363 |
}
|