@js-joda/extra 0.9.1 → 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/Quarter.js
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright (c) 2016, 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
|
+
// TODO: hm... is this a good idea?? copied from joda currently, could we add a js-joda-utils module??
|
|
8
|
+
import { requireNonNull, requireInstance } from './assert';
|
|
9
|
+
import { ChronoField, DateTimeException, IllegalArgumentException, IllegalStateException, IsoChronology, IsoFields, Month, TemporalAccessor, TemporalField, TemporalQueries, TemporalQuery, UnsupportedTemporalTypeException, _ as jodaInternal } from '@js-joda/core';
|
|
10
|
+
|
|
11
|
+
const MathUtil = jodaInternal.MathUtil;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A quarter-of-year, such as 'Q2'.
|
|
15
|
+
*
|
|
16
|
+
* `Quarter` is an enum representing the 4 quarters of the year -
|
|
17
|
+
* Q1, Q2, Q3 and Q4. These are defined as January to March, April to June,
|
|
18
|
+
* July to September and October to December.
|
|
19
|
+
*
|
|
20
|
+
* The `int` value follows the quarter, from 1 (Q1) to 4 (Q4).
|
|
21
|
+
* It is recommended that applications use the enum rather than the `int` value
|
|
22
|
+
* to ensure code clarity.
|
|
23
|
+
*
|
|
24
|
+
* **Do not use `ordinal()` to obtain the numeric representation of `Quarter`.
|
|
25
|
+
* Use `getValue()` instead.**
|
|
26
|
+
*
|
|
27
|
+
* This enum represents a common concept that is found in many calendar systems.
|
|
28
|
+
* As such, this enum may be used by any calendar system that has the quarter-of-year
|
|
29
|
+
* concept defined exactly equivalent to the ISO calendar system.
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export class Quarter extends TemporalAccessor {
|
|
33
|
+
/**
|
|
34
|
+
* replacement for enum `valueOf(name)`
|
|
35
|
+
*
|
|
36
|
+
* @param {string} name
|
|
37
|
+
* @return {Quarter}
|
|
38
|
+
*/
|
|
39
|
+
static valueOf(name) {
|
|
40
|
+
requireNonNull(name, 'name');
|
|
41
|
+
switch (name) {
|
|
42
|
+
case 'Q1':
|
|
43
|
+
return Quarter.Q1;
|
|
44
|
+
case 'Q2':
|
|
45
|
+
return Quarter.Q2;
|
|
46
|
+
case 'Q3':
|
|
47
|
+
return Quarter.Q3;
|
|
48
|
+
case 'Q4':
|
|
49
|
+
return Quarter.Q4;
|
|
50
|
+
}
|
|
51
|
+
throw new IllegalArgumentException(`No enum constant Quarter.${name}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* replacement for enum `values()`
|
|
56
|
+
* @return {Quarter[]}
|
|
57
|
+
*/
|
|
58
|
+
static values() {
|
|
59
|
+
return QUARTERS.slice();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//-----------------------------------------------------------------------
|
|
63
|
+
/**
|
|
64
|
+
* Obtains an instance of `Quarter` from an `int` value.
|
|
65
|
+
*
|
|
66
|
+
* `Quarter` is an enum representing the 4 quarters of the year.
|
|
67
|
+
* This factory allows the enum to be obtained from the `int` value.
|
|
68
|
+
* The `int` value follows the quarter, from 1 (Q1) to 4 (Q4).
|
|
69
|
+
*
|
|
70
|
+
* @param {number} quarterOfYear - the quarter-of-year to represent, from 1 (Q1) to 4 (Q4)
|
|
71
|
+
* @return {Quarter} the quarter-of-year, not null
|
|
72
|
+
* @throws {DateTimeException} if the quarter-of-year is invalid
|
|
73
|
+
*/
|
|
74
|
+
static of(quarterOfYear) {
|
|
75
|
+
requireNonNull(quarterOfYear, 'quarterOfYear');
|
|
76
|
+
switch (quarterOfYear) {
|
|
77
|
+
case 1:
|
|
78
|
+
return Quarter.Q1;
|
|
79
|
+
case 2:
|
|
80
|
+
return Quarter.Q2;
|
|
81
|
+
case 3:
|
|
82
|
+
return Quarter.Q3;
|
|
83
|
+
case 4:
|
|
84
|
+
return Quarter.Q4;
|
|
85
|
+
default:
|
|
86
|
+
throw new DateTimeException(`Invalid value for Quarter: ${quarterOfYear}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Obtains an instance of `Quarter` from a month-of-year.
|
|
92
|
+
*
|
|
93
|
+
* `Quarter` is an enum representing the 4 quarters of the year.
|
|
94
|
+
* This factory allows the enum to be obtained from the `Month` value.
|
|
95
|
+
*
|
|
96
|
+
* January to March are Q1, April to June are Q2, July to September are Q3
|
|
97
|
+
* and October to December are Q4.
|
|
98
|
+
*
|
|
99
|
+
* @param {number} monthOfYear - the month-of-year to convert from, from 1 to 12
|
|
100
|
+
* @return {Quarter} the quarter-of-year, not null
|
|
101
|
+
* @throws {DateTimeException} if the month-of-year is invalid
|
|
102
|
+
*/
|
|
103
|
+
static ofMonth(monthOfYear) {
|
|
104
|
+
requireNonNull(monthOfYear, 'monthOfYear');
|
|
105
|
+
ChronoField.MONTH_OF_YEAR.range().checkValidValue(monthOfYear, ChronoField.MONTH_OF_YEAR);
|
|
106
|
+
return Quarter.of(MathUtil.intDiv(monthOfYear - 1, 3) + 1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//-----------------------------------------------------------------------
|
|
110
|
+
/**
|
|
111
|
+
* Obtains an instance of `Quarter` from a temporal object.
|
|
112
|
+
*
|
|
113
|
+
* This obtains a quarter based on the specified temporal.
|
|
114
|
+
* A `TemporalAccessor` represents an arbitrary set of date and time information,
|
|
115
|
+
* which this factory converts to an instance of `Quarter`.
|
|
116
|
+
*
|
|
117
|
+
* The conversion extracts the {@link IsoFields.QUARTER_OF_YEAR} field.
|
|
118
|
+
* The extraction is only permitted if the temporal object has an ISO
|
|
119
|
+
* chronology, or can be converted to a `LocalDate`.
|
|
120
|
+
*
|
|
121
|
+
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
122
|
+
* allowing it to be used in queries via method reference, `Quarter.from`.
|
|
123
|
+
*
|
|
124
|
+
* @param {Temporal} temporal - the temporal-time object to convert, not null
|
|
125
|
+
* @return {Quarter} the quarter-of-year, not null
|
|
126
|
+
* @throws {DateTimeException} if unable to convert to a `Quarter`
|
|
127
|
+
*/
|
|
128
|
+
static from(temporal) {
|
|
129
|
+
if (temporal instanceof Quarter) {
|
|
130
|
+
return temporal;
|
|
131
|
+
} else if (temporal instanceof Month) {
|
|
132
|
+
const month = temporal;
|
|
133
|
+
return Quarter.of(MathUtil.intDiv(month.ordinal(), 3) + 1);
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
/* TODO: only IsoChronology for now
|
|
137
|
+
if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
|
|
138
|
+
temporal = LocalDate.from(temporal);
|
|
139
|
+
}
|
|
140
|
+
*/
|
|
141
|
+
const qoy = MathUtil.safeToInt(temporal.getLong(IsoFields.QUARTER_OF_YEAR));
|
|
142
|
+
return Quarter.of(qoy);
|
|
143
|
+
} catch (ex) {
|
|
144
|
+
throw new DateTimeException(`Unable to obtain Quarter from TemporalAccessor: ${temporal} of type ${temporal.constructor.name}`, ex);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
*
|
|
150
|
+
* @param {number} value
|
|
151
|
+
* @param {string} name
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
constructor(value, name) {
|
|
155
|
+
super();
|
|
156
|
+
this._value = MathUtil.safeToInt(value);
|
|
157
|
+
this._name = name;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//-----------------------------------------------------------------------
|
|
161
|
+
/**
|
|
162
|
+
* Gets the quarter-of-year `int` value.
|
|
163
|
+
*
|
|
164
|
+
* The values are numbered following the ISO-8601 standard,
|
|
165
|
+
* from 1 (Q1) to 4 (Q4).
|
|
166
|
+
*
|
|
167
|
+
* @return {number} the quarter-of-year, from 1 (Q1) to 4 (Q4)
|
|
168
|
+
*/
|
|
169
|
+
value() {
|
|
170
|
+
return this._value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
//-----------------------------------------------------------------------
|
|
174
|
+
/**
|
|
175
|
+
* Gets the textual representation, such as 'Q1' or '4th quarter'.
|
|
176
|
+
*
|
|
177
|
+
* This returns the textual name used to identify the quarter-of-year,
|
|
178
|
+
* suitable for presentation to the user.
|
|
179
|
+
* The parameters control the style of the returned text and the locale.
|
|
180
|
+
*
|
|
181
|
+
* If no textual mapping is found then the {@link #getValue() numeric value} is returned.
|
|
182
|
+
*
|
|
183
|
+
* @param {TextStyle} style - the length of the text required, not null
|
|
184
|
+
* @param {Locale} locale - the locale to use, not null
|
|
185
|
+
* @return {string} the text value of the day-of-week, not null
|
|
186
|
+
*/
|
|
187
|
+
// eslint-disable-next-line no-unused-vars
|
|
188
|
+
displayName(style, locale) {
|
|
189
|
+
throw new IllegalArgumentException('Pattern using (localized) text not implemented yet!');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//-----------------------------------------------------------------------
|
|
193
|
+
/**
|
|
194
|
+
* Checks if the specified field is supported.
|
|
195
|
+
*
|
|
196
|
+
* This checks if this quarter-of-year can be queried for the specified field.
|
|
197
|
+
* If false, then calling the {@link #range(TemporalField) range} and
|
|
198
|
+
* {@link #get(TemporalField) get} methods will throw an exception.
|
|
199
|
+
*
|
|
200
|
+
* If the field is {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR} then
|
|
201
|
+
* this method returns true.
|
|
202
|
+
* All `ChronoField` instances will return false.
|
|
203
|
+
*
|
|
204
|
+
* If the field is not a `ChronoField`, then the result of this method
|
|
205
|
+
* is obtained by invoking `TemporalField.isSupportedBy(TemporalAccessor)`
|
|
206
|
+
* passing `this` as the argument.
|
|
207
|
+
* Whether the field is supported is determined by the field.
|
|
208
|
+
*
|
|
209
|
+
* @param {TemporalField} field - the field to check, null returns false
|
|
210
|
+
* @return {boolean} true if the field is supported on this quarter-of-year, false if not
|
|
211
|
+
*/
|
|
212
|
+
isSupported(field) {
|
|
213
|
+
if (field === IsoFields.QUARTER_OF_YEAR) {
|
|
214
|
+
return true;
|
|
215
|
+
} else if (field instanceof ChronoField) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
return field != null && field.isSupportedBy(this);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Gets the range of valid values for the specified field.
|
|
223
|
+
*
|
|
224
|
+
* The range object expresses the minimum and maximum valid values for a field.
|
|
225
|
+
* This quarter is used to enhance the accuracy of the returned range.
|
|
226
|
+
* If it is not possible to return the range, because the field is not supported
|
|
227
|
+
* or for some other reason, an exception is thrown.
|
|
228
|
+
*
|
|
229
|
+
* If the field is {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR} then the
|
|
230
|
+
* range of the quarter-of-year, from 1 to 4, will be returned.
|
|
231
|
+
* All `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
|
|
232
|
+
*
|
|
233
|
+
* If the field is not a `ChronoField`, then the result of this method
|
|
234
|
+
* is obtained by invoking `TemporalField.rangeRefinedBy(TemporalAccessor)`
|
|
235
|
+
* passing `this` as the argument.
|
|
236
|
+
* Whether the range can be obtained is determined by the field.
|
|
237
|
+
*
|
|
238
|
+
* @param {TemporalField} field - the field to query the range for, not null
|
|
239
|
+
* @return {ValueRange} the range of valid values for the field, not null
|
|
240
|
+
* @throws {DateTimeException} if the range for the field cannot be obtained
|
|
241
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported
|
|
242
|
+
*/
|
|
243
|
+
range(field) {
|
|
244
|
+
requireNonNull(field, 'field');
|
|
245
|
+
if (field === IsoFields.QUARTER_OF_YEAR) {
|
|
246
|
+
return field.range();
|
|
247
|
+
} else if (field instanceof ChronoField) {
|
|
248
|
+
throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
|
|
249
|
+
}
|
|
250
|
+
return super.range(field);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Gets the value of the specified field from this quarter-of-year as an `int`.
|
|
255
|
+
*
|
|
256
|
+
* This queries this quarter for the value for the specified field.
|
|
257
|
+
* The returned value will always be within the valid range of values for the field.
|
|
258
|
+
* If it is not possible to return the value, because the field is not supported
|
|
259
|
+
* or for some other reason, an exception is thrown.
|
|
260
|
+
*
|
|
261
|
+
* If the field is {@link IsoFields.QUARTER_OF_YEAR} then the
|
|
262
|
+
* value of the quarter-of-year, from 1 to 4, will be returned.
|
|
263
|
+
* All `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
|
|
264
|
+
*
|
|
265
|
+
* If the field is not a `ChronoField`, then the result of this method
|
|
266
|
+
* is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
|
|
267
|
+
* passing `this` as the argument. Whether the value can be obtained,
|
|
268
|
+
* and what the value represents, is determined by the field.
|
|
269
|
+
*
|
|
270
|
+
* @param {TemporalField} field - the field to get, not null
|
|
271
|
+
* @return {number} the value for the field, within the valid range of values
|
|
272
|
+
* @throws {DateTimeException} if a value for the field cannot be obtained or
|
|
273
|
+
* the value is outside the range of valid values for the field
|
|
274
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported or
|
|
275
|
+
* the range of values exceeds an `int`
|
|
276
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
277
|
+
*/
|
|
278
|
+
get(field) {
|
|
279
|
+
requireNonNull(field, 'field');
|
|
280
|
+
requireInstance(field, TemporalField, 'field');
|
|
281
|
+
return this.range(field).checkValidIntValue(this.getLong(field), field);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Gets the value of the specified field from this quarter-of-year as a `long`.
|
|
286
|
+
*
|
|
287
|
+
* This queries this quarter for the value for the specified field.
|
|
288
|
+
* If it is not possible to return the value, because the field is not supported
|
|
289
|
+
* or for some other reason, an exception is thrown.
|
|
290
|
+
*
|
|
291
|
+
* If the field is {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR} then the
|
|
292
|
+
* value of the quarter-of-year, from 1 to 4, will be returned.
|
|
293
|
+
* All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
|
|
294
|
+
*
|
|
295
|
+
* If the field is not a `ChronoField`, then the result of this method
|
|
296
|
+
* is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
|
|
297
|
+
* passing `this` as the argument. Whether the value can be obtained,
|
|
298
|
+
* and what the value represents, is determined by the field.
|
|
299
|
+
*
|
|
300
|
+
* @param {TemporalField} field - the field to get, not null
|
|
301
|
+
* @return {number} the value for the field
|
|
302
|
+
* @throws {DateTimeException} if a value for the field cannot be obtained
|
|
303
|
+
* @throws {UnsupportedTemporalTypeException} if the field is not supported
|
|
304
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
305
|
+
*/
|
|
306
|
+
getLong(field) {
|
|
307
|
+
requireNonNull(field, 'field');
|
|
308
|
+
if (field === IsoFields.QUARTER_OF_YEAR) {
|
|
309
|
+
return this.value();
|
|
310
|
+
} else if (field instanceof ChronoField) {
|
|
311
|
+
throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
|
|
312
|
+
}
|
|
313
|
+
return field.getFrom(this);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
//-----------------------------------------------------------------------
|
|
317
|
+
/**
|
|
318
|
+
* Returns the quarter that is the specified number of quarters after this one.
|
|
319
|
+
*
|
|
320
|
+
* The calculation rolls around the end of the year from Q4 to Q1.
|
|
321
|
+
* The specified period may be negative.
|
|
322
|
+
*
|
|
323
|
+
* This instance is immutable and unaffected by this method call.
|
|
324
|
+
*
|
|
325
|
+
* @param {number} quarters - the quarters to add, positive or negative
|
|
326
|
+
* @return {Quarter} the resulting quarter, not null
|
|
327
|
+
*/
|
|
328
|
+
plus(quarters) {
|
|
329
|
+
const amount = MathUtil.intMod(quarters, 4);
|
|
330
|
+
return QUARTERS[(this.ordinal() + amount + 4) % 4];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Returns the quarter that is the specified number of quarters before this one.
|
|
335
|
+
*
|
|
336
|
+
* The calculation rolls around the start of the year from Q1 to Q4.
|
|
337
|
+
* The specified period may be negative.
|
|
338
|
+
*
|
|
339
|
+
* This instance is immutable and unaffected by this method call.
|
|
340
|
+
*
|
|
341
|
+
* @param {number} quarters - the quarters to subtract, positive or negative
|
|
342
|
+
* @return {Quarter} the resulting quarter, not null
|
|
343
|
+
*/
|
|
344
|
+
minus(quarters) {
|
|
345
|
+
return this.plus(-(MathUtil.intMod(quarters, 4)));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
//-----------------------------------------------------------------------
|
|
349
|
+
/**
|
|
350
|
+
* Gets the length of this quarter in days.
|
|
351
|
+
*
|
|
352
|
+
* This takes a flag to determine whether to return the length for a leap year or not.
|
|
353
|
+
*
|
|
354
|
+
* Q1 has 90 in a standard year and 91 days in a leap year.
|
|
355
|
+
* Q2 has 91 days.
|
|
356
|
+
* Q3 and Q4 have 92 days.
|
|
357
|
+
*
|
|
358
|
+
* @param {boolean} leapYear - true if the length is required for a leap year
|
|
359
|
+
* @return {number} the length of this month in days, from 90 to 92
|
|
360
|
+
*/
|
|
361
|
+
length(leapYear) {
|
|
362
|
+
switch (this) {
|
|
363
|
+
case Quarter.Q1:
|
|
364
|
+
return (leapYear ? 91 : 90);
|
|
365
|
+
case Quarter.Q2:
|
|
366
|
+
return 91;
|
|
367
|
+
default:
|
|
368
|
+
return 92;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
//-----------------------------------------------------------------------
|
|
373
|
+
/**
|
|
374
|
+
* Gets the first of the three months that this quarter refers to.
|
|
375
|
+
*
|
|
376
|
+
* Q1 will return January.
|
|
377
|
+
* Q2 will return April.
|
|
378
|
+
* Q3 will return July.
|
|
379
|
+
* Q4 will return October.
|
|
380
|
+
*
|
|
381
|
+
* To obtain the other two months of the quarter, simply use {@link Month#plus(long)}
|
|
382
|
+
* on the returned month.
|
|
383
|
+
*
|
|
384
|
+
* @return {Month} the first month in the quarter, not null
|
|
385
|
+
*/
|
|
386
|
+
firstMonth() {
|
|
387
|
+
switch (this) {
|
|
388
|
+
case Quarter.Q1:
|
|
389
|
+
return Month.JANUARY;
|
|
390
|
+
case Quarter.Q2:
|
|
391
|
+
return Month.APRIL;
|
|
392
|
+
case Quarter.Q3:
|
|
393
|
+
return Month.JULY;
|
|
394
|
+
case Quarter.Q4:
|
|
395
|
+
return Month.OCTOBER;
|
|
396
|
+
default:
|
|
397
|
+
throw new IllegalStateException('Unreachable');
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
//-----------------------------------------------------------------------
|
|
402
|
+
/**
|
|
403
|
+
* Queries this quarter-of-year using the specified query.
|
|
404
|
+
*
|
|
405
|
+
* This queries this quarter-of-year using the specified query strategy object.
|
|
406
|
+
* The `TemporalQuery` object defines the logic to be used to
|
|
407
|
+
* obtain the result. Read the documentation of the query to understand
|
|
408
|
+
* what the result of this method will be.
|
|
409
|
+
*
|
|
410
|
+
* The result of this method is obtained by invoking the
|
|
411
|
+
* {@link TemporalQuery.queryFrom(TemporalAccessor)} method on the
|
|
412
|
+
* specified query passing `this` as the argument.
|
|
413
|
+
*
|
|
414
|
+
* @param {TemporalQuery} query - the query to invoke, not null
|
|
415
|
+
* @return {*} the query result, null may be returned (defined by the query)
|
|
416
|
+
* @throws {DateTimeException} if unable to query (defined by the query)
|
|
417
|
+
* @throws {ArithmeticException} if numeric overflow occurs (defined by the query)
|
|
418
|
+
*/
|
|
419
|
+
query(query) {
|
|
420
|
+
requireNonNull(query, 'query');
|
|
421
|
+
requireInstance(query, TemporalQuery, 'query');
|
|
422
|
+
if (query === TemporalQueries.chronology()) {
|
|
423
|
+
return IsoChronology.INSTANCE;
|
|
424
|
+
} else if (query === TemporalQueries.precision()) {
|
|
425
|
+
return IsoFields.QUARTER_YEARS;
|
|
426
|
+
}
|
|
427
|
+
return super.query(query);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Adjusts the specified temporal object to have this quarter-of-year.
|
|
432
|
+
*
|
|
433
|
+
* This returns a temporal object of the same observable type as the input
|
|
434
|
+
* with the quarter-of-year changed to be the same as this.
|
|
435
|
+
*
|
|
436
|
+
* The adjustment is equivalent to using {@link Temporal.with}
|
|
437
|
+
* passing {@link IsoFields.QUARTER_OF_YEAR} as the field.
|
|
438
|
+
* If the specified temporal object does not use the ISO calendar system then
|
|
439
|
+
* a `DateTimeException` is thrown.
|
|
440
|
+
*
|
|
441
|
+
* In most cases, it is clearer to reverse the calling pattern by using
|
|
442
|
+
* {@link Temporal.with}:
|
|
443
|
+
* ```
|
|
444
|
+
* // these two lines are equivalent, but the second approach is recommended
|
|
445
|
+
* temporal = thisQuarter.adjustInto(temporal);
|
|
446
|
+
* temporal = temporal.with(thisQuarter);
|
|
447
|
+
* ```
|
|
448
|
+
*
|
|
449
|
+
* For example, given a date in May, the following are output:
|
|
450
|
+
* ```
|
|
451
|
+
* dateInMay.with(Q1); // three months earlier
|
|
452
|
+
* dateInMay.with(Q2); // no change
|
|
453
|
+
* dateInMay.with(Q3); // three months later
|
|
454
|
+
* dateInMay.with(Q4); // six months later
|
|
455
|
+
* ```
|
|
456
|
+
*
|
|
457
|
+
* This instance is immutable and unaffected by this method call.
|
|
458
|
+
*
|
|
459
|
+
* @param {Temporal} temporal - the target object to be adjusted, not null
|
|
460
|
+
* @return {Temporal} the adjusted object, not null
|
|
461
|
+
* @throws {DateTimeException} if unable to make the adjustment
|
|
462
|
+
* @throws {ArithmeticException} if numeric overflow occurs
|
|
463
|
+
*/
|
|
464
|
+
adjustInto(temporal) {
|
|
465
|
+
requireNonNull(temporal, 'temporal');
|
|
466
|
+
/* TODO: only IsoChronology for now
|
|
467
|
+
if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
|
|
468
|
+
throw new DateTimeException('Adjustment only supported on ISO date-time');
|
|
469
|
+
}
|
|
470
|
+
*/
|
|
471
|
+
return temporal.with(IsoFields.QUARTER_OF_YEAR, this.value());
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
//-----------------------------------------------------------------------
|
|
475
|
+
// Enum
|
|
476
|
+
//-----------------------------------------------------------------------
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
*
|
|
480
|
+
* @return {number}
|
|
481
|
+
*/
|
|
482
|
+
ordinal() {
|
|
483
|
+
return this._value - 1;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
*
|
|
488
|
+
* @return {string}
|
|
489
|
+
*/
|
|
490
|
+
name() {
|
|
491
|
+
return this._name;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
//-----------------------------------------------------------------------
|
|
495
|
+
// Comparable
|
|
496
|
+
//-----------------------------------------------------------------------
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Compares this Quarter to another Quarter.
|
|
500
|
+
*
|
|
501
|
+
* The comparison is based on the value of the Quarter.
|
|
502
|
+
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
503
|
+
*
|
|
504
|
+
* @param {Quarter} other - the other quarter to compare to, not null
|
|
505
|
+
* @return {number} the comparator value, negative if less, positive if greater
|
|
506
|
+
*/
|
|
507
|
+
compareTo(other) {
|
|
508
|
+
requireNonNull(other, 'other');
|
|
509
|
+
requireInstance(other, Quarter, 'other');
|
|
510
|
+
return this._value - other._value;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
//-----------------------------------------------------------------------
|
|
514
|
+
// Object
|
|
515
|
+
//-----------------------------------------------------------------------
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Outputs this quarter as a String, such as Q1
|
|
519
|
+
*
|
|
520
|
+
* @return {string} a string representation of this quarter, not null
|
|
521
|
+
*/
|
|
522
|
+
toString() {
|
|
523
|
+
return this.name();
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
*
|
|
528
|
+
* @return {boolean}
|
|
529
|
+
*/
|
|
530
|
+
equals(other){
|
|
531
|
+
return this === other;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* A hash code for this quarter.
|
|
536
|
+
*
|
|
537
|
+
* @return {number} a suitable hash code
|
|
538
|
+
*/
|
|
539
|
+
hashCode() {
|
|
540
|
+
return this._value;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
let QUARTERS;
|
|
545
|
+
|
|
546
|
+
export function _init() {
|
|
547
|
+
/**
|
|
548
|
+
* The singleton instance for the first quarter-of-year, from January to March.
|
|
549
|
+
* This has the numeric value of `1`.
|
|
550
|
+
*/
|
|
551
|
+
Quarter.Q1 = new Quarter(1, 'Q1');
|
|
552
|
+
/**
|
|
553
|
+
* The singleton instance for the second quarter-of-year, from April to June.
|
|
554
|
+
* This has the numeric value of `2`.
|
|
555
|
+
*/
|
|
556
|
+
Quarter.Q2 = new Quarter(2, 'Q2');
|
|
557
|
+
/**
|
|
558
|
+
* The singleton instance for the third quarter-of-year, from July to September.
|
|
559
|
+
* This has the numeric value of `3`.
|
|
560
|
+
*/
|
|
561
|
+
Quarter.Q3 = new Quarter(3, 'Q3');
|
|
562
|
+
/**
|
|
563
|
+
* The singleton instance for the fourth quarter-of-year, from October to December.
|
|
564
|
+
* This has the numeric value of `4`.
|
|
565
|
+
*/
|
|
566
|
+
Quarter.Q4 = new Quarter(4, 'Q4');
|
|
567
|
+
|
|
568
|
+
Quarter.FROM = createTemporalQuery('Quarter.FROM', (temporal) => {
|
|
569
|
+
return Quarter.from(temporal);
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
QUARTERS = [
|
|
573
|
+
Quarter.Q1,
|
|
574
|
+
Quarter.Q2,
|
|
575
|
+
Quarter.Q3,
|
|
576
|
+
Quarter.Q4
|
|
577
|
+
];
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// copied from packages/core/src/temporal/TemporalQuery.js
|
|
581
|
+
function createTemporalQuery(name, queryFromFunction) {
|
|
582
|
+
class ExtendedTemporalQuery extends TemporalQuery {
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
ExtendedTemporalQuery.prototype.queryFrom = queryFromFunction;
|
|
586
|
+
return new ExtendedTemporalQuery(name);
|
|
587
|
+
}
|