@js-joda/extra 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/js-joda-extra.esm.js +1991 -22
- package/dist/js-joda-extra.esm.js.map +1 -1
- package/dist/js-joda-extra.js +1994 -20
- package/dist/js-joda-extra.js.map +1 -1
- package/dist/js-joda-extra.min.js +2 -2
- package/package.json +3 -3
- package/src/DayOfMonth.js +54 -54
- package/src/DayOfYear.js +48 -49
- package/src/Interval.js +93 -99
- package/src/LocalDateRange.js +750 -0
- package/src/OffsetDate.js +1279 -0
- package/src/Quarter.js +587 -0
- package/src/YearQuarter.js +1177 -0
- package/src/YearWeek.js +1026 -0
- package/src/_init.js +10 -0
- package/src/assert.js +2 -2
- package/src/js-joda-extra.js +10 -0
- package/src/plug.js +10 -0
- package/typings/js-joda-extra.d.ts +234 -2
package/src/YearWeek.js
ADDED
|
@@ -0,0 +1,1026 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @copyright (c) 2022, Philipp Thürwächter & Pattrick Hüper & Michał Sobkiewicz
|
|
3
|
+
* @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
|
|
4
|
+
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ChronoField, ChronoUnit, Clock, DateTimeException, DateTimeFormatterBuilder, DayOfWeek, IllegalArgumentException, IsoChronology, IsoFields, LocalDate, NullPointerException, SignStyle, Temporal, TemporalField, TemporalQueries, TemporalQuery, TemporalUnit, UnsupportedTemporalTypeException, ValueRange, Year, ZoneId } from '@js-joda/core';
|
|
8
|
+
|
|
9
|
+
// TODO: hm... is this a good idea?? copied from joda currently, could we add a js-joda-utils module??
|
|
10
|
+
import { assert, requireInstance, requireNonNull } from './assert';
|
|
11
|
+
import { _ as jodaInternal } from '@js-joda/core';
|
|
12
|
+
|
|
13
|
+
const MathUtil = jodaInternal.MathUtil;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A year-week in the ISO week date system such as `2015-W13`
|
|
17
|
+
*
|
|
18
|
+
* {@link YearWeek} is an immutable date-time object that represents the combination
|
|
19
|
+
* of a week-based-year and week-of-week-based-year.
|
|
20
|
+
* Any field that can be derived from those two fields can be obtained.
|
|
21
|
+
*
|
|
22
|
+
* This class does not store or represent a day, time or time-zone.
|
|
23
|
+
* For example, the value '13th week of 2007' can be stored in a {@link YearWeek}.
|
|
24
|
+
*
|
|
25
|
+
* The ISO-8601 calendar system is the modern civil calendar system used today
|
|
26
|
+
* in most of the world. It is equivalent to the proleptic Gregorian calendar
|
|
27
|
+
* system, in which today's rules for leap years are applied for all time.
|
|
28
|
+
* For most applications written today, the ISO-8601 rules are entirely suitable.
|
|
29
|
+
* However, any application that makes use of historical dates, and requires them
|
|
30
|
+
* to be accurate will find the ISO-8601 approach unsuitable.
|
|
31
|
+
*
|
|
32
|
+
* ISO-8601 defines the week as always starting with Monday.
|
|
33
|
+
* The first week is the week which contains the first Thursday of the calendar year.
|
|
34
|
+
* As such, the week-based-year used in this class does not align with the calendar year.
|
|
35
|
+
*/
|
|
36
|
+
export class YearWeek extends Temporal {
|
|
37
|
+
/**
|
|
38
|
+
* Function overloading for {@link YearWeek.now}:
|
|
39
|
+
* - if called with no arguments, {@link YearWeek._now0} is executed;
|
|
40
|
+
* - if called with an instance of {@link ZoneId}, then {@link YearWeek._nowZoneId} is executed;
|
|
41
|
+
* - if called with an instance of {@link Clock}, then {@link YearWeek._nowClock} is executed;
|
|
42
|
+
* - otherwise {@link IllegalArgumentException} is thrown.
|
|
43
|
+
*
|
|
44
|
+
* @param {?(ZoneId|Clock)} zoneIdOrClock
|
|
45
|
+
* @return {YearWeek}
|
|
46
|
+
*/
|
|
47
|
+
static now(zoneIdOrClock) {
|
|
48
|
+
switch (arguments.length) {
|
|
49
|
+
case 0:
|
|
50
|
+
return YearWeek._now0();
|
|
51
|
+
case 1:
|
|
52
|
+
requireNonNull(zoneIdOrClock, 'clockOrZone');
|
|
53
|
+
if (zoneIdOrClock instanceof ZoneId) {
|
|
54
|
+
return YearWeek._nowZoneId(zoneIdOrClock);
|
|
55
|
+
}
|
|
56
|
+
if (zoneIdOrClock instanceof Clock) {
|
|
57
|
+
return YearWeek._nowClock(zoneIdOrClock);
|
|
58
|
+
}
|
|
59
|
+
throw new IllegalArgumentException(`zoneIdOrClock must be an instance of ZoneId or Clock, but is ${zoneIdOrClock.constructor.name}`);
|
|
60
|
+
default:
|
|
61
|
+
throw new IllegalArgumentException(`Invalid number of arguments: ${arguments.length}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//-----------------------------------------------------------------------
|
|
66
|
+
/**
|
|
67
|
+
* Obtains the current year-week from the system clock in the default time-zone.
|
|
68
|
+
*
|
|
69
|
+
* This will query the {@link Clock.systemDefaultZone} system clock in the default
|
|
70
|
+
* time-zone to obtain the current year-week.
|
|
71
|
+
* The zone and offset will be set based on the time-zone in the clock.
|
|
72
|
+
*
|
|
73
|
+
* Using this method will prevent the ability to use an alternate clock for testing
|
|
74
|
+
* because the clock is hard-coded.
|
|
75
|
+
*
|
|
76
|
+
* @return {YearWeek} the current year-week using the system clock and default time-zone, not null
|
|
77
|
+
* @protected
|
|
78
|
+
*/
|
|
79
|
+
static _now0() {
|
|
80
|
+
return YearWeek.now(Clock.systemDefaultZone());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Obtains the current year-week from the system clock in the specified time-zone.
|
|
85
|
+
*
|
|
86
|
+
* This will query the {@link Clock.system} system clock to obtain the current year-week.
|
|
87
|
+
* Specifying the time-zone avoids dependence on the default time-zone.
|
|
88
|
+
*
|
|
89
|
+
* Using this method will prevent the ability to use an alternate clock for testing
|
|
90
|
+
* because the clock is hard-coded.
|
|
91
|
+
*
|
|
92
|
+
* @param {ZoneId} zone - the zone ID to use, not null
|
|
93
|
+
* @return {YearWeek} the current year-week using the system clock, not null
|
|
94
|
+
* @protected
|
|
95
|
+
*/
|
|
96
|
+
static _nowZoneId(zone) {
|
|
97
|
+
return YearWeek.now(Clock.system(zone));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Obtains the current year-week from the specified clock.
|
|
102
|
+
*
|
|
103
|
+
* This will query the specified clock to obtain the current year-week.
|
|
104
|
+
* Using this method allows the use of an alternate clock for testing.
|
|
105
|
+
* The alternate clock may be introduced using {@link Clock} dependency injection.
|
|
106
|
+
*
|
|
107
|
+
* @param {Clock} clock - the clock to use, not null
|
|
108
|
+
* @return {YearWeek} the current year-week, not null
|
|
109
|
+
* @protected
|
|
110
|
+
*/
|
|
111
|
+
static _nowClock(clock) {
|
|
112
|
+
const now = LocalDate.now(clock); // called once
|
|
113
|
+
return YearWeek.of(now.get(IsoFields.WEEK_BASED_YEAR), now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//-----------------------------------------------------------------------
|
|
117
|
+
/**
|
|
118
|
+
* Function overloading for {@link YearWeek.of}:
|
|
119
|
+
* - if called with an instances of {@link Year} and `int`, then {@link YearWeek._ofYearWeek} is executed;
|
|
120
|
+
* - if called with an instances of number and `int`, then {@link YearWeek._ofWeekBasedYear} is executed;
|
|
121
|
+
* - otherwise {@link IllegalArgumentException} is thrown.
|
|
122
|
+
*
|
|
123
|
+
* @param {Year|number} year - the year to represent, not null
|
|
124
|
+
* @param {number} week - the week-of-week-based-year to represent, from 1 to 53
|
|
125
|
+
* @return {YearWeek} the year-week, not null
|
|
126
|
+
*/
|
|
127
|
+
static of(year, week) {
|
|
128
|
+
MathUtil.verifyInt(week);
|
|
129
|
+
if (year instanceof Year) {
|
|
130
|
+
return this._ofYearWeek(year, week);
|
|
131
|
+
} else {
|
|
132
|
+
MathUtil.verifyInt(year);
|
|
133
|
+
return this._ofWeekBasedYear(year, week);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Obtains an instance of {@link YearWeek} from a year and week.
|
|
139
|
+
*
|
|
140
|
+
* If the week is 53 and the year does not have 53 weeks, week one of the following
|
|
141
|
+
* year is selected.
|
|
142
|
+
*
|
|
143
|
+
* Note that this class is based on the week-based-year which aligns to Monday to Sunday weeks,
|
|
144
|
+
* whereas {@link Year} is intended to represent standard years aligned from January to December.
|
|
145
|
+
* This difference may be seen at the start and/or end of the year.
|
|
146
|
+
* This method treats the standard year as though it is the week-based-year.
|
|
147
|
+
* Thus, `YearWeek.of(Year.of(2020), 1)` creates an object where Monday and Tuesday of the week
|
|
148
|
+
* are actually the last two days of 2019.
|
|
149
|
+
*
|
|
150
|
+
* @param {Year} year - the year to represent, not null
|
|
151
|
+
* @param {number} week - the week-of-week-based-year to represent, from 1 to 53
|
|
152
|
+
* @return {YearWeek} the year-week, not null
|
|
153
|
+
* @throws {DateTimeException} if the week value is invalid
|
|
154
|
+
* @protected
|
|
155
|
+
*/
|
|
156
|
+
static _ofYearWeek(year, week) {
|
|
157
|
+
const weekBasedYear = year.value();
|
|
158
|
+
return this._ofWeekBasedYear(weekBasedYear, week);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Obtains an instance of {@link YearWeek} from a week-based-year and week.
|
|
163
|
+
*
|
|
164
|
+
* If the week is 53 and the year does not have 53 weeks, week one of the following
|
|
165
|
+
* year is selected.
|
|
166
|
+
*
|
|
167
|
+
* @param {number} weekBasedYear - the week-based-year to represent, from MIN_YEAR to MAX_YEAR
|
|
168
|
+
* @param {number} week - the week-of-week-based-year to represent, from 1 to 53
|
|
169
|
+
* @return {YearWeek} the year-week, not null
|
|
170
|
+
* @throws {DateTimeException} if either field is invalid
|
|
171
|
+
* @protected
|
|
172
|
+
*/
|
|
173
|
+
static _ofWeekBasedYear(weekBasedYear, week) {
|
|
174
|
+
IsoFields.WEEK_BASED_YEAR.range().checkValidValue(weekBasedYear, IsoFields.WEEK_BASED_YEAR);
|
|
175
|
+
IsoFields.WEEK_OF_WEEK_BASED_YEAR.range().checkValidValue(week, IsoFields.WEEK_OF_WEEK_BASED_YEAR);
|
|
176
|
+
if (week === 53 && YearWeek._weekRange(weekBasedYear) < 53) {
|
|
177
|
+
week = 1;
|
|
178
|
+
weekBasedYear++;
|
|
179
|
+
IsoFields.WEEK_BASED_YEAR.range().checkValidValue(weekBasedYear, IsoFields.WEEK_BASED_YEAR);
|
|
180
|
+
}
|
|
181
|
+
return new YearWeek(weekBasedYear, week);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* from IsoFields in ThreeTen-Backport
|
|
186
|
+
* @private
|
|
187
|
+
*/
|
|
188
|
+
static _weekRange(weekBasedYear) {
|
|
189
|
+
const date = LocalDate.of(weekBasedYear, 1, 1);
|
|
190
|
+
// 53 weeks if year starts on Thursday, or Wed in a leap year
|
|
191
|
+
if (date.dayOfWeek() === DayOfWeek.THURSDAY || (date.dayOfWeek() === DayOfWeek.WEDNESDAY && date.isLeapYear())) {
|
|
192
|
+
return 53;
|
|
193
|
+
}
|
|
194
|
+
return 52;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//-----------------------------------------------------------------------
|
|
198
|
+
/**
|
|
199
|
+
* Obtains an instance of {@link YearWeek} from a temporal object.
|
|
200
|
+
*
|
|
201
|
+
* This obtains a year-week based on the specified temporal.
|
|
202
|
+
* A {@link TemporalAccessor} represents an arbitrary set of date and time information,
|
|
203
|
+
* which this factory converts to an instance of {@link YearWeek}.
|
|
204
|
+
*
|
|
205
|
+
* The conversion extracts the {@link IsoFields.WEEK_BASED_YEAR} WEEK_BASED_YEAR and
|
|
206
|
+
* {@link IsoFields.WEEK_OF_WEEK_BASED_YEAR} WEEK_OF_WEEK_BASED_YEAR fields.
|
|
207
|
+
* The extraction is only permitted if the temporal object has an ISO
|
|
208
|
+
* chronology, or can be converted to a {@link LocalDate}.
|
|
209
|
+
*
|
|
210
|
+
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
211
|
+
* allowing it to be used in queries via method reference, {@link YearWeek.FROM}.
|
|
212
|
+
*
|
|
213
|
+
* @param {Temporal} temporal - the temporal object to convert, not null
|
|
214
|
+
* @return {YearWeek} the year-week, not null
|
|
215
|
+
* @throws {DateTimeException} if unable to convert to a {@link YearWeek}
|
|
216
|
+
*/
|
|
217
|
+
static from(temporal) {
|
|
218
|
+
if (temporal instanceof YearWeek) {
|
|
219
|
+
return temporal;
|
|
220
|
+
}
|
|
221
|
+
requireNonNull(temporal, 'temporal');
|
|
222
|
+
try {
|
|
223
|
+
/* TODO: only IsoChronology for now
|
|
224
|
+
if (!IsoChronology.INSTANCE.equals(Chronology.from(temporal))) {
|
|
225
|
+
temporal = LocalDate.from(temporal);
|
|
226
|
+
}*/
|
|
227
|
+
// need to use getLong() as JDK Parsed class get() doesn't work properly
|
|
228
|
+
const year = MathUtil.safeToInt(temporal.getLong(IsoFields.WEEK_BASED_YEAR));
|
|
229
|
+
const week = MathUtil.safeToInt(temporal.getLong(IsoFields.WEEK_OF_WEEK_BASED_YEAR));
|
|
230
|
+
return YearWeek.of(year, week);
|
|
231
|
+
} catch (ex) {
|
|
232
|
+
throw new DateTimeException(`Unable to obtain YearWeek from TemporalAccessor: ${temporal.constructor.name}`, ex);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
//-----------------------------------------------------------------------
|
|
237
|
+
/**
|
|
238
|
+
* Obtains an instance of {@link YearWeek} from a text string using a specific formatter.
|
|
239
|
+
*
|
|
240
|
+
* The text is parsed using the formatter, returning a year-week.
|
|
241
|
+
*
|
|
242
|
+
* @param {string} text - the text to parse, not null
|
|
243
|
+
* @param {DateTimeFormatter} [formatter=YearWeek.PARSER] - the formatter to use, default is
|
|
244
|
+
* {@link YearWeek.PARSER}
|
|
245
|
+
*
|
|
246
|
+
* @return {YearWeek} the parsed year-week, not null
|
|
247
|
+
* @throws {DateTimeParseException} if the text cannot be parsed
|
|
248
|
+
*/
|
|
249
|
+
static parse(text, formatter = YearWeek.PARSER) {
|
|
250
|
+
assert(formatter != null, 'formatter', NullPointerException);
|
|
251
|
+
return formatter.parse(text, YearWeek.FROM);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
//-----------------------------------------------------------------------
|
|
255
|
+
/**
|
|
256
|
+
* Constructor.
|
|
257
|
+
*
|
|
258
|
+
* @param {number} weekBasedYear - the week-based-year to represent, validated from MIN_YEAR to MAX_YEAR
|
|
259
|
+
* @param {number} week - the week to represent, validated
|
|
260
|
+
* @private
|
|
261
|
+
*/
|
|
262
|
+
constructor(weekBasedYear, week) {
|
|
263
|
+
super();
|
|
264
|
+
this._year = weekBasedYear;
|
|
265
|
+
this._week = week;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* function overloading for {@link YearWeek.isSupported}
|
|
270
|
+
* - if called with an instance of {@link TemporalField}, then {@link YearWeek._isSupportedField} is executed,
|
|
271
|
+
* - if called with an instance of {@link TemporalUnit}, then {@link YearWeek._isSupportedUnit} is executed,
|
|
272
|
+
* - otherwise {@link IllegalArgumentException} is thrown.
|
|
273
|
+
*
|
|
274
|
+
* @param {TemporalField|TemporalUnit} fieldOrUnit
|
|
275
|
+
* @return {boolean}
|
|
276
|
+
*/
|
|
277
|
+
isSupported(fieldOrUnit) {
|
|
278
|
+
if (fieldOrUnit instanceof TemporalField) {
|
|
279
|
+
return this._isSupportedField(fieldOrUnit);
|
|
280
|
+
}
|
|
281
|
+
if (fieldOrUnit instanceof TemporalUnit) {
|
|
282
|
+
return this._isSupportedUnit(fieldOrUnit);
|
|
283
|
+
}
|
|
284
|
+
if (fieldOrUnit == null) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
throw new IllegalArgumentException(`fieldOrUnit must be an instance of TemporalField or TemporalUnit, but is ${fieldOrUnit.constructor.name}`);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
//-----------------------------------------------------------------------
|
|
292
|
+
/**
|
|
293
|
+
* Checks if the specified field is supported.
|
|
294
|
+
*
|
|
295
|
+
* This checks if this year-week can be queried for the specified field.
|
|
296
|
+
* If false, then calling the {@link YearWeek.range} range and
|
|
297
|
+
* {@link YearWeek.get} get methods will throw an exception.
|
|
298
|
+
*
|
|
299
|
+
* The supported fields are:
|
|
300
|
+
* <ul>
|
|
301
|
+
* <li>{@link IsoFields.WEEK_OF_WEEK_BASED_YEAR}
|
|
302
|
+
* <li>{@link IsoFields.WEEK_BASED_YEAR}
|
|
303
|
+
* </ul>
|
|
304
|
+
* All other {@link ChronoField} instances will return false.
|
|
305
|
+
*
|
|
306
|
+
* If the field is not a {@link ChronoField}, then the result of this method
|
|
307
|
+
* is obtained by invoking {@link TemporalField.isSupportedBy}
|
|
308
|
+
* passing this as the argument.
|
|
309
|
+
* Whether the field is supported is determined by the field.
|
|
310
|
+
*
|
|
311
|
+
* @param {TemporalField} field - the field to check, null returns false
|
|
312
|
+
* @return {boolean} true if the field is supported on this year-week, false if not
|
|
313
|
+
* @protected
|
|
314
|
+
*/
|
|
315
|
+
_isSupportedField(field) {
|
|
316
|
+
if (field === IsoFields.WEEK_OF_WEEK_BASED_YEAR || field === IsoFields.WEEK_BASED_YEAR) {
|
|
317
|
+
return true;
|
|
318
|
+
} else if (field instanceof ChronoField) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
return field != null && field.isSupportedBy(this);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Checks if the specified unit is supported.
|
|
326
|
+
*
|
|
327
|
+
* This checks if the specified unit can be added to, or subtracted from, this date-time.
|
|
328
|
+
* If false, then calling the {@link YearWeek.plus} and
|
|
329
|
+
* {@link YearWeek.minus} minus methods will throw an exception.
|
|
330
|
+
*
|
|
331
|
+
* The supported units are:
|
|
332
|
+
* - {@link ChronoUnit.WEEKS}
|
|
333
|
+
* - {@link IsoFields.WEEK_BASED_YEARS}
|
|
334
|
+
*
|
|
335
|
+
* All other {@link ChronoUnit} instances will return false.
|
|
336
|
+
*
|
|
337
|
+
* If the unit is not a {@link ChronoUnit}, then the result of this method
|
|
338
|
+
* is obtained by invoking {@link TemporalUnit.isSupportedBy}
|
|
339
|
+
* passing this as the argument.
|
|
340
|
+
* Whether the unit is supported is determined by the unit.
|
|
341
|
+
*
|
|
342
|
+
* @param {TemporalUnit} unit - the unit to check, null returns false
|
|
343
|
+
* @return {boolean} true if the unit can be added/subtracted, false if not
|
|
344
|
+
* @protected
|
|
345
|
+
*/
|
|
346
|
+
_isSupportedUnit(unit) {
|
|
347
|
+
if (unit === ChronoUnit.WEEKS || unit === IsoFields.WEEK_BASED_YEARS) {
|
|
348
|
+
return true;
|
|
349
|
+
} else if (unit instanceof ChronoUnit) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
return unit != null && unit.isSupportedBy(this);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
//-----------------------------------------------------------------------
|
|
356
|
+
/**
|
|
357
|
+
* Gets the range of valid values for the specified field.
|
|
358
|
+
*
|
|
359
|
+
* The range object expresses the minimum and maximum valid values for a field.
|
|
360
|
+
* This year-week is used to enhance the accuracy of the returned range.
|
|
361
|
+
* If it is not possible to return the range, because the field is not supported
|
|
362
|
+
* or for some other reason, an exception is thrown.
|
|
363
|
+
*
|
|
364
|
+
* The range for the {@link IsoFields.WEEK_BASED_YEAR} WEEK_BASED_YEAR and
|
|
365
|
+
* {@link IsoFields.WEEK_OF_WEEK_BASED_YEAR} WEEK_OF_WEEK_BASED_YEAR fields is returned.
|
|
366
|
+
* All {@link ChronoField} instances will throw an {@link UnsupportedTemporalTypeException}.
|
|
367
|
+
*
|
|
368
|
+
* @param {TemporalField} field - the field to query the range for, not null
|
|
369
|
+
* @return {ValueRange} the range of valid values for the field, not null
|
|
370
|
+
* @throws {DateTimeException} if the range for the field cannot be obtained
|
|
371
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported
|
|
372
|
+
*/
|
|
373
|
+
range(field) {
|
|
374
|
+
requireNonNull(field, 'field');
|
|
375
|
+
if (field === IsoFields.WEEK_BASED_YEAR) {
|
|
376
|
+
return IsoFields.WEEK_BASED_YEAR.range();
|
|
377
|
+
}
|
|
378
|
+
if (field === IsoFields.WEEK_OF_WEEK_BASED_YEAR) {
|
|
379
|
+
return ValueRange.of(1, YearWeek._weekRange(this._year));
|
|
380
|
+
}
|
|
381
|
+
return super.range(field);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Gets the value of the specified field from this year-week as an `int`.
|
|
386
|
+
*
|
|
387
|
+
* This queries this year-week for the value for the specified field.
|
|
388
|
+
* The returned value will always be within the valid range of values for the field.
|
|
389
|
+
* If it is not possible to return the value, because the field is not supported
|
|
390
|
+
* or for some other reason, an exception is thrown.
|
|
391
|
+
*
|
|
392
|
+
* The value for the {@link IsoFields.WEEK_BASED_YEAR} WEEK_BASED_YEAR and
|
|
393
|
+
* {@link IsoFields.WEEK_OF_WEEK_BASED_YEAR} WEEK_OF_WEEK_BASED_YEAR fields is returned.
|
|
394
|
+
* All {@link ChronoField} instances will throw an {@link UnsupportedTemporalTypeException}.
|
|
395
|
+
*
|
|
396
|
+
* @param {TemporalField} field - the field to get, not null
|
|
397
|
+
* @return {number} the value for the field
|
|
398
|
+
* @throws {DateTimeException} if a value for the field cannot be obtained or
|
|
399
|
+
* the value is outside the range of valid values for the field
|
|
400
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported or
|
|
401
|
+
* the range of values exceeds an `int`
|
|
402
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
403
|
+
*/
|
|
404
|
+
get(field) {
|
|
405
|
+
return this.range(field).checkValidIntValue(this.getLong(field), field);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Gets the value of the specified field from this year-week as a `long`.
|
|
410
|
+
*
|
|
411
|
+
* This queries this year-week for the value for the specified field.
|
|
412
|
+
* If it is not possible to return the value, because the field is not supported
|
|
413
|
+
* or for some other reason, an exception is thrown.
|
|
414
|
+
*
|
|
415
|
+
* The value for the {@link IsoFields.WEEK_BASED_YEAR} WEEK_BASED_YEAR and
|
|
416
|
+
* {@link IsoFields.WEEK_OF_WEEK_BASED_YEAR} WEEK_OF_WEEK_BASED_YEAR fields is returned.
|
|
417
|
+
* All {@link ChronoField} instances will throw an {@link UnsupportedTemporalTypeException}.
|
|
418
|
+
*
|
|
419
|
+
* @param {TemporalField} field - the field to get, not null
|
|
420
|
+
* @return {number} the value for the field
|
|
421
|
+
* @throws {DateTimeException} if a value for the field cannot be obtained
|
|
422
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported
|
|
423
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
424
|
+
*/
|
|
425
|
+
getLong(field) {
|
|
426
|
+
requireNonNull(field, 'field');
|
|
427
|
+
if (field === IsoFields.WEEK_BASED_YEAR) {
|
|
428
|
+
return this._year;
|
|
429
|
+
}
|
|
430
|
+
if (field === IsoFields.WEEK_OF_WEEK_BASED_YEAR) {
|
|
431
|
+
return this._week;
|
|
432
|
+
}
|
|
433
|
+
if (field instanceof ChronoField) {
|
|
434
|
+
throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
|
|
435
|
+
}
|
|
436
|
+
return field.getFrom(this);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
//-----------------------------------------------------------------------
|
|
440
|
+
/**
|
|
441
|
+
* Gets the week-based-year field.
|
|
442
|
+
*
|
|
443
|
+
* This method returns the primitive `int` value for the week-based-year.
|
|
444
|
+
*
|
|
445
|
+
* Note that the ISO week-based-year does not align with the standard Gregorian/ISO calendar year.
|
|
446
|
+
*
|
|
447
|
+
* @return {number} the week-based-year
|
|
448
|
+
*/
|
|
449
|
+
year() {
|
|
450
|
+
return this._year;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Gets the week-of-week-based-year field.
|
|
455
|
+
*
|
|
456
|
+
* This method returns the primitive `int` value for the week of the week-based-year.
|
|
457
|
+
*
|
|
458
|
+
* @return {number} the week-of-week-based-year
|
|
459
|
+
*/
|
|
460
|
+
week() {
|
|
461
|
+
return this._week;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
//-----------------------------------------------------------------------
|
|
465
|
+
/**
|
|
466
|
+
* Checks if the week-based-year has 53 weeks.
|
|
467
|
+
*
|
|
468
|
+
* This determines if the year has 53 weeks, returning true.
|
|
469
|
+
* If false, the year has 52 weeks.
|
|
470
|
+
*
|
|
471
|
+
* @return {boolean} true if the year has 53 weeks, false otherwise
|
|
472
|
+
*/
|
|
473
|
+
is53WeekYear() {
|
|
474
|
+
return YearWeek._weekRange(this._year) === 53;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Returns the length of the week-based-year.
|
|
479
|
+
*
|
|
480
|
+
* This returns the length of the year in days, either 364 or 371.
|
|
481
|
+
*
|
|
482
|
+
* @return {number} 364 if the year has 52 weeks, 371 if it has 53 weeks
|
|
483
|
+
*/
|
|
484
|
+
lengthOfYear() {
|
|
485
|
+
return (this.is53WeekYear() ? 371 : 364);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
//-----------------------------------------------------------------------
|
|
489
|
+
/**
|
|
490
|
+
* Returns an adjusted copy of this year-week.
|
|
491
|
+
*
|
|
492
|
+
* This returns a {@link YearWeek}, based on this one, with the year-week adjusted.
|
|
493
|
+
* The adjustment takes place using the specified adjuster strategy object.
|
|
494
|
+
* Read the documentation of the adjuster to understand what adjustment will be made.
|
|
495
|
+
*
|
|
496
|
+
* The result of this method is obtained by invoking the
|
|
497
|
+
* {@link TemporalAdjusteradjustInto} method on the
|
|
498
|
+
* specified adjuster passing this as the argument.
|
|
499
|
+
*
|
|
500
|
+
* This instance is immutable and unaffected by this method call.
|
|
501
|
+
*
|
|
502
|
+
* @param {TemporalAdjuster} adjuster - adjuster the adjuster to use, not null
|
|
503
|
+
* @return {YearWeek} a year-week based on this with the adjustment made, not null
|
|
504
|
+
* @throws {DateTimeException} if the adjustment cannot be made
|
|
505
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
506
|
+
*/
|
|
507
|
+
_withAdjuster(adjuster) {
|
|
508
|
+
// optimizations
|
|
509
|
+
if (adjuster instanceof YearWeek) {
|
|
510
|
+
return adjuster;
|
|
511
|
+
}
|
|
512
|
+
return super._withAdjuster(adjuster);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Returns a copy of this year-week with the specified field set to a new value.
|
|
517
|
+
*
|
|
518
|
+
* This returns a {@link YearWeek}, based on this one, with the value
|
|
519
|
+
* for the specified field changed.
|
|
520
|
+
* This can be used to change any supported field, such as the year or week.
|
|
521
|
+
* If it is not possible to set the value, because the field is not supported or for
|
|
522
|
+
* some other reason, an exception is thrown.
|
|
523
|
+
*
|
|
524
|
+
* If the field is a {@link ChronoField} then the adjustment is implemented here.
|
|
525
|
+
* The supported fields behave as follows:
|
|
526
|
+
* - {@link WEEK_OF_WEEK_BASED_YEAR} - Returns a {@link YearWeek} with the specified week-of-year set as per {@link YearWeek.withWeek}.
|
|
527
|
+
* - {@link WEEK_BASED_YEAR} - Returns a {@link YearWeek} with the specified year set as per {@link YearWeek.withYear}.
|
|
528
|
+
*
|
|
529
|
+
* All {@link ChronoField} instances will throw an {@link UnsupportedTemporalTypeException}.
|
|
530
|
+
*
|
|
531
|
+
* If the field is not a {@link ChronoField}, then the result of this method
|
|
532
|
+
* is obtained by invoking {@link TemporalField.adjustInto}
|
|
533
|
+
* passing this as the argument. In this case, the field determines
|
|
534
|
+
* whether and how to adjust the instant.
|
|
535
|
+
*
|
|
536
|
+
* This instance is immutable and unaffected by this method call.
|
|
537
|
+
*
|
|
538
|
+
* @param {TemporalField} field - the field to set in the result, not null
|
|
539
|
+
* @param {number} newValue - the new value of the field in the result
|
|
540
|
+
* @return {YearWeek} a year-week based on this with the specified field set, not null
|
|
541
|
+
* @throws {DateTimeException} if the field cannot be set
|
|
542
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported
|
|
543
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
544
|
+
*/
|
|
545
|
+
_withField(field, newValue) {
|
|
546
|
+
if (field === IsoFields.WEEK_OF_WEEK_BASED_YEAR) {
|
|
547
|
+
return this.withWeek(IsoFields.WEEK_OF_WEEK_BASED_YEAR.range().checkValidIntValue(newValue, IsoFields.WEEK_OF_WEEK_BASED_YEAR));
|
|
548
|
+
} else if (field === IsoFields.WEEK_BASED_YEAR) {
|
|
549
|
+
return this.withYear(IsoFields.WEEK_BASED_YEAR.range().checkValidIntValue(newValue, IsoFields.WEEK_BASED_YEAR));
|
|
550
|
+
} else if (field instanceof ChronoField) {
|
|
551
|
+
throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
|
|
552
|
+
}
|
|
553
|
+
return field.adjustInto(this, newValue);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Returns a copy of this {@link YearWeek} with the week-based-year altered.
|
|
558
|
+
*
|
|
559
|
+
* This returns a year-week with the specified week-based-year.
|
|
560
|
+
* If the week of this instance is 53 and the new year does not have 53 weeks,
|
|
561
|
+
* the week will be adjusted to be 52.
|
|
562
|
+
*
|
|
563
|
+
* This instance is immutable and unaffected by this method call.
|
|
564
|
+
*
|
|
565
|
+
* @param {number} weekBasedYear - the week-based-year to set in the returned year-week
|
|
566
|
+
* @return {YearWeek} a year-week based on this year-week with the requested year, not null
|
|
567
|
+
* @throws {DateTimeException} if the week-based-year value is invalid
|
|
568
|
+
*/
|
|
569
|
+
withYear(weekBasedYear) {
|
|
570
|
+
if (this._week === 53 && YearWeek._weekRange(weekBasedYear) < 53) {
|
|
571
|
+
return YearWeek.of(weekBasedYear, 52);
|
|
572
|
+
}
|
|
573
|
+
return YearWeek.of(weekBasedYear, this._week);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Returns a copy of this {@link YearWeek} with the week altered.
|
|
578
|
+
*
|
|
579
|
+
* This returns a year-week with the specified week-of-week-based-year.
|
|
580
|
+
* If the new week is 53 and the year does not have 53 weeks, week one of the
|
|
581
|
+
* following year is selected.
|
|
582
|
+
*
|
|
583
|
+
* This instance is immutable and unaffected by this method call.
|
|
584
|
+
*
|
|
585
|
+
* @param {number} week - the week-of-week-based-year to set in the returned year-week
|
|
586
|
+
* @return {YearWeek} a year-week based on this year-week with the requested week, not null
|
|
587
|
+
* @throws {DateTimeException} if the week-of-week-based-year value is invalid
|
|
588
|
+
*/
|
|
589
|
+
withWeek(week) {
|
|
590
|
+
return YearWeek.of(this._year, week);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Returns a copy of this year-week with the specified amount added.
|
|
595
|
+
*
|
|
596
|
+
* This returns a {@link YearWeek}, based on this one, with the amount
|
|
597
|
+
* in terms of the unit added. If it is not possible to add the amount, because the
|
|
598
|
+
* unit is not supported or for some other reason, an exception is thrown.
|
|
599
|
+
*
|
|
600
|
+
* If the field is a {@link ChronoUnit} then the addition is implemented here.
|
|
601
|
+
* The supported fields behave as follows:
|
|
602
|
+
* - {@link WEEKS} - Returns a {@link YearWeek} with the weeks added as per {@link YearWeek.plusWeeks}.
|
|
603
|
+
* - {@link WEEK_BASED_YEARS} - Returns a {@link YearWeek} with the years added as per {@link YearWeek.plusYears}.
|
|
604
|
+
*
|
|
605
|
+
* All {@link ChronoUnit} instances will throw an {@link UnsupportedTemporalTypeException}.
|
|
606
|
+
*
|
|
607
|
+
* If the field is not a {@link ChronoUnit}, then the result of this method
|
|
608
|
+
* is obtained by invoking {@link TemporalUnit.addTo}
|
|
609
|
+
* passing this as the argument. In this case, the unit determines
|
|
610
|
+
* whether and how to perform the addition.
|
|
611
|
+
*
|
|
612
|
+
* This instance is immutable and unaffected by this method call.
|
|
613
|
+
*
|
|
614
|
+
* @param {number} amountToAdd - the amount of the unit to add to the result, may be negative
|
|
615
|
+
* @param {TemporalUnit} unit - the unit of the amount to add, not null
|
|
616
|
+
* @return {YearWeek} a year-week based on this year-week with the specified amount added, not null
|
|
617
|
+
* @throws {DateTimeException} if the addition cannot be made
|
|
618
|
+
* @throws {UnsupportedTemporalTypeException} if the unit is not supported
|
|
619
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
620
|
+
*/
|
|
621
|
+
_plusUnit(amountToAdd, unit) {
|
|
622
|
+
if (unit === ChronoUnit.WEEKS) {
|
|
623
|
+
return this.plusWeeks(amountToAdd);
|
|
624
|
+
} else if (unit === IsoFields.WEEK_BASED_YEARS) {
|
|
625
|
+
return this.plusYears(amountToAdd);
|
|
626
|
+
} else if (unit instanceof ChronoUnit) {
|
|
627
|
+
throw new UnsupportedTemporalTypeException(`Unsupported unit: ${unit}`);
|
|
628
|
+
}
|
|
629
|
+
return unit.addTo(this, amountToAdd);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Returns a copy of this year-week with the specified number of years added.
|
|
634
|
+
*
|
|
635
|
+
* If the week of this instance is 53 and the new year does not have 53 weeks,
|
|
636
|
+
* the week will be adjusted to be 52.
|
|
637
|
+
*
|
|
638
|
+
* This instance is immutable and unaffected by this method call.
|
|
639
|
+
*
|
|
640
|
+
* @param {number} yearsToAdd - the years to add, may be negative
|
|
641
|
+
* @return {YearWeek} the year-week with the years added, not null
|
|
642
|
+
*/
|
|
643
|
+
plusYears(yearsToAdd) {
|
|
644
|
+
if (yearsToAdd === 0) {
|
|
645
|
+
return this;
|
|
646
|
+
}
|
|
647
|
+
const newYear = MathUtil.safeToInt(MathUtil.safeAdd(this._year, yearsToAdd));
|
|
648
|
+
return this.withYear(newYear);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Returns a copy of this year-week with the specified number of weeks added.
|
|
653
|
+
*
|
|
654
|
+
* This instance is immutable and unaffected by this method call.
|
|
655
|
+
*
|
|
656
|
+
* @param {number} weeksToAdd - the weeks to add, may be negative
|
|
657
|
+
* @return {YearWeek} the year-week with the weeks added, not null
|
|
658
|
+
*/
|
|
659
|
+
plusWeeks(weeksToAdd) {
|
|
660
|
+
if (weeksToAdd === 0) {
|
|
661
|
+
return this;
|
|
662
|
+
}
|
|
663
|
+
const mondayOfWeek = this.atDay(DayOfWeek.MONDAY).plusWeeks(weeksToAdd);
|
|
664
|
+
return YearWeek.from(mondayOfWeek);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Returns a copy of this year-week with the specified amount subtracted.
|
|
669
|
+
*
|
|
670
|
+
* This returns a {@link YearWeek}, based on this one, with the amount
|
|
671
|
+
* in terms of the unit subtracted. If it is not possible to subtract the amount,
|
|
672
|
+
* because the unit is not supported or for some other reason, an exception is thrown.
|
|
673
|
+
*
|
|
674
|
+
* This method is equivalent to {@link YearWeek.plus} with the amount negated.
|
|
675
|
+
* See that method for a full description of how addition, and thus subtraction, works.
|
|
676
|
+
*
|
|
677
|
+
* This instance is immutable and unaffected by this method call.
|
|
678
|
+
*
|
|
679
|
+
* @param {number} amountToSubtract - the amount of the unit to subtract from the result, may be negative
|
|
680
|
+
* @param {TemporalUnit} unit - the unit of the amount to subtract, not null
|
|
681
|
+
* @return {YearWeek} a year-week based on this year-week with the specified amount subtracted, not null
|
|
682
|
+
* @throws {DateTimeException} if the subtraction cannot be made
|
|
683
|
+
* @throws {UnsupportedTemporalTypeException} if the unit is not supported
|
|
684
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
685
|
+
*/
|
|
686
|
+
_minusUnit(amountToSubtract, unit) {
|
|
687
|
+
requireNonNull(amountToSubtract, 'amountToSubtract');
|
|
688
|
+
requireNonNull(unit, 'unit');
|
|
689
|
+
return this._plusUnit(-amountToSubtract, unit);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Returns a copy of this year-week with the specified number of years subtracted.
|
|
694
|
+
*
|
|
695
|
+
* If the week of this instance is 53 and the new year does not have 53 weeks,
|
|
696
|
+
* the week will be adjusted to be 52.
|
|
697
|
+
*
|
|
698
|
+
* This instance is immutable and unaffected by this method call.
|
|
699
|
+
*
|
|
700
|
+
* @param {number} yearsToSubtract - the years to subtract, may be negative
|
|
701
|
+
* @return {YearWeek} the year-week with the years subtracted, not null
|
|
702
|
+
*/
|
|
703
|
+
minusYears(yearsToSubtract) {
|
|
704
|
+
if (yearsToSubtract === 0) {
|
|
705
|
+
return this;
|
|
706
|
+
}
|
|
707
|
+
const newYear = MathUtil.safeToInt(MathUtil.safeSubtract(this._year, yearsToSubtract));
|
|
708
|
+
return this.withYear(newYear);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Returns a copy of this year-week with the specified number of weeks subtracted.
|
|
713
|
+
*
|
|
714
|
+
* This instance is immutable and unaffected by this method call.
|
|
715
|
+
*
|
|
716
|
+
* @param {number} weeksToSubtract - the weeks to subtract, may be negative
|
|
717
|
+
* @return {YearWeek} the year-week with the weeks subtracted, not null
|
|
718
|
+
*/
|
|
719
|
+
minusWeeks(weeksToSubtract) {
|
|
720
|
+
if (weeksToSubtract === 0) {
|
|
721
|
+
return this;
|
|
722
|
+
}
|
|
723
|
+
const mondayOfWeek = this.atDay(DayOfWeek.MONDAY).minusWeeks(weeksToSubtract);
|
|
724
|
+
return YearWeek.from(mondayOfWeek);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
//-----------------------------------------------------------------------
|
|
728
|
+
/**
|
|
729
|
+
* Queries this year-week using the specified query.
|
|
730
|
+
*
|
|
731
|
+
* This queries this year-week using the specified query strategy object.
|
|
732
|
+
* The {@link TemporalQuery} object defines the logic to be used to
|
|
733
|
+
* obtain the result. Read the documentation of the query to understand
|
|
734
|
+
* what the result of this method will be.
|
|
735
|
+
*
|
|
736
|
+
* The result of this method is obtained by invoking the
|
|
737
|
+
* {@link TemporalQuery.queryFrom} method on the
|
|
738
|
+
* specified query passing this as the argument.
|
|
739
|
+
*
|
|
740
|
+
* @param {TemporalQuery} query - the query to invoke, not null
|
|
741
|
+
* @return {*} the query result, null may be returned (defined by the query)
|
|
742
|
+
* @throws {DateTimeException} if unable to query (defined by the query)
|
|
743
|
+
* @throws {ArithmeticException} if numeric overflow occurs (defined by the query)
|
|
744
|
+
*/
|
|
745
|
+
query(query) {
|
|
746
|
+
requireNonNull(query, 'query');
|
|
747
|
+
requireInstance(query, TemporalQuery, 'query');
|
|
748
|
+
if (query === TemporalQueries.chronology()) {
|
|
749
|
+
return IsoChronology.INSTANCE;
|
|
750
|
+
}
|
|
751
|
+
return super.query(query);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Adjusts the specified temporal object to have this year-week.
|
|
756
|
+
*
|
|
757
|
+
* This returns a temporal object of the same observable type as the input
|
|
758
|
+
* with the week-based-year and week changed to be the same as this.
|
|
759
|
+
*
|
|
760
|
+
* The adjustment is equivalent to using {@link Temporal.with}
|
|
761
|
+
* twice, passing {@link IsoFields.WEEK_BASED_YEAR} and
|
|
762
|
+
* {@link IsoFields.WEEK_OF_WEEK_BASED_YEAR} as the fields.
|
|
763
|
+
* If the specified temporal object does not use the ISO calendar system then
|
|
764
|
+
* a {@link DateTimeException} is thrown.
|
|
765
|
+
*
|
|
766
|
+
* In most cases, it is clearer to reverse the calling pattern by using
|
|
767
|
+
* {@link Temporal.with}:
|
|
768
|
+
* ```
|
|
769
|
+
* // these two lines are equivalent, but the second approach is recommended
|
|
770
|
+
* temporal = thisYearWeek.adjustInto(temporal);
|
|
771
|
+
* temporal = temporal.with(thisYearWeek);
|
|
772
|
+
* ```
|
|
773
|
+
*
|
|
774
|
+
* This instance is immutable and unaffected by this method call.
|
|
775
|
+
*
|
|
776
|
+
* @param {Temporal} temporal - the target object to be adjusted, not null
|
|
777
|
+
* @return {Temporal} the adjusted object, not null
|
|
778
|
+
* @throws {DateTimeException} if unable to make the adjustment
|
|
779
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
780
|
+
*/
|
|
781
|
+
adjustInto(temporal) {
|
|
782
|
+
return temporal.with(IsoFields.WEEK_BASED_YEAR, this._year).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, this._week);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Calculates the amount of time until another year-week in terms of the specified unit.
|
|
787
|
+
*
|
|
788
|
+
* This calculates the amount of time between two {@link YearWeek}
|
|
789
|
+
* objects in terms of a single {@link TemporalUnit}.
|
|
790
|
+
* The start and end points are this and the specified year-week.
|
|
791
|
+
* The result will be negative if the end is before the start.
|
|
792
|
+
* The {@link Temporal} passed to this method is converted to a
|
|
793
|
+
* {@link YearWeek} using {@link YearWeek.from}.
|
|
794
|
+
* For example, the period in years between two year-weeks can be calculated
|
|
795
|
+
* using `startYearWeek.until(endYearWeek, YEARS)`.
|
|
796
|
+
*
|
|
797
|
+
* The calculation returns a whole number, representing the number of
|
|
798
|
+
* complete units between the two year-weeks.
|
|
799
|
+
* For example, the period in years between 2012-W23 and 2032-W22
|
|
800
|
+
* will only be 9 years as it is one week short of 10 years.
|
|
801
|
+
*
|
|
802
|
+
* There are two equivalent ways of using this method.
|
|
803
|
+
* The first is to invoke this method.
|
|
804
|
+
* The second is to use {@link TemporalUnit.between}:
|
|
805
|
+
* ```
|
|
806
|
+
* // these two lines are equivalent
|
|
807
|
+
* amount = start.until(end, WEEKS);
|
|
808
|
+
* amount = WEEKS.between(start, end);
|
|
809
|
+
* ```
|
|
810
|
+
* The choice should be made based on which makes the code more readable.
|
|
811
|
+
*
|
|
812
|
+
* The calculation is implemented in this method for units {@link WEEKS}
|
|
813
|
+
* and {@link WEEK_BASED_YEARS}.
|
|
814
|
+
* Other {@link ChronoUnit} values will throw an exception.
|
|
815
|
+
*
|
|
816
|
+
* If the unit is not a {@link ChronoUnit}, then the result of this method
|
|
817
|
+
* is obtained by invoking {@link TemporalUnit.between}
|
|
818
|
+
* passing this as the first argument and the converted input temporal
|
|
819
|
+
* as the second argument.
|
|
820
|
+
*
|
|
821
|
+
* This instance is immutable and unaffected by this method call.
|
|
822
|
+
*
|
|
823
|
+
* @param {Temporal} endExclusive - the end date, exclusive, which is converted to a {@link YearWeek}, not null
|
|
824
|
+
* @param {TemporalUnit} unit - the unit to measure the amount in, not null
|
|
825
|
+
* @return {number} the amount of time between this year-week and the end year-week
|
|
826
|
+
* @throws {DateTimeException} if the amount cannot be calculated, or the end
|
|
827
|
+
* temporal cannot be converted to a {@link YearWeek}
|
|
828
|
+
* @throws {UnsupportedTemporalTypeException} if the unit is not supported
|
|
829
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
830
|
+
*/
|
|
831
|
+
until(endExclusive, unit) {
|
|
832
|
+
const end = YearWeek.from(endExclusive);
|
|
833
|
+
if (unit === ChronoUnit.WEEKS) {
|
|
834
|
+
return this._daysUntil(end);
|
|
835
|
+
} else if (unit === IsoFields.WEEK_BASED_YEARS) {
|
|
836
|
+
return this._yearsUntil(end);
|
|
837
|
+
} else if (unit instanceof ChronoUnit) {
|
|
838
|
+
throw new UnsupportedTemporalTypeException(`Unsupported unit: ${unit}`);
|
|
839
|
+
}
|
|
840
|
+
return unit.between(this, end);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* @private
|
|
845
|
+
*/
|
|
846
|
+
_daysUntil(end) {
|
|
847
|
+
const startDate = this.atDay(DayOfWeek.MONDAY);
|
|
848
|
+
const endDate = end.atDay(DayOfWeek.MONDAY);
|
|
849
|
+
const days = endDate.toEpochDay() - startDate.toEpochDay();
|
|
850
|
+
return MathUtil.intDiv(days, 7);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* @private
|
|
855
|
+
*/
|
|
856
|
+
_yearsUntil(end) {
|
|
857
|
+
const yearsDiff = end._year - this._year;
|
|
858
|
+
if (yearsDiff > 0 && end._week < this._week) {
|
|
859
|
+
return yearsDiff - 1;
|
|
860
|
+
}
|
|
861
|
+
if (yearsDiff < 0 && end._week > this._week) {
|
|
862
|
+
return yearsDiff + 1;
|
|
863
|
+
}
|
|
864
|
+
return yearsDiff;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Formats this year-week using the specified formatter.
|
|
869
|
+
*
|
|
870
|
+
* This year-week will be passed to the formatter to produce a string.
|
|
871
|
+
*
|
|
872
|
+
* @param {DateTimeFormatter} formatter - the formatter to use, not null
|
|
873
|
+
* @return {string} the formatted year-week string, not null
|
|
874
|
+
* @throws {DateTimeException} if an error occurs during printing
|
|
875
|
+
*/
|
|
876
|
+
format(formatter) {
|
|
877
|
+
requireNonNull(formatter, 'formatter');
|
|
878
|
+
return formatter.format(this);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
//-----------------------------------------------------------------------
|
|
882
|
+
/**
|
|
883
|
+
* Combines this year-week with a day-of-week to create a {@link LocalDate}.
|
|
884
|
+
*
|
|
885
|
+
* This returns a {@link LocalDate} formed from this year-week and the specified day-of-Week.
|
|
886
|
+
*
|
|
887
|
+
* This method can be used as part of a chain to produce a date:
|
|
888
|
+
* ```
|
|
889
|
+
* LocalDate date = yearWeek.atDay(MONDAY);
|
|
890
|
+
* ```
|
|
891
|
+
*
|
|
892
|
+
* @param {DayOfWeek} dayOfWeek - the day-of-week to use, not null
|
|
893
|
+
* @return {LocalDate} the date formed from this year-week and the specified day, not null
|
|
894
|
+
*/
|
|
895
|
+
atDay(dayOfWeek) {
|
|
896
|
+
requireNonNull(dayOfWeek, 'dayOfWeek');
|
|
897
|
+
const correction = LocalDate.of(this._year, 1, 4).dayOfWeek().value() + 3;
|
|
898
|
+
const dayOfYear = this._week * 7 + dayOfWeek.value() - correction;
|
|
899
|
+
const maxDaysOfYear = Year.isLeap(this._year) ? 366 : 365;
|
|
900
|
+
if (dayOfYear > maxDaysOfYear) {
|
|
901
|
+
return LocalDate.ofYearDay(this._year + 1, dayOfYear - maxDaysOfYear);
|
|
902
|
+
}
|
|
903
|
+
if (dayOfYear > 0) {
|
|
904
|
+
return LocalDate.ofYearDay(this._year, dayOfYear);
|
|
905
|
+
} else {
|
|
906
|
+
const daysOfPreviousYear = Year.isLeap(this._year - 1) ? 366 : 365;
|
|
907
|
+
return LocalDate.ofYearDay(this._year - 1, daysOfPreviousYear + dayOfYear);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
//-----------------------------------------------------------------------
|
|
912
|
+
/**
|
|
913
|
+
* Compares this year-week to another
|
|
914
|
+
*
|
|
915
|
+
* The comparison is based first on the value of the year, then on the value of the week.
|
|
916
|
+
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
917
|
+
*
|
|
918
|
+
* @param {YearWeek} other - the other year-week to compare to, not null
|
|
919
|
+
* @return {number} the comparator value, negative if less, positive if greater
|
|
920
|
+
*/
|
|
921
|
+
compareTo(other) {
|
|
922
|
+
requireNonNull(other, 'other');
|
|
923
|
+
requireInstance(other, YearWeek, 'other');
|
|
924
|
+
let cmp = (this._year - other._year);
|
|
925
|
+
if (cmp === 0) {
|
|
926
|
+
cmp = (this._week - other._week);
|
|
927
|
+
}
|
|
928
|
+
return cmp;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Is this year-week after the specified year-week.
|
|
933
|
+
*
|
|
934
|
+
* @param {YearWeek} other - the other year-week to compare to, not null
|
|
935
|
+
* @return {boolean} true if this is after the specified year-week
|
|
936
|
+
*/
|
|
937
|
+
isAfter(other) {
|
|
938
|
+
return this.compareTo(other) > 0;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Is this year-week before the specified year-week.
|
|
943
|
+
*
|
|
944
|
+
* @param {YearWeek} other - the other year-week to compare to, not null
|
|
945
|
+
* @return {boolean} true if this point is before the specified year-week
|
|
946
|
+
*/
|
|
947
|
+
isBefore(other) {
|
|
948
|
+
return this.compareTo(other) < 0;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
//-----------------------------------------------------------------------
|
|
952
|
+
/**
|
|
953
|
+
* Checks if this year-week is equal to another year-week.
|
|
954
|
+
*
|
|
955
|
+
* @param {*} obj - the object to check, null returns false
|
|
956
|
+
* @return {boolean} true if this is equal to the other year-week
|
|
957
|
+
*/
|
|
958
|
+
equals(obj) {
|
|
959
|
+
if (this === obj) {
|
|
960
|
+
return true;
|
|
961
|
+
}
|
|
962
|
+
if (obj instanceof YearWeek) {
|
|
963
|
+
return this._year === obj._year && this._week === obj._week;
|
|
964
|
+
}
|
|
965
|
+
return false;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/**
|
|
969
|
+
* A hash code for this year-week.
|
|
970
|
+
*
|
|
971
|
+
* @return {number} a suitable hash code
|
|
972
|
+
*/
|
|
973
|
+
hashCode() {
|
|
974
|
+
return this._year ^ (this._week << 25);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
//-----------------------------------------------------------------------
|
|
978
|
+
/**
|
|
979
|
+
* Outputs this year-week as a {@link String}, such as `2015-W13`.
|
|
980
|
+
*
|
|
981
|
+
* The output will be in the format `YYYY-'W'ww`:
|
|
982
|
+
*
|
|
983
|
+
* @return {string} a string representation of this year-week, not null
|
|
984
|
+
*/
|
|
985
|
+
toString() {
|
|
986
|
+
let yearString;
|
|
987
|
+
const yearValue = this._year;
|
|
988
|
+
const absYear = Math.abs(yearValue);
|
|
989
|
+
if (absYear < 1000) {
|
|
990
|
+
if (yearValue < 0) {
|
|
991
|
+
yearString = `-${(`${yearValue - 10000}`).slice(-4)}`;
|
|
992
|
+
} else {
|
|
993
|
+
yearString = (`${yearValue + 10000}`).slice(-4);
|
|
994
|
+
}
|
|
995
|
+
} else {
|
|
996
|
+
if (yearValue > 9999) {
|
|
997
|
+
yearString = `+${yearValue}`;
|
|
998
|
+
} else {
|
|
999
|
+
yearString = `${yearValue}`;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return yearString.concat(this._week < 10 ? '-W0' : '-W').concat(this._week);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
export function _init() {
|
|
1007
|
+
YearWeek.PARSER = new DateTimeFormatterBuilder()
|
|
1008
|
+
.parseCaseInsensitive()
|
|
1009
|
+
.appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
|
|
1010
|
+
.appendLiteral('-W')
|
|
1011
|
+
.appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
|
|
1012
|
+
.toFormatter();
|
|
1013
|
+
|
|
1014
|
+
YearWeek.FROM = createTemporalQuery('YearWeek.FROM', (temporal) => {
|
|
1015
|
+
return YearWeek.from(temporal);
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// copied from packages/core/src/temporal/TemporalQuery.js
|
|
1020
|
+
function createTemporalQuery(name, queryFromFunction) {
|
|
1021
|
+
class ExtendedTemporalQuery extends TemporalQuery {
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
ExtendedTemporalQuery.prototype.queryFrom = queryFromFunction;
|
|
1025
|
+
return new ExtendedTemporalQuery(name);
|
|
1026
|
+
}
|