Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/temporal/IsoFields.java
38918 views
1
/*
2
* Copyright (c) 2012, 2018, 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
* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
28
*
29
* All rights reserved.
30
*
31
* Redistribution and use in source and binary forms, with or without
32
* modification, are permitted provided that the following conditions are met:
33
*
34
* * Redistributions of source code must retain the above copyright notice,
35
* this list of conditions and the following disclaimer.
36
*
37
* * Redistributions in binary form must reproduce the above copyright notice,
38
* this list of conditions and the following disclaimer in the documentation
39
* and/or other materials provided with the distribution.
40
*
41
* * Neither the name of JSR-310 nor the names of its contributors
42
* may be used to endorse or promote products derived from this software
43
* without specific prior written permission.
44
*
45
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56
*/
57
package java.time.temporal;
58
59
import static java.time.DayOfWeek.THURSDAY;
60
import static java.time.DayOfWeek.WEDNESDAY;
61
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
62
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
63
import static java.time.temporal.ChronoField.EPOCH_DAY;
64
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
65
import static java.time.temporal.ChronoField.YEAR;
66
import static java.time.temporal.ChronoUnit.DAYS;
67
import static java.time.temporal.ChronoUnit.FOREVER;
68
import static java.time.temporal.ChronoUnit.MONTHS;
69
import static java.time.temporal.ChronoUnit.WEEKS;
70
import static java.time.temporal.ChronoUnit.YEARS;
71
72
import java.time.DateTimeException;
73
import java.time.Duration;
74
import java.time.LocalDate;
75
import java.time.chrono.ChronoLocalDate;
76
import java.time.chrono.Chronology;
77
import java.time.chrono.IsoChronology;
78
import java.time.format.ResolverStyle;
79
import java.util.Locale;
80
import java.util.Map;
81
import java.util.Objects;
82
import java.util.ResourceBundle;
83
84
import sun.util.locale.provider.LocaleProviderAdapter;
85
import sun.util.locale.provider.LocaleResources;
86
87
/**
88
* Fields and units specific to the ISO-8601 calendar system,
89
* including quarter-of-year and week-based-year.
90
* <p>
91
* This class defines fields and units that are specific to the ISO calendar system.
92
*
93
* <h3>Quarter of year</h3>
94
* The ISO-8601 standard is based on the standard civic 12 month year.
95
* This is commonly divided into four quarters, often abbreviated as Q1, Q2, Q3 and Q4.
96
* <p>
97
* January, February and March are in Q1.
98
* April, May and June are in Q2.
99
* July, August and September are in Q3.
100
* October, November and December are in Q4.
101
* <p>
102
* The complete date is expressed using three fields:
103
* <ul>
104
* <li>{@link #DAY_OF_QUARTER DAY_OF_QUARTER} - the day within the quarter, from 1 to 90, 91 or 92
105
* <li>{@link #QUARTER_OF_YEAR QUARTER_OF_YEAR} - the week within the week-based-year
106
* <li>{@link ChronoField#YEAR YEAR} - the standard ISO year
107
* </ul>
108
*
109
* <h3>Week based years</h3>
110
* The ISO-8601 standard was originally intended as a data interchange format,
111
* defining a string format for dates and times. However, it also defines an
112
* alternate way of expressing the date, based on the concept of week-based-year.
113
* <p>
114
* The date is expressed using three fields:
115
* <ul>
116
* <li>{@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} - the standard field defining the
117
* day-of-week from Monday (1) to Sunday (7)
118
* <li>{@link #WEEK_OF_WEEK_BASED_YEAR} - the week within the week-based-year
119
* <li>{@link #WEEK_BASED_YEAR WEEK_BASED_YEAR} - the week-based-year
120
* </ul>
121
* The week-based-year itself is defined relative to the standard ISO proleptic year.
122
* It differs from the standard year in that it always starts on a Monday.
123
* <p>
124
* The first week of a week-based-year is the first Monday-based week of the standard
125
* ISO year that has at least 4 days in the new year.
126
* <ul>
127
* <li>If January 1st is Monday then week 1 starts on January 1st
128
* <li>If January 1st is Tuesday then week 1 starts on December 31st of the previous standard year
129
* <li>If January 1st is Wednesday then week 1 starts on December 30th of the previous standard year
130
* <li>If January 1st is Thursday then week 1 starts on December 29th of the previous standard year
131
* <li>If January 1st is Friday then week 1 starts on January 4th
132
* <li>If January 1st is Saturday then week 1 starts on January 3rd
133
* <li>If January 1st is Sunday then week 1 starts on January 2nd
134
* </ul>
135
* There are 52 weeks in most week-based years, however on occasion there are 53 weeks.
136
* <p>
137
* For example:
138
*
139
* <table cellpadding="0" cellspacing="3" border="0" style="text-align: left; width: 50%;">
140
* <caption>Examples of Week based Years</caption>
141
* <tr><th>Date</th><th>Day-of-week</th><th>Field values</th></tr>
142
* <tr><th>2008-12-28</th><td>Sunday</td><td>Week 52 of week-based-year 2008</td></tr>
143
* <tr><th>2008-12-29</th><td>Monday</td><td>Week 1 of week-based-year 2009</td></tr>
144
* <tr><th>2008-12-31</th><td>Wednesday</td><td>Week 1 of week-based-year 2009</td></tr>
145
* <tr><th>2009-01-01</th><td>Thursday</td><td>Week 1 of week-based-year 2009</td></tr>
146
* <tr><th>2009-01-04</th><td>Sunday</td><td>Week 1 of week-based-year 2009</td></tr>
147
* <tr><th>2009-01-05</th><td>Monday</td><td>Week 2 of week-based-year 2009</td></tr>
148
* </table>
149
*
150
* @implSpec
151
* <p>
152
* This class is immutable and thread-safe.
153
*
154
* @since 1.8
155
*/
156
public final class IsoFields {
157
158
/**
159
* The field that represents the day-of-quarter.
160
* <p>
161
* This field allows the day-of-quarter value to be queried and set.
162
* The day-of-quarter has values from 1 to 90 in Q1 of a standard year, from 1 to 91
163
* in Q1 of a leap year, from 1 to 91 in Q2 and from 1 to 92 in Q3 and Q4.
164
* <p>
165
* The day-of-quarter can only be calculated if the day-of-year, month-of-year and year
166
* are available.
167
* <p>
168
* When setting this field, the value is allowed to be partially lenient, taking any
169
* value from 1 to 92. If the quarter has less than 92 days, then day 92, and
170
* potentially day 91, is in the following quarter.
171
* <p>
172
* In the resolving phase of parsing, a date can be created from a year,
173
* quarter-of-year and day-of-quarter.
174
* <p>
175
* In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
176
* validated against their range of valid values. The day-of-quarter field
177
* is validated from 1 to 90, 91 or 92 depending on the year and quarter.
178
* <p>
179
* In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
180
* validated against their range of valid values. The day-of-quarter field is
181
* validated between 1 and 92, ignoring the actual range based on the year and quarter.
182
* If the day-of-quarter exceeds the actual range by one day, then the resulting date
183
* is one day later. If the day-of-quarter exceeds the actual range by two days,
184
* then the resulting date is two days later.
185
* <p>
186
* In {@linkplain ResolverStyle#LENIENT lenient mode}, only the year is validated
187
* against the range of valid values. The resulting date is calculated equivalent to
188
* the following three stage approach. First, create a date on the first of January
189
* in the requested year. Then take the quarter-of-year, subtract one, and add the
190
* amount in quarters to the date. Finally, take the day-of-quarter, subtract one,
191
* and add the amount in days to the date.
192
* <p>
193
* This unit is an immutable and thread-safe singleton.
194
*/
195
public static final TemporalField DAY_OF_QUARTER = Field.DAY_OF_QUARTER;
196
/**
197
* The field that represents the quarter-of-year.
198
* <p>
199
* This field allows the quarter-of-year value to be queried and set.
200
* The quarter-of-year has values from 1 to 4.
201
* <p>
202
* The quarter-of-year can only be calculated if the month-of-year is available.
203
* <p>
204
* In the resolving phase of parsing, a date can be created from a year,
205
* quarter-of-year and day-of-quarter.
206
* See {@link #DAY_OF_QUARTER} for details.
207
* <p>
208
* This unit is an immutable and thread-safe singleton.
209
*/
210
public static final TemporalField QUARTER_OF_YEAR = Field.QUARTER_OF_YEAR;
211
/**
212
* The field that represents the week-of-week-based-year.
213
* <p>
214
* This field allows the week of the week-based-year value to be queried and set.
215
* The week-of-week-based-year has values from 1 to 52, or 53 if the
216
* week-based-year has 53 weeks.
217
* <p>
218
* In the resolving phase of parsing, a date can be created from a
219
* week-based-year, week-of-week-based-year and day-of-week.
220
* <p>
221
* In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
222
* validated against their range of valid values. The week-of-week-based-year
223
* field is validated from 1 to 52 or 53 depending on the week-based-year.
224
* <p>
225
* In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
226
* validated against their range of valid values. The week-of-week-based-year
227
* field is validated between 1 and 53, ignoring the week-based-year.
228
* If the week-of-week-based-year is 53, but the week-based-year only has
229
* 52 weeks, then the resulting date is in week 1 of the following week-based-year.
230
* <p>
231
* In {@linkplain ResolverStyle#LENIENT lenient mode}, only the week-based-year
232
* is validated against the range of valid values. If the day-of-week is outside
233
* the range 1 to 7, then the resulting date is adjusted by a suitable number of
234
* weeks to reduce the day-of-week to the range 1 to 7. If the week-of-week-based-year
235
* value is outside the range 1 to 52, then any excess weeks are added or subtracted
236
* from the resulting date.
237
* <p>
238
* This unit is an immutable and thread-safe singleton.
239
*/
240
public static final TemporalField WEEK_OF_WEEK_BASED_YEAR = Field.WEEK_OF_WEEK_BASED_YEAR;
241
/**
242
* The field that represents the week-based-year.
243
* <p>
244
* This field allows the week-based-year value to be queried and set.
245
* <p>
246
* The field has a range that matches {@link LocalDate#MAX} and {@link LocalDate#MIN}.
247
* <p>
248
* In the resolving phase of parsing, a date can be created from a
249
* week-based-year, week-of-week-based-year and day-of-week.
250
* See {@link #WEEK_OF_WEEK_BASED_YEAR} for details.
251
* <p>
252
* This unit is an immutable and thread-safe singleton.
253
*/
254
public static final TemporalField WEEK_BASED_YEAR = Field.WEEK_BASED_YEAR;
255
/**
256
* The unit that represents week-based-years for the purpose of addition and subtraction.
257
* <p>
258
* This allows a number of week-based-years to be added to, or subtracted from, a date.
259
* The unit is equal to either 52 or 53 weeks.
260
* The estimated duration of a week-based-year is the same as that of a standard ISO
261
* year at {@code 365.2425 Days}.
262
* <p>
263
* The rules for addition add the number of week-based-years to the existing value
264
* for the week-based-year field. If the resulting week-based-year only has 52 weeks,
265
* then the date will be in week 1 of the following week-based-year.
266
* <p>
267
* This unit is an immutable and thread-safe singleton.
268
*/
269
public static final TemporalUnit WEEK_BASED_YEARS = Unit.WEEK_BASED_YEARS;
270
/**
271
* Unit that represents the concept of a quarter-year.
272
* For the ISO calendar system, it is equal to 3 months.
273
* The estimated duration of a quarter-year is one quarter of {@code 365.2425 Days}.
274
* <p>
275
* This unit is an immutable and thread-safe singleton.
276
*/
277
public static final TemporalUnit QUARTER_YEARS = Unit.QUARTER_YEARS;
278
279
/**
280
* Restricted constructor.
281
*/
282
private IsoFields() {
283
throw new AssertionError("Not instantiable");
284
}
285
286
//-----------------------------------------------------------------------
287
/**
288
* Implementation of the field.
289
*/
290
private static enum Field implements TemporalField {
291
DAY_OF_QUARTER {
292
@Override
293
public TemporalUnit getBaseUnit() {
294
return DAYS;
295
}
296
@Override
297
public TemporalUnit getRangeUnit() {
298
return QUARTER_YEARS;
299
}
300
@Override
301
public ValueRange range() {
302
return ValueRange.of(1, 90, 92);
303
}
304
@Override
305
public boolean isSupportedBy(TemporalAccessor temporal) {
306
return temporal.isSupported(DAY_OF_YEAR) && temporal.isSupported(MONTH_OF_YEAR) &&
307
temporal.isSupported(YEAR) && isIso(temporal);
308
}
309
@Override
310
public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
311
if (isSupportedBy(temporal) == false) {
312
throw new UnsupportedTemporalTypeException("Unsupported field: DayOfQuarter");
313
}
314
long qoy = temporal.getLong(QUARTER_OF_YEAR);
315
if (qoy == 1) {
316
long year = temporal.getLong(YEAR);
317
return (IsoChronology.INSTANCE.isLeapYear(year) ? ValueRange.of(1, 91) : ValueRange.of(1, 90));
318
} else if (qoy == 2) {
319
return ValueRange.of(1, 91);
320
} else if (qoy == 3 || qoy == 4) {
321
return ValueRange.of(1, 92);
322
} // else value not from 1 to 4, so drop through
323
return range();
324
}
325
@Override
326
public long getFrom(TemporalAccessor temporal) {
327
if (isSupportedBy(temporal) == false) {
328
throw new UnsupportedTemporalTypeException("Unsupported field: DayOfQuarter");
329
}
330
int doy = temporal.get(DAY_OF_YEAR);
331
int moy = temporal.get(MONTH_OF_YEAR);
332
long year = temporal.getLong(YEAR);
333
return doy - QUARTER_DAYS[((moy - 1) / 3) + (IsoChronology.INSTANCE.isLeapYear(year) ? 4 : 0)];
334
}
335
@SuppressWarnings("unchecked")
336
@Override
337
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
338
// calls getFrom() to check if supported
339
long curValue = getFrom(temporal);
340
range().checkValidValue(newValue, this); // leniently check from 1 to 92 TODO: check
341
return (R) temporal.with(DAY_OF_YEAR, temporal.getLong(DAY_OF_YEAR) + (newValue - curValue));
342
}
343
@Override
344
public ChronoLocalDate resolve(
345
Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
346
Long yearLong = fieldValues.get(YEAR);
347
Long qoyLong = fieldValues.get(QUARTER_OF_YEAR);
348
if (yearLong == null || qoyLong == null) {
349
return null;
350
}
351
int y = YEAR.checkValidIntValue(yearLong); // always validate
352
long doq = fieldValues.get(DAY_OF_QUARTER);
353
ensureIso(partialTemporal);
354
LocalDate date;
355
if (resolverStyle == ResolverStyle.LENIENT) {
356
date = LocalDate.of(y, 1, 1).plusMonths(Math.multiplyExact(Math.subtractExact(qoyLong, 1), 3));
357
doq = Math.subtractExact(doq, 1);
358
} else {
359
int qoy = QUARTER_OF_YEAR.range().checkValidIntValue(qoyLong, QUARTER_OF_YEAR); // validated
360
date = LocalDate.of(y, ((qoy - 1) * 3) + 1, 1);
361
if (doq < 1 || doq > 90) {
362
if (resolverStyle == ResolverStyle.STRICT) {
363
rangeRefinedBy(date).checkValidValue(doq, this); // only allow exact range
364
} else { // SMART
365
range().checkValidValue(doq, this); // allow 1-92 rolling into next quarter
366
}
367
}
368
doq--;
369
}
370
fieldValues.remove(this);
371
fieldValues.remove(YEAR);
372
fieldValues.remove(QUARTER_OF_YEAR);
373
return date.plusDays(doq);
374
}
375
@Override
376
public String toString() {
377
return "DayOfQuarter";
378
}
379
},
380
QUARTER_OF_YEAR {
381
@Override
382
public TemporalUnit getBaseUnit() {
383
return QUARTER_YEARS;
384
}
385
@Override
386
public TemporalUnit getRangeUnit() {
387
return YEARS;
388
}
389
@Override
390
public ValueRange range() {
391
return ValueRange.of(1, 4);
392
}
393
@Override
394
public boolean isSupportedBy(TemporalAccessor temporal) {
395
return temporal.isSupported(MONTH_OF_YEAR) && isIso(temporal);
396
}
397
@Override
398
public long getFrom(TemporalAccessor temporal) {
399
if (isSupportedBy(temporal) == false) {
400
throw new UnsupportedTemporalTypeException("Unsupported field: QuarterOfYear");
401
}
402
long moy = temporal.getLong(MONTH_OF_YEAR);
403
return ((moy + 2) / 3);
404
}
405
@SuppressWarnings("unchecked")
406
@Override
407
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
408
// calls getFrom() to check if supported
409
long curValue = getFrom(temporal);
410
range().checkValidValue(newValue, this); // strictly check from 1 to 4
411
return (R) temporal.with(MONTH_OF_YEAR, temporal.getLong(MONTH_OF_YEAR) + (newValue - curValue) * 3);
412
}
413
@Override
414
public String toString() {
415
return "QuarterOfYear";
416
}
417
},
418
WEEK_OF_WEEK_BASED_YEAR {
419
@Override
420
public String getDisplayName(Locale locale) {
421
Objects.requireNonNull(locale, "locale");
422
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
423
.getLocaleResources(locale);
424
ResourceBundle rb = lr.getJavaTimeFormatData();
425
return rb.containsKey("field.week") ? rb.getString("field.week") : toString();
426
}
427
428
@Override
429
public TemporalUnit getBaseUnit() {
430
return WEEKS;
431
}
432
@Override
433
public TemporalUnit getRangeUnit() {
434
return WEEK_BASED_YEARS;
435
}
436
@Override
437
public ValueRange range() {
438
return ValueRange.of(1, 52, 53);
439
}
440
@Override
441
public boolean isSupportedBy(TemporalAccessor temporal) {
442
return temporal.isSupported(EPOCH_DAY) && isIso(temporal);
443
}
444
@Override
445
public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
446
if (isSupportedBy(temporal) == false) {
447
throw new UnsupportedTemporalTypeException("Unsupported field: WeekOfWeekBasedYear");
448
}
449
return getWeekRange(LocalDate.from(temporal));
450
}
451
@Override
452
public long getFrom(TemporalAccessor temporal) {
453
if (isSupportedBy(temporal) == false) {
454
throw new UnsupportedTemporalTypeException("Unsupported field: WeekOfWeekBasedYear");
455
}
456
return getWeek(LocalDate.from(temporal));
457
}
458
@SuppressWarnings("unchecked")
459
@Override
460
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
461
// calls getFrom() to check if supported
462
range().checkValidValue(newValue, this); // lenient range
463
return (R) temporal.plus(Math.subtractExact(newValue, getFrom(temporal)), WEEKS);
464
}
465
@Override
466
public ChronoLocalDate resolve(
467
Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
468
Long wbyLong = fieldValues.get(WEEK_BASED_YEAR);
469
Long dowLong = fieldValues.get(DAY_OF_WEEK);
470
if (wbyLong == null || dowLong == null) {
471
return null;
472
}
473
int wby = WEEK_BASED_YEAR.range().checkValidIntValue(wbyLong, WEEK_BASED_YEAR); // always validate
474
long wowby = fieldValues.get(WEEK_OF_WEEK_BASED_YEAR);
475
ensureIso(partialTemporal);
476
LocalDate date = LocalDate.of(wby, 1, 4);
477
if (resolverStyle == ResolverStyle.LENIENT) {
478
long dow = dowLong; // unvalidated
479
if (dow > 7) {
480
date = date.plusWeeks((dow - 1) / 7);
481
dow = ((dow - 1) % 7) + 1;
482
} else if (dow < 1) {
483
date = date.plusWeeks(Math.subtractExact(dow, 7) / 7);
484
dow = ((dow + 6) % 7) + 1;
485
}
486
date = date.plusWeeks(Math.subtractExact(wowby, 1)).with(DAY_OF_WEEK, dow);
487
} else {
488
int dow = DAY_OF_WEEK.checkValidIntValue(dowLong); // validated
489
if (wowby < 1 || wowby > 52) {
490
if (resolverStyle == ResolverStyle.STRICT) {
491
getWeekRange(date).checkValidValue(wowby, this); // only allow exact range
492
} else { // SMART
493
range().checkValidValue(wowby, this); // allow 1-53 rolling into next year
494
}
495
}
496
date = date.plusWeeks(wowby - 1).with(DAY_OF_WEEK, dow);
497
}
498
fieldValues.remove(this);
499
fieldValues.remove(WEEK_BASED_YEAR);
500
fieldValues.remove(DAY_OF_WEEK);
501
return date;
502
}
503
@Override
504
public String toString() {
505
return "WeekOfWeekBasedYear";
506
}
507
},
508
WEEK_BASED_YEAR {
509
@Override
510
public TemporalUnit getBaseUnit() {
511
return WEEK_BASED_YEARS;
512
}
513
@Override
514
public TemporalUnit getRangeUnit() {
515
return FOREVER;
516
}
517
@Override
518
public ValueRange range() {
519
return YEAR.range();
520
}
521
@Override
522
public boolean isSupportedBy(TemporalAccessor temporal) {
523
return temporal.isSupported(EPOCH_DAY) && isIso(temporal);
524
}
525
@Override
526
public long getFrom(TemporalAccessor temporal) {
527
if (isSupportedBy(temporal) == false) {
528
throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
529
}
530
return getWeekBasedYear(LocalDate.from(temporal));
531
}
532
@SuppressWarnings("unchecked")
533
@Override
534
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
535
if (isSupportedBy(temporal) == false) {
536
throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
537
}
538
int newWby = range().checkValidIntValue(newValue, WEEK_BASED_YEAR); // strict check
539
LocalDate date = LocalDate.from(temporal);
540
int dow = date.get(DAY_OF_WEEK);
541
int week = getWeek(date);
542
if (week == 53 && getWeekRange(newWby) == 52) {
543
week = 52;
544
}
545
LocalDate resolved = LocalDate.of(newWby, 1, 4); // 4th is guaranteed to be in week one
546
int days = (dow - resolved.get(DAY_OF_WEEK)) + ((week - 1) * 7);
547
resolved = resolved.plusDays(days);
548
return (R) temporal.with(resolved);
549
}
550
@Override
551
public String toString() {
552
return "WeekBasedYear";
553
}
554
};
555
556
@Override
557
public boolean isDateBased() {
558
return true;
559
}
560
561
@Override
562
public boolean isTimeBased() {
563
return false;
564
}
565
566
@Override
567
public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
568
return range();
569
}
570
571
//-------------------------------------------------------------------------
572
private static final int[] QUARTER_DAYS = {0, 90, 181, 273, 0, 91, 182, 274};
573
574
private static boolean isIso(TemporalAccessor temporal) {
575
return Chronology.from(temporal).equals(IsoChronology.INSTANCE);
576
}
577
578
private static void ensureIso(TemporalAccessor temporal) {
579
if (isIso(temporal) == false) {
580
throw new DateTimeException("Resolve requires IsoChronology");
581
}
582
}
583
584
private static ValueRange getWeekRange(LocalDate date) {
585
int wby = getWeekBasedYear(date);
586
return ValueRange.of(1, getWeekRange(wby));
587
}
588
589
private static int getWeekRange(int wby) {
590
LocalDate date = LocalDate.of(wby, 1, 1);
591
// 53 weeks if standard year starts on Thursday, or Wed in a leap year
592
if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
593
return 53;
594
}
595
return 52;
596
}
597
598
private static int getWeek(LocalDate date) {
599
int dow0 = date.getDayOfWeek().ordinal();
600
int doy0 = date.getDayOfYear() - 1;
601
int doyThu0 = doy0 + (3 - dow0); // adjust to mid-week Thursday (which is 3 indexed from zero)
602
int alignedWeek = doyThu0 / 7;
603
int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
604
int firstMonDoy0 = firstThuDoy0 - 3;
605
if (firstMonDoy0 < -3) {
606
firstMonDoy0 += 7;
607
}
608
if (doy0 < firstMonDoy0) {
609
return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
610
}
611
int week = ((doy0 - firstMonDoy0) / 7) + 1;
612
if (week == 53) {
613
if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
614
week = 1;
615
}
616
}
617
return week;
618
}
619
620
private static int getWeekBasedYear(LocalDate date) {
621
int year = date.getYear();
622
int doy = date.getDayOfYear();
623
if (doy <= 3) {
624
int dow = date.getDayOfWeek().ordinal();
625
if (doy - dow < -2) {
626
year--;
627
}
628
} else if (doy >= 363) {
629
int dow = date.getDayOfWeek().ordinal();
630
doy = doy - 363 - (date.isLeapYear() ? 1 : 0);
631
if (doy - dow >= 0) {
632
year++;
633
}
634
}
635
return year;
636
}
637
}
638
639
//-----------------------------------------------------------------------
640
/**
641
* Implementation of the unit.
642
*/
643
private static enum Unit implements TemporalUnit {
644
645
/**
646
* Unit that represents the concept of a week-based-year.
647
*/
648
WEEK_BASED_YEARS("WeekBasedYears", Duration.ofSeconds(31556952L)),
649
/**
650
* Unit that represents the concept of a quarter-year.
651
*/
652
QUARTER_YEARS("QuarterYears", Duration.ofSeconds(31556952L / 4));
653
654
private final String name;
655
private final Duration duration;
656
657
private Unit(String name, Duration estimatedDuration) {
658
this.name = name;
659
this.duration = estimatedDuration;
660
}
661
662
@Override
663
public Duration getDuration() {
664
return duration;
665
}
666
667
@Override
668
public boolean isDurationEstimated() {
669
return true;
670
}
671
672
@Override
673
public boolean isDateBased() {
674
return true;
675
}
676
677
@Override
678
public boolean isTimeBased() {
679
return false;
680
}
681
682
@Override
683
public boolean isSupportedBy(Temporal temporal) {
684
return temporal.isSupported(EPOCH_DAY);
685
}
686
687
@SuppressWarnings("unchecked")
688
@Override
689
public <R extends Temporal> R addTo(R temporal, long amount) {
690
switch (this) {
691
case WEEK_BASED_YEARS:
692
return (R) temporal.with(WEEK_BASED_YEAR,
693
Math.addExact(temporal.get(WEEK_BASED_YEAR), amount));
694
case QUARTER_YEARS:
695
return (R) temporal.plus(amount / 4, YEARS)
696
.plus((amount % 4) * 3, MONTHS);
697
default:
698
throw new IllegalStateException("Unreachable");
699
}
700
}
701
702
@Override
703
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
704
if (temporal1Inclusive.getClass() != temporal2Exclusive.getClass()) {
705
return temporal1Inclusive.until(temporal2Exclusive, this);
706
}
707
switch(this) {
708
case WEEK_BASED_YEARS:
709
return Math.subtractExact(temporal2Exclusive.getLong(WEEK_BASED_YEAR),
710
temporal1Inclusive.getLong(WEEK_BASED_YEAR));
711
case QUARTER_YEARS:
712
return temporal1Inclusive.until(temporal2Exclusive, MONTHS) / 3;
713
default:
714
throw new IllegalStateException("Unreachable");
715
}
716
}
717
718
@Override
719
public String toString() {
720
return name;
721
}
722
}
723
}
724
725