@js-joda/extra 0.9.1 → 0.10.1

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.
@@ -0,0 +1,1177 @@
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
+ import { ChronoField, ChronoUnit, Clock, DateTimeException, DateTimeFormatter, DateTimeFormatterBuilder, IllegalArgumentException, IsoChronology, IsoFields, LocalDate, SignStyle, Temporal, TemporalField, TemporalQueries, TemporalQuery, TemporalUnit, UnsupportedTemporalTypeException, ValueRange, Year, ZoneId } from '@js-joda/core';
8
+ import { Quarter } from './Quarter';
9
+
10
+ // TODO: hm... is this a good idea?? copied from joda currently, could we add a js-joda-utils module??
11
+ import { requireInstance, requireNonNull } from './assert';
12
+ import { _ as jodaInternal } from '@js-joda/core';
13
+
14
+ const MathUtil = jodaInternal.MathUtil;
15
+
16
+ /**
17
+ * A year-quarter in the ISO-8601 calendar system, such as `2007-Q2`.
18
+ *
19
+ * `YearQuarter` is an immutable date-time object that represents the combination
20
+ * of a year and quarter. Any field that can be derived from a year and quarter can be obtained.
21
+ * A quarter is defined by {@link Quarter} and {@link Month.firstMonthOfQuarter} - Q1, Q2, Q3 and Q4.
22
+ * Q1 is January to March, Q2 is April to June, Q3 is July to September and Q4 is October to December.
23
+ *
24
+ * This class does not store or represent a day, time or time-zone.
25
+ * For example, the value '2nd quarter 2007' can be stored in a `YearQuarter`.
26
+ *
27
+ * The ISO-8601 calendar system is the modern civil calendar system used today
28
+ * in most of the world. It is equivalent to the proleptic Gregorian calendar
29
+ * system, in which today's rules for leap years are applied for all time.
30
+ * For most applications written today, the ISO-8601 rules are entirely suitable.
31
+ * However, any application that makes use of historical dates, and requires them
32
+ * to be accurate will find the ISO-8601 approach unsuitable.
33
+ * Note that the ISO-8601 standard does not define or refer to quarters.
34
+ *
35
+ */
36
+ export class YearQuarter extends Temporal {
37
+ //-----------------------------------------------------------------------
38
+ /**
39
+ * function overloading for {@link YearQuarter.now}
40
+ * - if called with 0 argument {@link YearQuarter._now0} is executed,
41
+ * - if called with 1 argument and first argument is an instance of ZoneId, then {@link YearQuarter._nowZoneId} is executed,
42
+ * - otherwise {@link YearQuarter._nowClock} is executed
43
+ *
44
+ * @param {?(ZoneId|Clock)} zoneIdOrClock
45
+ * @return {YearQuarter}
46
+ */
47
+ static now(zoneIdOrClock) {
48
+ if (arguments.length === 0) {
49
+ return YearQuarter._now0();
50
+ } else if (arguments.length === 1 && zoneIdOrClock instanceof ZoneId) {
51
+ return YearQuarter._nowZoneId(zoneIdOrClock);
52
+ } else {
53
+ return YearQuarter._nowClock(zoneIdOrClock);
54
+ }
55
+ }
56
+
57
+ //-----------------------------------------------------------------------
58
+ /**
59
+ * Obtains the current year-quarter from the system clock in the default time-zone.
60
+ *
61
+ * This will query the {@link Clock.systemDefaultZone} system clock in the default
62
+ * time-zone to obtain the current year-quarter.
63
+ * The zone and offset will be set based on the time-zone in the clock.
64
+ *
65
+ * Using this method will prevent the ability to use an alternate clock for testing
66
+ * because the clock is hard-coded.
67
+ *
68
+ * @return {YearQuarter} the current year-quarter using the system clock and default time-zone, not null
69
+ * @protected
70
+ */
71
+ static _now0() {
72
+ return YearQuarter.now(Clock.systemDefaultZone());
73
+ }
74
+
75
+ /**
76
+ * Obtains the current year-quarter from the system clock in the specified time-zone.
77
+ *
78
+ * This will query the {@link Clock.system} to obtain the current year-quarter.
79
+ * Specifying the time-zone avoids dependence on the default time-zone.
80
+ *
81
+ * Using this method will prevent the ability to use an alternate clock for testing
82
+ * because the clock is hard-coded.
83
+ *
84
+ * @param {ZoneId} zone - the zone ID to use, not null
85
+ * @return {YearQuarter} the current year-quarter using the system clock, not null
86
+ * @protected
87
+ */
88
+ static _nowZoneId(zone) {
89
+ return YearQuarter.now(Clock.system(zone));
90
+ }
91
+
92
+ /**
93
+ * Obtains the current year-quarter from the specified clock.
94
+ *
95
+ * This will query the specified clock to obtain the current year-quarter.
96
+ * Using this method allows the use of an alternate clock for testing.
97
+ * The alternate clock may be introduced using {@link Clock} dependency injection.
98
+ *
99
+ * @param {Clock} clock - the clock to use, not null
100
+ * @return {YearQuarter} the current year-quarter, not null
101
+ */
102
+ static _nowClock(clock) {
103
+ const now = LocalDate.now(clock); // called once
104
+ return YearQuarter.of(now.getYear(), Quarter.from(now.getMonth()));
105
+ }
106
+
107
+ //-----------------------------------------------------------------------
108
+ /**
109
+ * function overloading for {@link YearQuarter.of}
110
+ * - if called with {Year} and {Quarter} {@link YearQuarter._ofYearQuarter} is executed,
111
+ * - if called with {Year} and {number} {@link YearQuarter._ofYearInt} is executed,
112
+ * - if called with {number} and {Quarter} {@link YearQuarter._ofIntQuarter} is executed,
113
+ * - if called with {number} and {number} {@link YearQuarter._ofIntInt} is executed,
114
+ * otherwise throws IllegalArgumentException.
115
+ *
116
+ * @param {Year|number} year
117
+ * @param {Quarter|number} quarter
118
+ * @return {YearQuarter}
119
+ */
120
+ static of(year, quarter) {
121
+ if (year instanceof Year && quarter instanceof Quarter) {
122
+ return YearQuarter._ofYearQuarter(year, quarter);
123
+ }
124
+ if (year instanceof Year && typeof quarter === 'number') {
125
+ return YearQuarter._ofYearInt(year, quarter);
126
+ }
127
+ if (typeof year === 'number' && quarter instanceof Quarter) {
128
+ return YearQuarter._ofIntQuarter(year, quarter);
129
+ }
130
+ if (typeof year === 'number' && typeof quarter === 'number') {
131
+ return YearQuarter._ofIntInt(year, quarter);
132
+ }
133
+ // FIXME
134
+ const yearMessage = `year must be an instance of Year or number but is ${year.constructor.name}`;
135
+ const quarterMessage = `quarter must be an instance of Quarter or number but is ${quarter.constructor.name}`;
136
+ throw new IllegalArgumentException(`${yearMessage} and ${quarterMessage}`);
137
+ }
138
+
139
+ //-----------------------------------------------------------------------
140
+ /**
141
+ * Obtains an instance of `YearQuarter` from a year and quarter.
142
+ *
143
+ * @param {Year} year - the year to represent, not null
144
+ * @param {Quarter} quarter - the quarter-of-year to represent, not null
145
+ * @return {YearQuarter} the year-quarter, not null
146
+ * @protected
147
+ */
148
+ // FIXME ambiguous name?
149
+ static _ofYearQuarter(year, quarter) {
150
+ return YearQuarter.of(year.value(), quarter);
151
+ }
152
+
153
+ /**
154
+ * Obtains an instance of `YearQuarter` from a year and quarter.
155
+ *
156
+ * @param {Year} year - the year to represent, not null
157
+ * @param {number} quarter - the quarter-of-year to represent, from 1 to 4
158
+ * @return {YearQuarter} the year-quarter, not null
159
+ * @throws {DateTimeException} if the quarter value is invalid
160
+ * @protected
161
+ */
162
+ static _ofYearInt(year, quarter) {
163
+ return YearQuarter.of(year.value(), Quarter.of(quarter));
164
+ }
165
+
166
+ /**
167
+ * Obtains an instance of `YearQuarter` from a year and quarter.
168
+ *
169
+ * @param {number} year - the year to represent, from MIN_YEAR to MAX_YEAR
170
+ * @param {Quarter} quarter - the quarter-of-year to represent, not null
171
+ * @return {YearQuarter} the year-quarter, not null
172
+ * @throws {DateTimeException} if the year value is invalid
173
+ * @protected
174
+ */
175
+ static _ofIntQuarter(year, quarter) {
176
+ ChronoField.YEAR.checkValidValue(year);
177
+ requireNonNull(quarter, 'quarter');
178
+ return new YearQuarter(year, quarter);
179
+ }
180
+
181
+ /**
182
+ * Obtains an instance of `YearQuarter` from a year and quarter.
183
+ *
184
+ * @param {number} year - the year to represent, from MIN_YEAR to MAX_YEAR
185
+ * @param {number} quarter - the quarter-of-year to represent, from 1 to 4
186
+ * @return {YearQuarter} the year-quarter, not null
187
+ * @throws {DateTimeException} if either field value is invalid
188
+ * @protected
189
+ */
190
+ static _ofIntInt(year, quarter) {
191
+ ChronoField.YEAR.checkValidValue(year);
192
+ return new YearQuarter(year, Quarter.of(quarter));
193
+ }
194
+
195
+ //-----------------------------------------------------------------------
196
+ /**
197
+ * Obtains an instance of `YearQuarter` from a temporal object.
198
+ *
199
+ * This obtains a year-quarter based on the specified temporal.
200
+ * A `TemporalAccessor` represents an arbitrary set of date and time information,
201
+ * which this factory converts to an instance of `YearQuarter`.
202
+ *
203
+ * The conversion extracts the {@link ChronoField.YEAR} and
204
+ * {@link IsoFields.QUARTER_OF_YEAR} fields.
205
+ * The extraction is only permitted if the temporal object has an ISO
206
+ * chronology, or can be converted to a `LocalDate`.
207
+ *
208
+ * This method matches the signature of the functional interface {@link TemporalQuery}
209
+ * allowing it to be used in queries via method reference, `YearQuarter.FROM`.
210
+ *
211
+ * @param {TemporalAccessor} temporal - the temporal object to convert, not null
212
+ * @return {YearQuarter} the year-quarter, not null
213
+ * @throws {DateTimeException} if unable to convert to a `YearQuarter`
214
+ */
215
+ static from(temporal) {
216
+ if (temporal instanceof YearQuarter) {
217
+ return temporal;
218
+ }
219
+ requireNonNull(temporal, 'temporal');
220
+ try {
221
+ /* TODO: only IsoChronology for now
222
+ if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {
223
+ temporal = LocalDate.from(temporal);
224
+ }
225
+ */
226
+ // need to use getLong() as JDK Parsed class get() doesn't work properly
227
+ const year = MathUtil.safeToInt(temporal.getLong(ChronoField.YEAR));
228
+ const qoy = MathUtil.safeToInt(temporal.getLong(IsoFields.QUARTER_OF_YEAR));
229
+ return YearQuarter.of(year, qoy);
230
+ } catch (ex) {
231
+ throw new DateTimeException(`Unable to obtain YearQuarter from TemporalAccessor: ${temporal} of type ${temporal.constructor.name}`, ex);
232
+ }
233
+ }
234
+
235
+ //-----------------------------------------------------------------------
236
+ /**
237
+ * Obtains an instance of `YearQuarter` from a text string using a specific formatter.
238
+ *
239
+ * The text is parsed using the formatter, returning a year-quarter.
240
+ *
241
+ * @param {string} text - the text to parse, not null
242
+ * @param {DateTimeFormatter} [formatter=YearQuarter.PARSER] - the formatter to use, default is {@link YearQuarter.PARSER}
243
+ * @return {YearQuarter} the parsed year-quarter, not null
244
+ * @throws {DateTimeParseException} if the text cannot be parsed
245
+ */
246
+ static parse(text, formatter = YearQuarter.PARSER) {
247
+ requireNonNull(formatter, 'formatter');
248
+ requireInstance(formatter, DateTimeFormatter, 'formatter');
249
+ return formatter.parse(text, YearQuarter.FROM);
250
+ }
251
+
252
+ //-----------------------------------------------------------------------
253
+ /**
254
+ * Constructor.
255
+ *
256
+ * @param {number} year - the year to represent, validated from MIN_YEAR to MAX_YEAR
257
+ * @param {Quarter} quarter - the quarter-of-year to represent, validated not null
258
+ * @private
259
+ */
260
+ constructor(year, quarter) {
261
+ super();
262
+ this._year = MathUtil.safeToInt(year);
263
+ this._quarter = requireInstance(quarter, Quarter, 'Quarter');
264
+ }
265
+
266
+ /**
267
+ * Returns a copy of this year-quarter with the new year and quarter, checking
268
+ * to see if a new object is in fact required.
269
+ *
270
+ * @param {number} newYear - the year to represent, validated from MIN_YEAR to MAX_YEAR
271
+ * @param {Quarter} newQuarter - the quarter-of-year to represent, validated not null
272
+ * @return {YearQuarter} the year-quarter, not null
273
+ * @private
274
+ */
275
+ _with(newYear, newQuarter) {
276
+ if (this._year === newYear && this._quarter === newQuarter) {
277
+ return this;
278
+ }
279
+ return new YearQuarter(newYear, newQuarter);
280
+ }
281
+
282
+ /**
283
+ * function overloading for {@link YearWeek.isSupported}
284
+ * - if called with an instance of {@link TemporalField}, then {@link YearWeek.isSupportedField} is executed,
285
+ * - if called with an instance of {@link TemporalUnit}, then {@link YearWeek.isSupportedUnit} is executed,
286
+ * - otherwise {@link IllegalArgumentException} is thrown.
287
+ *
288
+ * @param {TemporalField|TemporalUnit} fieldOrUnit
289
+ * @return {boolean}
290
+ */
291
+ isSupported(fieldOrUnit) {
292
+ if (fieldOrUnit instanceof TemporalField) {
293
+ return this._isSupportedField(fieldOrUnit);
294
+ }
295
+ if (fieldOrUnit instanceof TemporalUnit) {
296
+ return this._isSupportedUnit(fieldOrUnit);
297
+ }
298
+ if (fieldOrUnit == null) {
299
+ return false;
300
+ }
301
+ throw new IllegalArgumentException(`fieldOrUnit must be an instance of TemporalField or TemporalUnit, but is ${fieldOrUnit.constructor.name}`);
302
+ }
303
+
304
+ //-----------------------------------------------------------------------
305
+ /**
306
+ * Checks if the specified field is supported.
307
+ *
308
+ * This checks if this year-quarter can be queried for the specified field.
309
+ * If false, then calling the {@link YearQuarter.range},
310
+ * {@link YearQuarter.get} and {@link YearQuarter.with}
311
+ * methods will throw an exception.
312
+ *
313
+ * If the field is a {@link ChronoField} then the query is implemented here.
314
+ * The supported fields are:
315
+ *
316
+ * - `QUARTER_OF_YEAR`
317
+ * - `YEAR_OF_ERA`
318
+ * - `YEAR`
319
+ * - `ERA`
320
+ *
321
+ * All other `ChronoField` instances will return false.
322
+ *
323
+ * If the field is not a `ChronoField`, then the result of this method
324
+ * is obtained by invoking `TemporalField.isSupportedBy(TemporalAccessor)`
325
+ * passing `this` as the argument.
326
+ * Whether the field is supported is determined by the field.
327
+ *
328
+ * @param {TemporalField} field - the field to check, null returns false
329
+ * @return {boolean} true if the field is supported on this year-quarter, false if not
330
+ * @protected
331
+ */
332
+ _isSupportedField(field) {
333
+ if (field === IsoFields.QUARTER_OF_YEAR) {
334
+ return true;
335
+ } else if (field instanceof ChronoField) {
336
+ return field === ChronoField.YEAR || field === ChronoField.YEAR_OF_ERA || field === ChronoField.ERA;
337
+ }
338
+ return field != null && field.isSupportedBy(this);
339
+ }
340
+
341
+ /**
342
+ * Checks if the specified unit is supported.
343
+ *
344
+ * This checks if the specified unit can be added to, or subtracted from, this year-quarter.
345
+ * If false, then calling the {@link YearQuarter.plus} and
346
+ * {@link YearQuarter.minus} methods will throw an exception.
347
+ *
348
+ * If the unit is a {@link ChronoUnit} then the query is implemented here.
349
+ * The supported units are:
350
+ *
351
+ * - `QUARTER_YEARS`
352
+ * - `YEARS`
353
+ * - `DECADES`
354
+ * - `CENTURIES`
355
+ * - `MILLENNIA`
356
+ * - `ERAS`
357
+ *
358
+ * All other `ChronoUnit` instances will return false.
359
+ *
360
+ * If the unit is not a `ChronoUnit`, then the result of this method
361
+ * is obtained by invoking `TemporalUnit.isSupportedBy(Temporal)`
362
+ * passing `this` as the argument.
363
+ * Whether the unit is supported is determined by the unit.
364
+ *
365
+ * @param {TemporalUnit} unit - the unit to check, null returns false
366
+ * @return {boolean} true if the unit can be added/subtracted, false if not
367
+ * @protected
368
+ */
369
+ _isSupportedUnit(unit) {
370
+ if (unit === IsoFields.QUARTER_YEARS) {
371
+ return true;
372
+ } else if (unit instanceof ChronoUnit) {
373
+ return unit === ChronoUnit.YEARS || unit === ChronoUnit.DECADES || unit === ChronoUnit.CENTURIES || unit === ChronoUnit.MILLENNIA || unit === ChronoUnit.ERAS;
374
+ }
375
+ return unit != null && unit.isSupportedBy(this);
376
+ }
377
+
378
+ //-----------------------------------------------------------------------
379
+ /**
380
+ * Gets the range of valid values for the specified field.
381
+ *
382
+ * The range object expresses the minimum and maximum valid values for a field.
383
+ * This year-quarter is used to enhance the accuracy of the returned range.
384
+ * If it is not possible to return the range, because the field is not supported
385
+ * or for some other reason, an exception is thrown.
386
+ *
387
+ * If the field is a {@link ChronoField} then the query is implemented here.
388
+ * The {@link YearQuarter.isSupported} supported fields will return
389
+ * appropriate range instances.
390
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
391
+ *
392
+ * If the field is not a `ChronoField`, then the result of this method
393
+ * is obtained by invoking `TemporalField.rangeRefinedBy(TemporalAccessor)`
394
+ * passing `this` as the argument.
395
+ * Whether the range can be obtained is determined by the field.
396
+ *
397
+ * @param {TemporalField} field - the field to query the range for, not null
398
+ * @return {ValueRange} the range of valid values for the field, not null
399
+ * @throws {DateTimeException} if the range for the field cannot be obtained
400
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
401
+ */
402
+ range(field) {
403
+ requireNonNull(field, 'field');
404
+ requireInstance(field, TemporalField, 'field');
405
+ if (field === IsoFields.QUARTER_OF_YEAR) {
406
+ return IsoFields.QUARTER_OF_YEAR.range();
407
+ }
408
+ if (field === ChronoField.YEAR_OF_ERA) {
409
+ return (this.year() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
410
+ }
411
+ return super.range(field);
412
+ }
413
+
414
+ /**
415
+ * Gets the value of the specified field from this year-quarter as an `int`.
416
+ *
417
+ * This queries this year-quarter for the value for the specified field.
418
+ * The returned value will always be within the valid range of values for the field.
419
+ * If it is not possible to return the value, because the field is not supported
420
+ * or for some other reason, an exception is thrown.
421
+ *
422
+ * If the field is a {@link ChronoField} then the query is implemented here.
423
+ * The {@link YearQuarter.isSupported} supported fields will return valid
424
+ * values based on this year-quarter,.
425
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
426
+ *
427
+ * If the field is not a `ChronoField`, then the result of this method
428
+ * is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
429
+ * passing `this` as the argument. Whether the value can be obtained,
430
+ * and what the value represents, is determined by the field.
431
+ *
432
+ * @param {TemporalField} field - the field to get, not null
433
+ * @return {number} the value for the field
434
+ * @throws {DateTimeException} if a value for the field cannot be obtained or
435
+ * the value is outside the range of valid values for the field
436
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported or
437
+ * the range of values exceeds an `int`
438
+ * @throws {ArithmeticException} if numeric overflow occurs
439
+ */
440
+ get(field) {
441
+ requireNonNull(field, 'field');
442
+ requireInstance(field, TemporalField, 'field');
443
+ return this.range(field).checkValidIntValue(this.getLong(field), field);
444
+ }
445
+
446
+ /**
447
+ * Gets the value of the specified field from this year-quarter as a `long`.
448
+ *
449
+ * This queries this year-quarter for the value for the specified field.
450
+ * If it is not possible to return the value, because the field is not supported
451
+ * or for some other reason, an exception is thrown.
452
+ *
453
+ * If the field is a {@link ChronoField} then the query is implemented here.
454
+ * The {@link YearQuarter.isSupported} supported fields will return valid
455
+ * values based on this year-quarter.
456
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
457
+ *
458
+ * If the field is not a `ChronoField`, then the result of this method
459
+ * is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
460
+ * passing `this` as the argument. Whether the value can be obtained,
461
+ * and what the value represents, is determined by the field.
462
+ *
463
+ * @param {TemporalField} field - the field to get, not null
464
+ * @return {number} the value for the field
465
+ * @throws {DateTimeException} if a value for the field cannot be obtained
466
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
467
+ * @throws {ArithmeticException} if numeric overflow occurs
468
+ */
469
+ getLong(field) {
470
+ requireNonNull(field, 'field');
471
+ requireInstance(field, TemporalField, 'field');
472
+ if (field === IsoFields.QUARTER_OF_YEAR) {
473
+ return this._quarter.value();
474
+ } else if (field instanceof ChronoField) {
475
+ switch (field) {
476
+ case ChronoField.YEAR_OF_ERA:
477
+ return (this._year < 1 ? 1 - this._year : this._year);
478
+ case ChronoField.YEAR:
479
+ return this._year;
480
+ case ChronoField.ERA:
481
+ return (this._year < 1 ? 0 : 1);
482
+ default:
483
+ throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
484
+ }
485
+ }
486
+ return super.get(field);
487
+ }
488
+
489
+ /**
490
+ * @private
491
+ */
492
+ _prolepticQuarter() {
493
+ return this._year * 4 + (this._quarter.value() - 1);
494
+ }
495
+
496
+ //-----------------------------------------------------------------------
497
+ /**
498
+ * Gets the year field.
499
+ *
500
+ * This method returns the primitive `int` value for the year.
501
+ *
502
+ * The year returned by this method is proleptic as per `get(YEAR)`.
503
+ *
504
+ * @return {number} the year, from MIN_YEAR to MAX_YEAR
505
+ */
506
+ year() {
507
+ return this._year;
508
+ }
509
+
510
+ /**
511
+ * Gets the quarter-of-year field from 1 to 4.
512
+ *
513
+ * This method returns the quarter as an `int` from 1 to 4.
514
+ * Application code is frequently clearer if the enum {@link Quarter}
515
+ * is used by calling {@link YearQuarter.getQuarter}.
516
+ *
517
+ * @return {number} the quarter-of-year, from 1 to 4
518
+ * @see {@link YearQuarter.quarter}
519
+ */
520
+ quarterValue() {
521
+ return this._quarter.value();
522
+ }
523
+
524
+ /**
525
+ * Gets the quarter-of-year field using the `Quarter` enum.
526
+ *
527
+ * This method returns the enum {@link Quarter} for the quarter.
528
+ * This avoids confusion as to what `int` values mean.
529
+ * If you need access to the primitive `int` value then the enum
530
+ * provides the {@link Quarter.value}.
531
+ *
532
+ * @return {Quarter} the quarter-of-year, not null
533
+ * @see {@link YearQuarter.quarterValue}
534
+ */
535
+ quarter() {
536
+ return this._quarter;
537
+ }
538
+
539
+ //-----------------------------------------------------------------------
540
+ /**
541
+ * Checks if the year is a leap year, according to the ISO proleptic
542
+ * calendar system rules.
543
+ *
544
+ * This method applies the current rules for leap years across the whole time-line.
545
+ * In general, a year is a leap year if it is divisible by four without
546
+ * remainder. However, years divisible by 100, are not leap years, with
547
+ * the exception of years divisible by 400 which are.
548
+ *
549
+ * For example, 1904 is a leap year it is divisible by 4.
550
+ * 1900 was not a leap year as it is divisible by 100, however 2000 was a
551
+ * leap year as it is divisible by 400.
552
+ *
553
+ * The calculation is proleptic - applying the same rules into the far future and far past.
554
+ * This is historically inaccurate, but is correct for the ISO-8601 standard.
555
+ *
556
+ * @return {boolean} true if the year is leap, false otherwise
557
+ */
558
+ isLeapYear() {
559
+ return IsoChronology.isLeapYear(this._year);
560
+ }
561
+
562
+ /**
563
+ * Checks if the day-of-quarter is valid for this year-quarter.
564
+ *
565
+ * This method checks whether this year and quarter and the input day form
566
+ * a valid date.
567
+ *
568
+ * @param {number} dayOfQuarter - the day-of-quarter to validate, from 1 to 92, invalid value returns false
569
+ * @return {boolean} true if the day is valid for this year-quarter
570
+ */
571
+ isValidDay(dayOfQuarter) {
572
+ return dayOfQuarter >= 1 && dayOfQuarter <= this.lengthOfQuarter();
573
+ }
574
+
575
+ /**
576
+ * Returns the length of the quarter, taking account of the year.
577
+ *
578
+ * This returns the length of the quarter in days.
579
+ *
580
+ * @return {number} the length of the quarter in days, from 90 to 92
581
+ */
582
+ lengthOfQuarter() {
583
+ return this._quarter.length(this.isLeapYear());
584
+ }
585
+
586
+ /**
587
+ * Returns the length of the year.
588
+ *
589
+ * This returns the length of the year in days, either 365 or 366.
590
+ *
591
+ * @return {number} 366 if the year is leap, 365 otherwise
592
+ */
593
+ lengthOfYear() {
594
+ return (this.isLeapYear() ? 366 : 365);
595
+ }
596
+
597
+ //-----------------------------------------------------------------------
598
+ /**
599
+ * Returns an adjusted copy of this year-quarter.
600
+ *
601
+ * This returns a {@code YearQuarter} based on this one, with the year-quarter adjusted.
602
+ * The adjustment takes place using the specified adjuster strategy object.
603
+ * Read the documentation of the adjuster to understand what adjustment will be made.
604
+ *
605
+ * A simple adjuster might simply set the one of the fields, such as the year field.
606
+ * A more complex adjuster might set the year-quarter to the next quarter that
607
+ * Halley's comet will pass the Earth.
608
+ *
609
+ * The result of this method is obtained by invoking the
610
+ * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
611
+ * specified adjuster passing {@code this} as the argument.
612
+ *
613
+ * This instance is immutable and unaffected by this method call.
614
+ *
615
+ * @param {TemporalAdjuster} adjuster - the adjuster to use, not null
616
+ * @return {YearQuarter} based on {@code this} with the adjustment made, not null
617
+ * @throws {DateTimeException} if the adjustment cannot be made
618
+ * @throws {ArithmeticException} if numeric overflow occurs
619
+ */
620
+ _withAdjuster(adjuster) {
621
+ // optimizations
622
+ if (adjuster instanceof YearQuarter) {
623
+ return adjuster;
624
+ }
625
+ return super._withAdjuster(adjuster);
626
+ }
627
+
628
+ /**
629
+ * Returns a copy of this year-quarter with the specified field set to a new value.
630
+ *
631
+ * This returns a `YearQuarter` based on this one, with the value
632
+ * for the specified field changed.
633
+ * This can be used to change any supported field, such as the year or quarter.
634
+ * If it is not possible to set the value, because the field is not supported or for
635
+ * some other reason, an exception is thrown.
636
+ *
637
+ * If the field is a {@link ChronoField} then the adjustment is implemented here.
638
+ * The supported fields behave as follows:
639
+ * - `QUARTER_OF_YEAR` -
640
+ * Returns a `YearQuarter` with the specified quarter-of-year.
641
+ * The year will be unchanged.
642
+ * - `YEAR_OF_ERA` -
643
+ * Returns a `YearQuarter` with the specified year-of-era
644
+ * The quarter and era will be unchanged.
645
+ * - `YEAR` -
646
+ * Returns a `YearQuarter` with the specified year.
647
+ * The quarter will be unchanged.
648
+ * - `ERA` -
649
+ * Returns a `YearQuarter` with the specified era.
650
+ * The quarter and year-of-era will be unchanged.
651
+ *
652
+ * In all cases, if the new value is outside the valid range of values for the field
653
+ * then a `DateTimeException` will be thrown.
654
+ *
655
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
656
+ *
657
+ * If the field is not a `ChronoField`, then the result of this method
658
+ * is obtained by invoking `TemporalField.adjustInto(Temporal, long)`
659
+ * passing `this` as the argument. In this case, the field determines
660
+ * whether and how to adjust the instant.
661
+ *
662
+ * This instance is immutable and unaffected by this method call.
663
+ *
664
+ * @param {TemporalField} field - the field to set in the result, not null
665
+ * @param {number} newValue - the new value of the field in the result
666
+ * @return {YearQuarter} a `YearQuarter` based on `this` with the specified field set, not null
667
+ * @throws {DateTimeException} if the field cannot be set
668
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
669
+ * @throws {ArithmeticException} if numeric overflow occurs
670
+ */
671
+ _withField(field, newValue) {
672
+ requireNonNull(field, 'field');
673
+ requireInstance(field, TemporalField, 'field');
674
+ if (field === IsoFields.QUARTER_OF_YEAR) {
675
+ return this.withQuarter(IsoFields.QUARTER_OF_YEAR.range().checkValidIntValue(newValue, IsoFields.QUARTER_OF_YEAR));
676
+ } else if (field instanceof ChronoField) {
677
+ field.checkValidValue(newValue);
678
+ switch (field) {
679
+ case ChronoField.YEAR_OF_ERA:
680
+ return this.withYear(this._year < 1 ? 1 - newValue : newValue);
681
+ case ChronoField.YEAR:
682
+ return this.withYear(newValue);
683
+ case ChronoField.ERA:
684
+ return this.getLong(ChronoField.ERA) === newValue ? this : this.withYear(1 - this._year);
685
+ default:
686
+ throw new UnsupportedTemporalTypeException(`Unsupported field: ${field}`);
687
+ }
688
+ }
689
+ return field.adjustInto(this, newValue);
690
+ }
691
+
692
+ //-----------------------------------------------------------------------
693
+ /**
694
+ * Returns a copy of this `YearQuarter` with the year altered.
695
+ *
696
+ * This instance is immutable and unaffected by this method call.
697
+ *
698
+ * @param {number} year - the year to set in the returned year-quarter, from MIN_YEAR to MAX_YEAR
699
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the requested year, not null
700
+ * @throws {DateTimeException} if the year value is invalid
701
+ */
702
+ withYear(year) {
703
+ ChronoField.YEAR.checkValidValue(year);
704
+ return this._with(year, this._quarter);
705
+ }
706
+
707
+ /**
708
+ * Returns a copy of this `YearQuarter` with the quarter-of-year altered.
709
+ *
710
+ * This instance is immutable and unaffected by this method call.
711
+ *
712
+ * @param {number} quarter - the quarter-of-year to set in the returned year-quarter, from 1 to 4
713
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the requested quarter, not null
714
+ * @throws {DateTimeException} if the quarter-of-year value is invalid
715
+ */
716
+ withQuarter(quarter) {
717
+ IsoFields.QUARTER_OF_YEAR.range().checkValidValue(quarter, IsoFields.QUARTER_OF_YEAR);
718
+ return this._with(this._year, Quarter.of(quarter));
719
+ }
720
+
721
+ /**
722
+ * Returns a copy of this year-quarter with the specified amount added.
723
+ *
724
+ * This returns a `YearQuarter` based on this one, with the amount
725
+ * in terms of the unit added. If it is not possible to add the amount, because the
726
+ * unit is not supported or for some other reason, an exception is thrown.
727
+ *
728
+ * If the field is a {@link ChronoUnit} then the addition is implemented here.
729
+ * The supported fields behave as follows:
730
+ *
731
+ * - `QUARTER_YEARS` -
732
+ * Returns a `YearQuarter` with the specified number of quarters added.
733
+ * This is equivalent to {@link YearQuarter.plusQuarters}.
734
+ * - `YEARS` -
735
+ * Returns a `YearQuarter` with the specified number of years added.
736
+ * This is equivalent to {@link YearQuarter.plusYears}.
737
+ * - `DECADES` -
738
+ * Returns a `YearQuarter` with the specified number of decades added.
739
+ * This is equivalent to calling {@link YearQuarter.plusYears} with the amount
740
+ * multiplied by 10.
741
+ * - `CENTURIES` -
742
+ * Returns a `YearQuarter` with the specified number of centuries added.
743
+ * This is equivalent to calling {@link YearQuarter.plusYears} with the amount
744
+ * multiplied by 100.
745
+ * - `MILLENNIA` -
746
+ * Returns a `YearQuarter` with the specified number of millennia added.
747
+ * This is equivalent to calling {@link YearQuarter.plusYears} with the amount
748
+ * multiplied by 1,000.
749
+ * - `ERAS` -
750
+ * Returns a `YearQuarter` with the specified number of eras added.
751
+ * Only two eras are supported so the amount must be one, zero or minus one.
752
+ * If the amount is non-zero then the year is changed such that the year-of-era
753
+ * is unchanged.
754
+ *
755
+ * All other `ChronoUnit` instances will throw an `UnsupportedTemporalTypeException`.
756
+ *
757
+ * If the field is not a `ChronoUnit`, then the result of this method
758
+ * is obtained by invoking `TemporalUnit.addTo(Temporal, long)`
759
+ * passing `this` as the argument. In this case, the unit determines
760
+ * whether and how to perform the addition.
761
+ *
762
+ * This instance is immutable and unaffected by this method call.
763
+ *
764
+ * @param {number} amountToAdd - the amount of the unit to add to the result, may be negative
765
+ * @param {TemporalUnit} unit - the unit of the amount to add, not null
766
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the specified amount added, not null
767
+ * @throws {DateTimeException} if the addition cannot be made
768
+ * @throws {UnsupportedTemporalTypeException} if the unit is not supported
769
+ * @throws {ArithmeticException} if numeric overflow occurs
770
+ */
771
+ _plusUnit(amountToAdd, unit) {
772
+ if (unit === IsoFields.QUARTER_YEARS) {
773
+ return this.plusQuarters(amountToAdd);
774
+ } else if (unit instanceof ChronoUnit) {
775
+ switch (unit) {
776
+ case ChronoUnit.YEARS:
777
+ return this.plusYears(amountToAdd);
778
+ case ChronoUnit.DECADES:
779
+ return this.plusYears(MathUtil.safeMultiply(amountToAdd, 10));
780
+ case ChronoUnit.CENTURIES:
781
+ return this.plusYears(MathUtil.safeMultiply(amountToAdd, 100));
782
+ case ChronoUnit.MILLENNIA:
783
+ return this.plusYears(MathUtil.safeMultiply(amountToAdd, 1000));
784
+ case ChronoUnit.ERAS:
785
+ return this.with(ChronoField.ERA, MathUtil.safeAdd(this.getLong(ChronoField.ERA), amountToAdd));
786
+ default:
787
+ throw new UnsupportedTemporalTypeException(`Unsupported unit: ${unit}`);
788
+ }
789
+ }
790
+ return unit.addTo(this, amountToAdd);
791
+ }
792
+
793
+ /**
794
+ * Returns a copy of this year-quarter with the specified period in years added.
795
+ *
796
+ * This instance is immutable and unaffected by this method call.
797
+ *
798
+ * @param {number} yearsToAdd - the years to add, may be negative
799
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the years added, not null
800
+ * @throws {DateTimeException} if the result exceeds the supported range
801
+ */
802
+ plusYears(yearsToAdd) {
803
+ if (yearsToAdd === 0) {
804
+ return this;
805
+ }
806
+ const newYear = ChronoField.YEAR.checkValidIntValue(this._year + yearsToAdd); // safe overflow
807
+ return this._with(newYear, this._quarter);
808
+ }
809
+
810
+ /**
811
+ * Returns a copy of this year-quarter with the specified period in quarters added.
812
+ *
813
+ * This instance is immutable and unaffected by this method call.
814
+ *
815
+ * @param {number} quartersToAdd - the quarters to add, may be negative
816
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the quarters added, not null
817
+ * @throws {DateTimeException} if the result exceeds the supported range
818
+ */
819
+ plusQuarters(quartersToAdd) {
820
+ if (quartersToAdd === 0) {
821
+ return this;
822
+ }
823
+ const quarterCount = this._year * 4 + (this._quarter.value() - 1);
824
+ const calcQuarters = quarterCount + quartersToAdd; // safe overflow
825
+ const newYear = ChronoField.YEAR.checkValidIntValue(MathUtil.intDiv(calcQuarters, 4));
826
+ const newQuarter = MathUtil.floorMod(calcQuarters, 4) + 1;
827
+ return this._with(newYear, Quarter.of(newQuarter));
828
+ }
829
+
830
+ /**
831
+ * Returns a copy of this year-quarter with the specified period in years subtracted.
832
+ *
833
+ * This instance is immutable and unaffected by this method call.
834
+ *
835
+ * @param {number} yearsToSubtract - the years to subtract, may be negative
836
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the years subtracted, not null
837
+ * @throws {DateTimeException} if the result exceeds the supported range
838
+ */
839
+ minusYears(yearsToSubtract) {
840
+ return (yearsToSubtract === MathUtil.MIN_SAFE_INTEGER ? this.plusYears(MathUtil.MIN_SAFE_INTEGER).plusYears(1) : this.plusYears(-yearsToSubtract));
841
+ }
842
+
843
+ /**
844
+ * Returns a copy of this year-quarter with the specified period in quarters subtracted.
845
+ *
846
+ * This instance is immutable and unaffected by this method call.
847
+ *
848
+ * @param {number} quartersToSubtract - the quarters to subtract, may be negative
849
+ * @return {YearQuarter} a `YearQuarter` based on this year-quarter with the quarters subtracted, not null
850
+ * @throws {DateTimeException} if the result exceeds the supported range
851
+ */
852
+ minusQuarters(quartersToSubtract) {
853
+ return (quartersToSubtract === MathUtil.MIN_SAFE_INTEGER ? this.plusQuarters(MathUtil.MIN_SAFE_INTEGER).plusQuarters(1) : this.plusQuarters(-quartersToSubtract));
854
+
855
+ }
856
+
857
+ //-----------------------------------------------------------------------
858
+ /**
859
+ * Queries this year-quarter using the specified query.
860
+ *
861
+ * This queries this year-quarter using the specified query strategy object.
862
+ * The `TemporalQuery` object defines the logic to be used to
863
+ * obtain the result. Read the documentation of the query to understand
864
+ * what the result of this method will be.
865
+ *
866
+ * The result of this method is obtained by invoking the
867
+ * {@link TemporalQuery.queryFrom} method on the
868
+ * specified query passing `this` as the argument.
869
+ *
870
+ * @param {TemporalQuery} query - the query to invoke, not null
871
+ * @return {*} the query result, null may be returned (defined by the query)
872
+ * @throws {DateTimeException} if unable to query (defined by the query)
873
+ * @throws {ArithmeticException} if numeric overflow occurs (defined by the query)
874
+ */
875
+ query(query) {
876
+ if (query === TemporalQueries.chronology()) {
877
+ return IsoChronology.INSTANCE;
878
+ } else if (query === TemporalQueries.precision()) {
879
+ return IsoFields.QUARTER_YEARS;
880
+ }
881
+ return super.query(query);
882
+ }
883
+
884
+ /**
885
+ * Adjusts the specified temporal object to have this year-quarter.
886
+ *
887
+ * This returns a temporal object of the same observable type as the input
888
+ * with the year and quarter changed to be the same as this.
889
+ *
890
+ * The adjustment is equivalent to using {@link Temporal.plus}
891
+ * passing the number of quarters to adjust by.
892
+ * If the specified temporal object does not use the ISO calendar system then
893
+ * a `DateTimeException` is thrown.
894
+ *
895
+ * In most cases, it is clearer to reverse the calling pattern by using
896
+ * {@link Temporal.with}:
897
+ * ```
898
+ * // these two lines are equivalent, but the second approach is recommended
899
+ * temporal = thisYearQuarter.adjustInto(temporal);
900
+ * temporal = temporal.with(thisYearQuarter);
901
+ * ```
902
+ *
903
+ * This instance is immutable and unaffected by this method call.
904
+ *
905
+ * @param {Temporal} temporal - the target object to be adjusted, not null
906
+ * @return {Temporal} the adjusted object, not null
907
+ * @throws {DateTimeException} if unable to make the adjustment
908
+ * @throws {ArithmeticException} if numeric overflow occurs
909
+ */
910
+ adjustInto(temporal) {
911
+ /* TODO: only IsoChronology for now
912
+ if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {
913
+ throw new DateTimeException('Adjustment only supported on ISO date-time');
914
+ }*/
915
+ const newProlepticQuarter = this._prolepticQuarter();
916
+ const oldProlepticQuarter = temporal.get(ChronoField.YEAR) * 4 + (temporal.get(IsoFields.QUARTER_OF_YEAR) - 1);
917
+ return temporal.plus(newProlepticQuarter - oldProlepticQuarter, IsoFields.QUARTER_YEARS);
918
+ }
919
+
920
+ /**
921
+ * Calculates the amount of time until another year-quarter in terms of the specified unit.
922
+ *
923
+ * This calculates the amount of time between two `YearQuarter`
924
+ * objects in terms of a single `TemporalUnit`.
925
+ * The start and end points are `this` and the specified year-quarter.
926
+ * The result will be negative if the end is before the start.
927
+ * The `Temporal` passed to this method is converted to a
928
+ * `YearQuarter` using {@link YearQuarter.from}.
929
+ * For example, the period in years between two year-quarters can be calculated
930
+ * using `startYearQuarter.until(endYearQuarter, YEARS)`.
931
+ *
932
+ * The calculation returns a whole number, representing the number of
933
+ * complete units between the two year-quarters.
934
+ * For example, the period in decades between 2012-Q3 and 2032-Q2
935
+ * will only be one decade as it is one quarter short of two decades.
936
+ *
937
+ * There are two equivalent ways of using this method.
938
+ * The first is to invoke this method.
939
+ * The second is to use {@link TemporalUnit.between}:
940
+ * <pre>
941
+ * // these two lines are equivalent
942
+ * amount = start.until(end, QUARTER_YEARS);
943
+ * amount = QUARTER_YEARS.between(start, end);
944
+ * </pre>
945
+ * The choice should be made based on which makes the code more readable.
946
+ *
947
+ * The calculation is implemented in this method for {@link ChronoUnit}.
948
+ * The units `QUARTER_YEARS`, `YEARS`, `DECADES`,
949
+ * `CENTURIES`, `MILLENNIA` and `ERAS` are supported.
950
+ * Other `ChronoUnit` values will throw an exception.
951
+ *
952
+ * If the unit is not a `ChronoUnit`, then the result of this method
953
+ * is obtained by invoking `TemporalUnit.between(Temporal, Temporal)`
954
+ * passing `this` as the first argument and the converted input temporal
955
+ * as the second argument.
956
+ *
957
+ * This instance is immutable and unaffected by this method call.
958
+ *
959
+ * @param {Temporal} endExclusive - the end date, exclusive, which is converted to a `YearQuarter`, not null
960
+ * @param {TemporalUnit} unit - the unit to measure the amount in, not null
961
+ * @return {number} the amount of time between this year-quarter and the end year-quarter
962
+ * @throws {DateTimeException} if the amount cannot be calculated, or the end
963
+ * temporal cannot be converted to a `YearQuarter`
964
+ * @throws {UnsupportedTemporalTypeException} if the unit is not supported
965
+ * @throws {ArithmeticException} if numeric overflow occurs
966
+ */
967
+ until(endExclusive, unit) {
968
+ requireNonNull(endExclusive, 'endExclusive');
969
+ requireNonNull(unit, 'unit');
970
+ requireInstance(endExclusive, Temporal, 'endExclusive');
971
+ requireInstance(unit, TemporalUnit, 'unit');
972
+
973
+ const end = YearQuarter.from(endExclusive);
974
+ const quartersUntil = end._prolepticQuarter() - this._prolepticQuarter(); // no overflow
975
+ if (unit === IsoFields.QUARTER_YEARS) {
976
+ return quartersUntil;
977
+ } else if (unit instanceof ChronoUnit) {
978
+ switch (unit) {
979
+ case ChronoUnit.YEARS:
980
+ return MathUtil.intDiv(quartersUntil, 4);
981
+ case ChronoUnit.DECADES:
982
+ return MathUtil.intDiv(quartersUntil, 40);
983
+ case ChronoUnit.CENTURIES:
984
+ return MathUtil.intDiv(quartersUntil, 400);
985
+ case ChronoUnit.MILLENNIA:
986
+ return MathUtil.intDiv(quartersUntil, 4000);
987
+ case ChronoUnit.ERAS:
988
+ return end.getLong(ChronoField.ERA) - this.getLong(ChronoField.ERA);
989
+ default:
990
+ throw new UnsupportedTemporalTypeException(`Unsupported unit: ${unit}`);
991
+ }
992
+ }
993
+ return unit.between(this, end);
994
+ }
995
+
996
+ /**
997
+ * Formats this year-quarter using the specified formatter.
998
+ *
999
+ * This year-quarter will be passed to the formatter to produce a string.
1000
+ *
1001
+ * @param {DateTimeFormatter} formatter - the formatter to use, not null
1002
+ * @return {string} the formatted year-quarter string, not null
1003
+ * @throws {DateTimeException} if an error occurs during printing
1004
+ */
1005
+ format(formatter) {
1006
+ requireNonNull(formatter, 'formatter');
1007
+ return formatter.format(this);
1008
+ }
1009
+
1010
+ //-----------------------------------------------------------------------
1011
+ /**
1012
+ * Combines this year-quarter with a day-of-quarter to create a `LocalDate`.
1013
+ *
1014
+ * This returns a `LocalDate` formed from this year-quarter and the specified day-of-quarter.
1015
+ *
1016
+ * The day-of-quarter value must be valid for the year-quarter.
1017
+ *
1018
+ * This method can be used as part of a chain to produce a date:
1019
+ * ```
1020
+ * LocalDate date = yearQuarter.atDay(day);
1021
+ * ```
1022
+ *
1023
+ * @param {number} dayOfQuarter the day-of-quarter to use, from 1 to 92
1024
+ * @return {LocalDate} the date formed from this year-quarter and the specified day, not null
1025
+ * @throws {DateTimeException} if the day is invalid for the year-quarter
1026
+ * @see {@link YearQuarter.isValidDay}
1027
+ */
1028
+ atDay(dayOfQuarter) {
1029
+ ValueRange.of(1, this.lengthOfQuarter()).checkValidValue(dayOfQuarter, IsoFields.DAY_OF_QUARTER);
1030
+ const leap = Year.isLeap(this._year);
1031
+ let month = this._quarter.firstMonth();
1032
+ while (dayOfQuarter > month.length(leap)) {
1033
+ dayOfQuarter -= month.length(leap);
1034
+ month = month.plus(1);
1035
+ }
1036
+ return LocalDate.of(this._year, month, dayOfQuarter);
1037
+ }
1038
+
1039
+ /**
1040
+ * Returns a `LocalDate` at the end of the quarter.
1041
+ *
1042
+ * This returns a `LocalDate` based on this year-quarter.
1043
+ * The day-of-quarter is set to the last valid day of the quarter, taking
1044
+ * into account leap years.
1045
+ *
1046
+ * This method can be used as part of a chain to produce a date:
1047
+ * ```
1048
+ * LocalDate date = year.atQuarter(quarter).atEndOfQuarter();
1049
+ * ```
1050
+ *
1051
+ * @return {LocalDate} the last valid date of this year-quarter, not null
1052
+ */
1053
+ atEndOfQuarter() {
1054
+ const month = this._quarter.firstMonth().plus(2);
1055
+ return LocalDate.of(this._year, month, month.maxLength());
1056
+ }
1057
+
1058
+ //-----------------------------------------------------------------------
1059
+ /**
1060
+ * Compares this year-quarter to another
1061
+ *
1062
+ * The comparison is based first on the value of the year, then on the value of the quarter.
1063
+ * It is 'consistent with equals', as defined by {@link Comparable}.
1064
+ *
1065
+ * @param {YearQuarter} other - the other year-quarter to compare to, not null
1066
+ * @return {number} the comparator value, negative if less, positive if greater
1067
+ */
1068
+ compareTo(other) {
1069
+ requireNonNull(other, 'other');
1070
+ requireInstance(other, YearQuarter, 'other');
1071
+ let cmp = (this._year - other._year);
1072
+ if (cmp === 0) {
1073
+ cmp = this._quarter.compareTo(other._quarter);
1074
+ }
1075
+ return cmp;
1076
+ }
1077
+
1078
+ /**
1079
+ * Is this year-quarter after the specified year-quarter.
1080
+ *
1081
+ * @param {YearQuarter} other - the other year-quarter to compare to, not null
1082
+ * @return {boolean} true if this is after the specified year-quarter
1083
+ */
1084
+ isAfter(other) {
1085
+ return this.compareTo(other) > 0;
1086
+ }
1087
+
1088
+ /**
1089
+ * Is this year-quarter before the specified year-quarter.
1090
+ *
1091
+ * @param {YearQuarter} other - the other year-quarter to compare to, not null
1092
+ * @return {boolean} true if this point is before the specified year-quarter
1093
+ */
1094
+ isBefore(other) {
1095
+ return this.compareTo(other) < 0;
1096
+ }
1097
+
1098
+ //-----------------------------------------------------------------------
1099
+ /**
1100
+ * Checks if this year-quarter is equal to another year-quarter.
1101
+ *
1102
+ * The comparison is based on the time-line position of the year-quarters.
1103
+ *
1104
+ * @param {*} obj - the object to check, null returns false
1105
+ * @return {boolean} true if this is equal to the other year-quarter
1106
+ */
1107
+ equals(obj) {
1108
+ if (this === obj) {
1109
+ return true;
1110
+ }
1111
+ if (obj instanceof YearQuarter) {
1112
+ const other = obj;
1113
+ return this._year === other._year && this._quarter === other._quarter;
1114
+ }
1115
+ return false;
1116
+ }
1117
+
1118
+ /**
1119
+ * A hash code for this year-quarter.
1120
+ *
1121
+ * @return {number} a suitable hash code
1122
+ */
1123
+ hashCode() {
1124
+ return this._year ^ (this._quarter.value() << 27);
1125
+ }
1126
+
1127
+ //-----------------------------------------------------------------------
1128
+ /**
1129
+ * Outputs this year-quarter as a `String`, such as `2007-Q2`.
1130
+ *
1131
+ * The output will be in the format `uuuu-'Q'Q`:
1132
+ *
1133
+ * @return {string} a string representation of this year-quarter, not null
1134
+ */
1135
+ toString() {
1136
+ let yearString;
1137
+ const yearValue = this._year;
1138
+ const absYear = Math.abs(yearValue);
1139
+ if (absYear < 1000) {
1140
+ if (yearValue < 0) {
1141
+ yearString = `-${(`${yearValue - 10000}`).slice(-4)}`;
1142
+ } else {
1143
+ yearString = (`${yearValue + 10000}`).slice(-4);
1144
+ }
1145
+ } else {
1146
+ if (yearValue > 9999) {
1147
+ yearString = `+${yearValue}`;
1148
+ } else {
1149
+ yearString = `${yearValue}`;
1150
+ }
1151
+ }
1152
+ return yearString.concat('-').concat(this._quarter);
1153
+ }
1154
+ }
1155
+
1156
+ export function _init() {
1157
+ YearQuarter.PARSER = new DateTimeFormatterBuilder()
1158
+ .parseCaseInsensitive()
1159
+ .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
1160
+ .appendLiteral('-')
1161
+ .appendLiteral('Q')
1162
+ .appendValue(IsoFields.QUARTER_OF_YEAR, 1)
1163
+ .toFormatter();
1164
+
1165
+ YearQuarter.FROM = createTemporalQuery('YearQuarter.FROM', (temporal) => {
1166
+ return YearQuarter.from(temporal);
1167
+ });
1168
+ }
1169
+
1170
+ // copied from packages/core/src/temporal/TemporalQuery.js
1171
+ function createTemporalQuery(name, queryFromFunction) {
1172
+ class ExtendedTemporalQuery extends TemporalQuery {
1173
+ }
1174
+
1175
+ ExtendedTemporalQuery.prototype.queryFrom = queryFromFunction;
1176
+ return new ExtendedTemporalQuery(name);
1177
+ }