@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,1279 @@
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, IllegalArgumentException, IsoChronology, LocalDate, OffsetDateTime, Temporal, TemporalField, TemporalQueries, TemporalQuery, TemporalUnit, ZoneId, ZoneOffset } 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 { requireInstance, requireNonNull } from './assert';
11
+ import { _ as jodaInternal } from '@js-joda/core';
12
+
13
+ const MathUtil = jodaInternal.MathUtil;
14
+
15
+ /**
16
+ * The number of seconds per day.
17
+ */
18
+ const SECONDS_PER_DAY = 86400;
19
+
20
+ /**
21
+ * A date with an offset from UTC/Greenwich in the ISO-8601 calendar system,
22
+ * such as `2007-12-03+01:00`.
23
+ *
24
+ * `OffsetDate` is an immutable date-time object that represents a date, often viewed
25
+ * as year-month-day-offset. This object can also access other date fields such as
26
+ * day-of-year, day-of-week and week-of-year.
27
+ *
28
+ * This class does not store or represent a time.
29
+ * For example, the value '2nd October 2007 +02:00' can be stored
30
+ * in an `OffsetDate`.
31
+ *
32
+ */
33
+ export class OffsetDate extends Temporal {
34
+ //-----------------------------------------------------------------------
35
+ /**
36
+ * function overloading for {@link OffsetDate.now}
37
+ * - if called with 0 argument {@link OffsetDate._now0} is executed,
38
+ * - if called with 1 argument and first argument is an instance of ZoneId, then {@link OffsetDate._nowZoneId} is executed,
39
+ * - otherwise {@link OffsetDate._nowClock} is executed
40
+ *
41
+ * @param {?(ZoneId|Clock)} zoneIdOrClock
42
+ * @return {YearQuarter}
43
+ */
44
+ static now(zoneIdOrClock) {
45
+ if (arguments.length === 0) {
46
+ return OffsetDate._now0();
47
+ } else if (arguments.length === 1 && zoneIdOrClock instanceof ZoneId) {
48
+ return OffsetDate._nowZoneId(zoneIdOrClock);
49
+ } else {
50
+ return OffsetDate._nowClock(zoneIdOrClock);
51
+ }
52
+ }
53
+
54
+ //-----------------------------------------------------------------------
55
+ /**
56
+ * Obtains the current date from the system clock in the default time-zone.
57
+ *
58
+ * This will query the {@link Clock.systemDefaultZone} system clock in the default
59
+ * time-zone to obtain the current date.
60
+ * The offset will be calculated from the time-zone in the clock.
61
+ *
62
+ * Using this method will prevent the ability to use an alternate clock for testing
63
+ * because the clock is hard-coded.
64
+ *
65
+ * @return {OffsetDate} the current date using the system clock, not null
66
+ * @protected
67
+ */
68
+ static _now0() {
69
+ return OffsetDate.now(Clock.systemDefaultZone());
70
+ }
71
+
72
+ /**
73
+ * Obtains the current date from the system clock in the specified time-zone.
74
+ *
75
+ * This will query the {@link Clock.system} system clock to obtain the current date.
76
+ * Specifying the time-zone avoids dependence on the default time-zone.
77
+ * The offset will be calculated from the specified time-zone.
78
+ *
79
+ * Using this method will prevent the ability to use an alternate clock for testing
80
+ * because the clock is hard-coded.
81
+ *
82
+ * @param {ZoneId} zone - the zone ID to use, not null
83
+ * @return {OffsetDate} the current date using the system clock, not null
84
+ * @protected
85
+ */
86
+ static _nowZoneId(zone) {
87
+ return OffsetDate.now(Clock.system(zone));
88
+ }
89
+
90
+ /**
91
+ * Obtains the current date from the specified clock.
92
+ *
93
+ * This will query the specified clock to obtain the current date - today.
94
+ * The offset will be calculated from the time-zone in the clock.
95
+ *
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 {OffsetDate} the current date, not null
101
+ * @protected
102
+ */
103
+ static _nowClock(clock) {
104
+ requireNonNull(clock, 'clock');
105
+ const now = clock.instant(); // called once
106
+ return OffsetDate.ofInstant(now, clock.zone().rules().offset(now));
107
+ }
108
+
109
+ //-----------------------------------------------------------------------
110
+ /**
111
+ * function overloading for {@link OffsetDate.of}
112
+ * - if called with 2 arguments {@link OffsetDate._ofLocalDateZoneOffset} is executed,
113
+ * - if called with 4 agruments {@link OffsetDate._ofIntIntIntZoneOffset} is executed,
114
+ * - otherwise throws IllegalArgumentException.
115
+ *
116
+ * @return {YearQuarter}
117
+ */
118
+ static of() {
119
+ switch (arguments.length) {
120
+ case 2: return OffsetDate._ofLocalDateZoneOffset(...arguments);
121
+ case 4: return OffsetDate._ofIntIntIntZoneOffset(...arguments);
122
+ default: throw new IllegalArgumentException('Illegal number of arguments');
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Obtains an instance of `OffsetDate` from a local date and an offset.
128
+ *
129
+ * @param {LocalDate} date - the local date, not null
130
+ * @param {ZoneOffset} offset - the zone offset, not null
131
+ * @return {OffsetDate} the offset date, not null
132
+ * @protected
133
+ */
134
+ static _ofLocalDateZoneOffset(date, offset) {
135
+ return new OffsetDate(date, offset);
136
+ }
137
+
138
+ /**
139
+ * Obtains an instance of `OffsetDate` from a year, month, day
140
+ * and offset.
141
+ *
142
+ * This creates an offset date with the four specified fields.
143
+ *
144
+ * This method exists primarily for writing test cases.
145
+ * Non test-code will typically use other methods to create an offset time.
146
+ *
147
+ * @param {number} year - the year to represent, from MIN_YEAR to MAX_YEAR
148
+ * @param {number} month - the month-of-year to represent, from 1 (January) to 12 (December)
149
+ * @param {number} dayOfMonth - the day-of-month to represent, from 1 to 31
150
+ * @param {ZoneOffset} offset - the zone offset, not null
151
+ * @return {OffsetDate} the offset date, not null
152
+ * @throws {DateTimeException} if the value of any field is out of range, or
153
+ * if the day-of-month is invalid for the month-year
154
+ * @protected
155
+ */
156
+ static _ofIntIntIntZoneOffset(year, month, dayOfMonth, offset) {
157
+ const d = LocalDate.of(year, month, dayOfMonth);
158
+ return new OffsetDate(d, offset);
159
+ }
160
+
161
+ //-----------------------------------------------------------------------
162
+ /**
163
+ * Obtains an instance of `OffsetDate` from an `Instant` and zone ID.
164
+ *
165
+ * This creates an offset date with the same instant as midnight at the
166
+ * start of day of the instant specified.
167
+ * Finding the offset from UTC/Greenwich is simple as there is only one valid
168
+ * offset for each instant.
169
+ *
170
+ * @param {Instant} instant - the instant to create the time from, not null
171
+ * @param {ZoneId} zone - the time-zone, which may be an offset, not null
172
+ * @return {OffsetDate} the offset time, not null
173
+ */
174
+ static ofInstant(instant, zone) {
175
+ requireNonNull(instant, 'instant');
176
+ requireNonNull(zone, 'zone');
177
+ const rules = zone.rules();
178
+ const offset = rules.offset(instant);
179
+ const epochSec = instant.epochSecond() + offset.totalSeconds(); // overflow caught later
180
+ const epochDay = MathUtil.floorDiv(epochSec, SECONDS_PER_DAY);
181
+ const date = LocalDate.ofEpochDay(epochDay);
182
+ return new OffsetDate(date, offset);
183
+ }
184
+
185
+ //-----------------------------------------------------------------------
186
+ /**
187
+ * Obtains an instance of `OffsetDate` from a temporal object.
188
+ *
189
+ * A `TemporalAccessor` represents some form of date and time information.
190
+ * This factory converts the arbitrary temporal object to an instance of `OffsetDate`.
191
+ *
192
+ * The conversion extracts and combines `LocalDate` and `ZoneOffset`.
193
+ *
194
+ * This method matches the signature of the functional interface {@link TemporalQuery}
195
+ * allowing it to be used in queries via method reference, `OffsetDate.FROM`.
196
+ *
197
+ * @param {TemporalAccessor} temporal - the temporal object to convert, not null
198
+ * @return {OffsetDate} the offset date, not null
199
+ * @throws {DateTimeException} if unable to convert to an `OffsetDate`
200
+ */
201
+ static from(temporal) {
202
+ if (temporal instanceof OffsetDate) {
203
+ return temporal;
204
+ }
205
+ requireNonNull(temporal, 'temporal');
206
+ try {
207
+ const date = LocalDate.from(temporal);
208
+ const offset = ZoneOffset.from(temporal);
209
+ return new OffsetDate(date, offset);
210
+ } catch (ex) {
211
+ throw new DateTimeException(`Unable to obtain OffsetDate from TemporalAccessor: ${temporal.constructor.name}`, ex);
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Obtains an instance of `OffsetDate` from a text string using a specific formatter.
217
+ *
218
+ * The text is parsed using the formatter, returning a date.
219
+ *
220
+ * @param {CharSequence} text - the text to parse, not null
221
+ * @param {DateTimeFormatter} formatter - the formatter to use, not null
222
+ * @return {OffsetDate} the parsed offset date, not null
223
+ * @throws {DateTimeParseException} if the text cannot be parsed
224
+ */
225
+ static parse(text, formatter = DateTimeFormatter.ISO_OFFSET_DATE) {
226
+ requireNonNull(formatter, 'formatter');
227
+ return formatter.parse(text, OffsetDate.FROM);
228
+ }
229
+
230
+ //-----------------------------------------------------------------------
231
+ /**
232
+ * Constructor.
233
+ *
234
+ * @param {LocalDate} date - the local date, not null
235
+ * @param {ZoneOffset} offset - the zone offset, not null
236
+ * @private
237
+ */
238
+ constructor(date, offset) {
239
+ super();
240
+ this._date = requireNonNull(date, 'date');
241
+ this._offset = requireNonNull(offset, 'offset');
242
+ }
243
+
244
+ /**
245
+ * Returns a new date based on this one, returning `this` where possible.
246
+ *
247
+ * @param {LocalDate} date - the date to create with, not null
248
+ * @param {ZoneOffset} offset - the zone offset to create with, not null
249
+ * @private
250
+ */
251
+ _with(date, offset) {
252
+ if (this._date === date && this._offset.equals(offset)) {
253
+ return this;
254
+ }
255
+ return new OffsetDate(date, offset);
256
+ }
257
+
258
+ /**
259
+ * function overloading for {@link OffsetDate.isSupported}
260
+ * - if called with an instance of {@link TemporalField}, then {@link OffsetDate._isSupportedField} is executed,
261
+ * - if called with an instance of {@link TemporalUnit}, then {@link OffsetDate._isSupportedUnit} is executed,
262
+ * - otherwise {@link IllegalArgumentException} is thrown.
263
+ *
264
+ * @param {TemporalField|TemporalUnit} fieldOrUnit
265
+ * @return {boolean}
266
+ */
267
+ isSupported(fieldOrUnit) {
268
+ if (fieldOrUnit instanceof TemporalField) {
269
+ return this._isSupportedField(fieldOrUnit);
270
+ }
271
+ if (fieldOrUnit instanceof TemporalUnit) {
272
+ return this._isSupportedUnit(fieldOrUnit);
273
+ }
274
+ if (fieldOrUnit == null) {
275
+ return false;
276
+ }
277
+ throw new IllegalArgumentException(`fieldOrUnit must be an instance of TemporalField or TemporalUnit, but is ${fieldOrUnit.constructor.name}`);
278
+ }
279
+
280
+ //-----------------------------------------------------------------------
281
+ /**
282
+ * Checks if the specified field is supported.
283
+ *
284
+ * This checks if this date can be queried for the specified field.
285
+ * If false, then calling the {@link OffsetDate.range},
286
+ * {@link OffsetDate.get} and {@link OffsetDate.with}
287
+ * methods will throw an exception.
288
+ *
289
+ * If the field is a {@link ChronoField} then the query is implemented here.
290
+ * The supported fields are:
291
+ *
292
+ * - `DAY_OF_WEEK`
293
+ * - `ALIGNED_DAY_OF_WEEK_IN_MONTH`
294
+ * - `ALIGNED_DAY_OF_WEEK_IN_YEAR`
295
+ * - `DAY_OF_MONTH`
296
+ * - `DAY_OF_YEAR`
297
+ * - `EPOCH_DAY`
298
+ * - `ALIGNED_WEEK_OF_MONTH`
299
+ * - `ALIGNED_WEEK_OF_YEAR`
300
+ * - `MONTH_OF_YEAR`
301
+ * - `PROLEPTIC_MONTH`
302
+ * - `YEAR_OF_ERA`
303
+ * - `YEAR`
304
+ * - `ERA`
305
+ * - `OFFSET_SECONDS`
306
+ *
307
+ * All other `ChronoField` instances will return false.
308
+ *
309
+ * If the field is not a `ChronoField`, then the result of this method
310
+ * is obtained by invoking `TemporalField.isSupportedBy(TemporalAccessor)`
311
+ * passing `this` as the argument.
312
+ * Whether the field is supported is determined by the field.
313
+ *
314
+ * @param {TemporalField} field - the field to check, null returns false
315
+ * @return {boolean} true if the field is supported on this date, false if not
316
+ * @protected
317
+ */
318
+ _isSupportedField(field) {
319
+ if (field instanceof ChronoField) {
320
+ return field.isDateBased() || field === ChronoField.OFFSET_SECONDS;
321
+ }
322
+ return field != null && field.isSupportedBy(this);
323
+ }
324
+
325
+ /**
326
+ * Checks if the specified unit is supported.
327
+ *
328
+ * This checks if the specified unit can be added to, or subtracted from, this date.
329
+ * If false, then calling the {@link OffsetDate.plus(long, TemporalUnit)} and
330
+ * {@link OffsetDate.minus(long, TemporalUnit) minus} methods will throw an exception.
331
+ *
332
+ * If the unit is a {@link ChronoUnit} then the query is implemented here.
333
+ * The supported units are:
334
+ * <ul>
335
+ * - `DAYS`
336
+ * - `WEEKS`
337
+ * - `MONTHS`
338
+ * - `YEARS`
339
+ * - `DECADES`
340
+ * - `CENTURIES`
341
+ * - `MILLENNIA`
342
+ * - `ERAS`
343
+ * </ul>
344
+ * All other `ChronoUnit` instances will return false.
345
+ *
346
+ * If the unit is not a `ChronoUnit`, then the result of this method
347
+ * is obtained by invoking `TemporalUnit.isSupportedBy(Temporal)`
348
+ * passing `this` as the argument.
349
+ * Whether the unit is supported is determined by the unit.
350
+ *
351
+ * @param {TemporalUnit} unit - the unit to check, null returns false
352
+ * @return {boolean} true if the unit can be added/subtracted, false if not
353
+ * @protected
354
+ */
355
+ _isSupportedUnit(unit) {
356
+ if (unit instanceof ChronoUnit) {
357
+ return unit.isDateBased();
358
+ }
359
+ return unit != null && unit.isSupportedBy(this);
360
+ }
361
+
362
+ //-----------------------------------------------------------------------
363
+ /**
364
+ * Gets the range of valid values for the specified field.
365
+ *
366
+ * The range object expresses the minimum and maximum valid values for a field.
367
+ * This date is used to enhance the accuracy of the returned range.
368
+ * If it is not possible to return the range, because the field is not supported
369
+ * or for some other reason, an exception is thrown.
370
+ *
371
+ * If the field is a {@link ChronoField} then the query is implemented here.
372
+ * The {@link OffsetDate.isSupported} supported fields will return
373
+ * appropriate range instances.
374
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
375
+ *
376
+ * If the field is not a `ChronoField`, then the result of this method
377
+ * is obtained by invoking `TemporalField.rangeRefinedBy(TemporalAccessor)`
378
+ * passing `this` as the argument.
379
+ * Whether the range can be obtained is determined by the field.
380
+ *
381
+ * @param {TemporalField} field - the field to query the range for, not null
382
+ * @return {ValueRange} the range of valid values for the field, not null
383
+ * @throws {DateTimeException} if the range for the field cannot be obtained
384
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
385
+ */
386
+ range(field) {
387
+ requireNonNull(field, 'field');
388
+ requireInstance(field, TemporalField, 'field');
389
+ if (field instanceof ChronoField) {
390
+ if (field === ChronoField.OFFSET_SECONDS) {
391
+ return field.range();
392
+ }
393
+ return this._date.range(field);
394
+ }
395
+ return field.rangeRefinedBy(this);
396
+ }
397
+
398
+ /**
399
+ * Gets the value of the specified field from this date as an `int`.
400
+ *
401
+ * This queries this date for the value for the specified field.
402
+ * The returned value will always be within the valid range of values for the field.
403
+ * If it is not possible to return the value, because the field is not supported
404
+ * or for some other reason, an exception is thrown.
405
+ *
406
+ * If the field is a {@link ChronoField} then the query is implemented here.
407
+ * The {@link OffsetDate.isSupported} supported fields will return valid
408
+ * values based on this date, except `EPOCH_DAY` and `PROLEPTIC_MONTH`
409
+ * which are too large to fit in an `int` and throw a `DateTimeException`.
410
+ * All other `ChronoField` instances will throw a `DateTimeException`.
411
+ *
412
+ * If the field is not a `ChronoField`, then the result of this method
413
+ * is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
414
+ * passing `this` as the argument. Whether the value can be obtained,
415
+ * and what the value represents, is determined by the field.
416
+ *
417
+ * @param {TemporalField} field - the field to get, not null
418
+ * @return {number} the value for the field
419
+ * @throws {DateTimeException} if a value for the field cannot be obtained or
420
+ * the value is outside the range of valid values for the field
421
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported or
422
+ * the range of values exceeds an `int`
423
+ * @throws {ArithmeticException} if numeric overflow occurs
424
+ */
425
+ get(field) {
426
+ requireNonNull(field, 'field');
427
+ requireInstance(field, TemporalField, 'field');
428
+ return this.range(field).checkValidIntValue(this.getLong(field), field);
429
+ }
430
+
431
+ /**
432
+ * Gets the value of the specified field from this date as a `long`.
433
+ *
434
+ * This queries this date for the value for the specified field.
435
+ * If it is not possible to return the value, because the field is not supported
436
+ * or for some other reason, an exception is thrown.
437
+ *
438
+ * If the field is a {@link ChronoField} then the query is implemented here.
439
+ * The {@link OffsetDate.isSupported} supported fields will return valid
440
+ * values based on this date.
441
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
442
+ *
443
+ * If the field is not a `ChronoField`, then the result of this method
444
+ * is obtained by invoking `TemporalField.getFrom(TemporalAccessor)`
445
+ * passing `this` as the argument. Whether the value can be obtained,
446
+ * and what the value represents, is determined by the field.
447
+ *
448
+ * @param {TemporalField} field - the field to get, not null
449
+ * @return {number} the value for the field
450
+ * @throws {DateTimeException} if a value for the field cannot be obtained
451
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
452
+ * @throws {ArithmeticException} if numeric overflow occurs
453
+ */
454
+ getLong(field) {
455
+ requireNonNull(field, 'field');
456
+ requireInstance(field, TemporalField, 'field');
457
+ if (field instanceof ChronoField) {
458
+ if (field === ChronoField.OFFSET_SECONDS) {
459
+ return this.offset().totalSeconds();
460
+ }
461
+ return this._date.getLong(field);
462
+ }
463
+ return field.getFrom(this);
464
+ }
465
+
466
+ //-----------------------------------------------------------------------
467
+ /**
468
+ * Gets the zone offset, such as '+01:00'.
469
+ *
470
+ * This is the offset of the local date from UTC/Greenwich.
471
+ *
472
+ * @return {ZoneOffset} the zone offset, not null
473
+ */
474
+ offset() {
475
+ return this._offset;
476
+ }
477
+
478
+ /**
479
+ * Returns a copy of this `OffsetDate` with the specified offset ensuring
480
+ * that the result has the same local date.
481
+ *
482
+ * This method returns an object with the same `LocalDate` and the specified `ZoneOffset`.
483
+ * No calculation is needed or performed.
484
+ * For example, if this time represents `2007-12-03+02:00` and the offset specified is
485
+ * `+03:00`, then this method will return `2007-12-03+03:00`.
486
+ *
487
+ * This instance is immutable and unaffected by this method call.
488
+ *
489
+ * @param {ZoneOffset} offset - the zone offset to change to, not null
490
+ * @return {OffsetDate} an `OffsetDate` based on this date with the requested offset, not null
491
+ */
492
+ withOffsetSameLocal(offset) {
493
+ requireNonNull(offset, 'offset');
494
+ return this._with(this._date, offset);
495
+ }
496
+
497
+ //-----------------------------------------------------------------------
498
+ /**
499
+ * Gets the `LocalDate` part of this date.
500
+ *
501
+ * This returns a `LocalDate` with the same year, month and day
502
+ * as this date.
503
+ *
504
+ * @return {LocalDate} the date part of this date, not null
505
+ */
506
+ toLocalDate() {
507
+ return this._date;
508
+ }
509
+
510
+ //-----------------------------------------------------------------------
511
+ /**
512
+ * Gets the year field.
513
+ *
514
+ * This method returns the primitive `int` value for the year.
515
+ *
516
+ * The year returned by this method is proleptic as per `get(YEAR)`.
517
+ * To obtain the year-of-era, use `get(YEAR_OF_ERA)`.
518
+ *
519
+ * @return {number} the year, from MIN_YEAR to MAX_YEAR
520
+ */
521
+ year() {
522
+ return this._date.year();
523
+ }
524
+
525
+ /**
526
+ * Gets the month-of-year field from 1 to 12.
527
+ *
528
+ * This method returns the month as an `int` from 1 to 12.
529
+ * Application code is frequently clearer if the enum {@link Month}
530
+ * is used by calling {@link OffsetDate.month}.
531
+ *
532
+ * @return {number} the month-of-year, from 1 to 12
533
+ * @see OffsetDate.month()
534
+ */
535
+ monthValue() {
536
+ return this._date.monthValue();
537
+ }
538
+
539
+ /**
540
+ * Gets the month-of-year field using the `Month` enum.
541
+ *
542
+ * This method returns the enum {@link Month} for the month.
543
+ * This avoids confusion as to what `int` values mean.
544
+ * If you need access to the primitive `int` value then the enum
545
+ * provides the {@link Month.value} int value.
546
+ *
547
+ * @return {Month} the month-of-year, not null
548
+ * @see OffsetDate.monthValue()
549
+ */
550
+ month() {
551
+ return this._date.month();
552
+ }
553
+
554
+ /**
555
+ * Gets the day-of-month field.
556
+ *
557
+ * This method returns the primitive `int` value for the day-of-month.
558
+ *
559
+ * @return {number} the day-of-month, from 1 to 31
560
+ */
561
+ dayOfMonth() {
562
+ return this._date.dayOfMonth();
563
+ }
564
+
565
+ /**
566
+ * Gets the day-of-year field.
567
+ *
568
+ * This method returns the primitive `int` value for the day-of-year.
569
+ *
570
+ * @return {number} the day-of-year, from 1 to 365, or 366 in a leap year
571
+ */
572
+ dayOfYear() {
573
+ return this._date.dayOfYear();
574
+ }
575
+
576
+ /**
577
+ * Gets the day-of-week field, which is an enum `DayOfWeek`.
578
+ *
579
+ * This method returns the enum {@link DayOfWeek} for the day-of-week.
580
+ * This avoids confusion as to what `int` values mean.
581
+ * If you need access to the primitive `int` value then the enum
582
+ * provides the {@link DayOfWeek.value} int value.
583
+ *
584
+ * Additional information can be obtained from the `DayOfWeek`.
585
+ * This includes textual names of the values.
586
+ *
587
+ * @return {DayOfWeek} the day-of-week, not null
588
+ */
589
+ dayOfWeek() {
590
+ return this._date.dayOfWeek();
591
+ }
592
+
593
+ //-----------------------------------------------------------------------
594
+ /**
595
+ * Returns an adjusted copy of this date.
596
+ *
597
+ * This returns an `OffsetDate` based on this one, with the date adjusted.
598
+ * The adjustment takes place using the specified adjuster strategy object.
599
+ * Read the documentation of the adjuster to understand what adjustment will be made.
600
+ *
601
+ * A simple adjuster might simply set the one of the fields, such as the year field.
602
+ * A more complex adjuster might set the date to the last day of the month.
603
+ * A selection of common adjustments is provided in {@link TemporalAdjusters}.
604
+ * These include finding the 'last day of the month' and 'next Wednesday'.
605
+ * Key date-time classes also implement the `TemporalAdjuster` interface,
606
+ * such as {@link Month} and {@link MonthDay}.
607
+ * The adjuster is responsible for handling special cases, such as the varying
608
+ * lengths of month and leap years.
609
+ *
610
+ * For example this code returns a date on the last day of July:
611
+ * <pre>
612
+ * import static java.time.Month.*;
613
+ * import static java.time.temporal.TemporalAdjusters.*;
614
+ *
615
+ * result = offsetDate.with(JULY).with(lastDayOfMonth());
616
+ * </pre>
617
+ *
618
+ * The classes {@link LocalDate} and {@link ZoneOffset} implement `TemporalAdjuster`,
619
+ * thus this method can be used to change the date or offset:
620
+ * <pre>
621
+ * result = offsetDate.with(date);
622
+ * result = offsetDate.with(offset);
623
+ * </pre>
624
+ *
625
+ * The result of this method is obtained by invoking the
626
+ * {@link TemporalAdjuster.adjustInto} method on the
627
+ * specified adjuster passing `this` as the argument.
628
+ *
629
+ * This instance is immutable and unaffected by this method call.
630
+ *
631
+ * @param {TemporalAdjuster} adjuster - the adjuster to use, not null
632
+ * @return {OffsetDate} an `OffsetDate` based on `this` with the adjustment made, not null
633
+ * @throws {DateTimeException} if the adjustment cannot be made
634
+ * @throws {ArithmeticException} if numeric overflow occurs
635
+ */
636
+ _withAdjuster(adjuster) {
637
+ // optimizations
638
+ if (adjuster instanceof LocalDate) {
639
+ return this._with(adjuster, this._offset);
640
+ } else if (adjuster instanceof ZoneOffset) {
641
+ return this._with(this._date, adjuster);
642
+ } else if (adjuster instanceof OffsetDate) {
643
+ return adjuster;
644
+ }
645
+ return super._withAdjuster(adjuster);
646
+ }
647
+
648
+ /**
649
+ * Returns a copy of this date with the specified field set to a new value.
650
+ *
651
+ * This returns an `OffsetDate` based on this one, with the value
652
+ * for the specified field changed.
653
+ * This can be used to change any supported field, such as the year, month or day-of-month.
654
+ * If it is not possible to set the value, because the field is not supported or for
655
+ * some other reason, an exception is thrown.
656
+ *
657
+ * In some cases, changing the specified field can cause the resulting date to become invalid,
658
+ * such as changing the month from 31st January to February would make the day-of-month invalid.
659
+ * In cases like this, the field is responsible for resolving the date. Typically it will choose
660
+ * the previous valid date, which would be the last valid day of February in this example.
661
+ *
662
+ * If the field is a {@link ChronoField} then the adjustment is implemented here.
663
+ *
664
+ * The `OFFSET_SECONDS` field will return a date with the specified offset.
665
+ * The local date is unaltered. If the new offset value is outside the valid range
666
+ * then a `DateTimeException` will be thrown.
667
+ *
668
+ * The other {@link OffsetDate.isSupported} supported fields will behave as per
669
+ * the matching method on {@link LocalDate.with} LocalDate.
670
+ * In this case, the offset is not part of the calculation and will be unchanged.
671
+ *
672
+ * All other `ChronoField` instances will throw an `UnsupportedTemporalTypeException`.
673
+ *
674
+ * If the field is not a `ChronoField`, then the result of this method
675
+ * is obtained by invoking `TemporalField.adjustInto(Temporal, long)`
676
+ * passing `this` as the argument. In this case, the field determines
677
+ * whether and how to adjust the instant.
678
+ *
679
+ * This instance is immutable and unaffected by this method call.
680
+ *
681
+ * @param {TemporalField} field - the field to set in the result, not null
682
+ * @param {number} newValue - the new value of the field in the result
683
+ * @return {OffsetDate} an `OffsetDate` based on `this` with the specified field set, not null
684
+ * @throws {DateTimeException} if the field cannot be set
685
+ * @throws {UnsupportedTemporalTypeException} if the field is not supported
686
+ * @throws {ArithmeticException} if numeric overflow occurs
687
+ */
688
+ _withField(field, newValue) {
689
+ requireNonNull(field, 'field');
690
+ requireInstance(field, TemporalField, 'field');
691
+ if (field instanceof ChronoField) {
692
+ if (field === ChronoField.OFFSET_SECONDS) {
693
+ const f = field;
694
+ return this._with(this._date, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
695
+ }
696
+ return this._with(this._date.with(field, newValue), this._offset);
697
+ }
698
+ return field.adjustInto(this, newValue);
699
+ }
700
+
701
+ //-----------------------------------------------------------------------
702
+ /**
703
+ * Returns a copy of this `OffsetDate` with the year altered.
704
+ *
705
+ * The offset does not affect the calculation and will be the same in the result.
706
+ * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
707
+ *
708
+ * This instance is immutable and unaffected by this method call.
709
+ *
710
+ * @param {OffsetDate} year - the year to set in the result, from MIN_YEAR to MAX_YEAR
711
+ * @return {number} an `OffsetDate` based on this date with the requested year, not null
712
+ * @throws {DateTimeException} if the year value is invalid
713
+ */
714
+ withYear(year) {
715
+ return this._with(this._date.withYear(year), this._offset);
716
+ }
717
+
718
+ /**
719
+ * Returns a copy of this `OffsetDate` with the month-of-year altered.
720
+ *
721
+ * The offset does not affect the calculation and will be the same in the result.
722
+ * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
723
+ *
724
+ * This instance is immutable and unaffected by this method call.
725
+ *
726
+ * @param {number} month - the month-of-year to set in the result, from 1 (January) to 12 (December)
727
+ * @return {OffsetDate} an `OffsetDate` based on this date with the requested month, not null
728
+ * @throws {DateTimeException} if the month-of-year value is invalid
729
+ */
730
+ withMonth(month) {
731
+ return this._with(this._date.withMonth(month), this._offset);
732
+ }
733
+
734
+ /**
735
+ * Returns a copy of this `OffsetDate` with the day-of-month altered.
736
+ *
737
+ * If the resulting date is invalid, an exception is thrown.
738
+ * The offset does not affect the calculation and will be the same in the result.
739
+ *
740
+ * This instance is immutable and unaffected by this method call.
741
+ *
742
+ * @param {number} dayOfMonth - the day-of-month to set in the result, from 1 to 28-31
743
+ * @return {OffsetDate} an `OffsetDate` based on this date with the requested day, not null
744
+ * @throws {DateTimeException} if the day-of-month value is invalid,
745
+ * or if the day-of-month is invalid for the month-year
746
+ */
747
+ withDayOfMonth(dayOfMonth) {
748
+ return this._with(this._date.withDayOfMonth(dayOfMonth), this._offset);
749
+ }
750
+
751
+ /**
752
+ * Returns a copy of this `OffsetDate` with the day-of-year altered.
753
+ *
754
+ * If the resulting date is invalid, an exception is thrown.
755
+ *
756
+ * This instance is immutable and unaffected by this method call.
757
+ *
758
+ * @param {number} dayOfYear - the day-of-year to set in the result, from 1 to 365-366
759
+ * @return {OffsetDate} an `OffsetDate` based on this date with the requested day, not null
760
+ * @throws {DateTimeException} if the day-of-year value is invalid,
761
+ * or if the day-of-year is invalid for the year
762
+ */
763
+ withDayOfYear(dayOfYear) {
764
+ return this._with(this._date.withDayOfYear(dayOfYear), this._offset);
765
+ }
766
+
767
+ /**
768
+ * Returns a copy of this date with the specified amount added.
769
+ *
770
+ * This returns an `OffsetDate` based on this one, with the amount
771
+ * in terms of the unit added. If it is not possible to add the amount, because the
772
+ * unit is not supported or for some other reason, an exception is thrown.
773
+ *
774
+ * If the field is a {@link ChronoUnit} then the addition is implemented by
775
+ * {@link LocalDate.plus}.
776
+ * The offset is not part of the calculation and will be unchanged in the result.
777
+ *
778
+ * If the field is not a `ChronoUnit`, then the result of this method
779
+ * is obtained by invoking `TemporalUnit.addTo(Temporal, long)`
780
+ * passing `this` as the argument. In this case, the unit determines
781
+ * whether and how to perform the addition.
782
+ *
783
+ * This instance is immutable and unaffected by this method call.
784
+ *
785
+ * @param {number} amountToAdd - the amount of the unit to add to the result, may be negative
786
+ * @param {TemporalUnit} unit - the unit of the amount to add, not null
787
+ * @return {OffsetDate} an `OffsetDate` based on this date with the specified amount added, not null
788
+ * @throws {DateTimeException} if the addition cannot be made
789
+ * @throws {UnsupportedTemporalTypeException} if the unit is not supported
790
+ * @throws {ArithmeticException} if numeric overflow occurs
791
+ */
792
+ _plusUnit(amountToAdd, unit) {
793
+ if (unit instanceof ChronoUnit) {
794
+ return this._with(this._date.plus(amountToAdd, unit), this._offset);
795
+ }
796
+ return unit.addTo(this, amountToAdd);
797
+ }
798
+
799
+ //-----------------------------------------------------------------------
800
+ /**
801
+ * Returns a copy of this `OffsetDate` with the specified number of years added.
802
+ *
803
+ * This uses {@link LocalDate.plusYears} to add the years.
804
+ * The offset does not affect the calculation and will be the same in the result.
805
+ *
806
+ * This instance is immutable and unaffected by this method call.
807
+ *
808
+ * @param {number} years - the years to add, may be negative
809
+ * @return {OffsetDate} an `OffsetDate` based on this date with the years added, not null
810
+ * @throws {DateTimeException} if the result exceeds the supported date range
811
+ */
812
+ plusYears(years) {
813
+ return this._with(this._date.plusYears(years), this._offset);
814
+ }
815
+
816
+ /**
817
+ * Returns a copy of this `OffsetDate` with the specified number of months added.
818
+ *
819
+ * This uses {@link LocalDate.plusMonths} to add the months.
820
+ * The offset does not affect the calculation and will be the same in the result.
821
+ *
822
+ * This instance is immutable and unaffected by this method call.
823
+ *
824
+ * @param {number} months - the months to add, may be negative
825
+ * @return {OffsetDate} an `OffsetDate` based on this date with the months added, not null
826
+ * @throws {DateTimeException} if the result exceeds the supported date range
827
+ */
828
+ plusMonths(months) {
829
+ return this._with(this._date.plusMonths(months), this._offset);
830
+ }
831
+
832
+ /**
833
+ * Returns a copy of this `OffsetDate` with the specified number of weeks added.
834
+ *
835
+ * This uses {@link LocalDate.plusWeeks} to add the weeks.
836
+ * The offset does not affect the calculation and will be the same in the result.
837
+ *
838
+ * This instance is immutable and unaffected by this method call.
839
+ *
840
+ * @param {number} weeks - the weeks to add, may be negative
841
+ * @return {OffsetDate} an `OffsetDate` based on this date with the weeks added, not null
842
+ * @throws {DateTimeException} if the result exceeds the supported date range
843
+ */
844
+ plusWeeks(weeks) {
845
+ return this._with(this._date.plusWeeks(weeks), this._offset);
846
+ }
847
+
848
+ /**
849
+ * Returns a copy of this `OffsetDate` with the specified number of days added.
850
+ *
851
+ * This uses {@link LocalDate.plusDays)} to add the days.
852
+ * The offset does not affect the calculation and will be the same in the result.
853
+ *
854
+ * This instance is immutable and unaffected by this method call.
855
+ *
856
+ * @param {number} days - the days to add, may be negative
857
+ * @return {OffsetDate} an `OffsetDate` based on this date with the days added, not null
858
+ * @throws {DateTimeException} if the result exceeds the supported date range
859
+ */
860
+ plusDays(days) {
861
+ return this._with(this._date.plusDays(days), this._offset);
862
+ }
863
+
864
+ //-----------------------------------------------------------------------
865
+ /**
866
+ * Returns a copy of this `OffsetDate` with the specified number of years subtracted.
867
+ *
868
+ * This uses {@link LocalDate.minusYears} to subtract the years.
869
+ * The offset does not affect the calculation and will be the same in the result.
870
+ *
871
+ * This instance is immutable and unaffected by this method call.
872
+ *
873
+ * @param {number} years - the years to subtract, may be negative
874
+ * @return {OffsetDate} an `OffsetDate` based on this date with the years subtracted, not null
875
+ * @throws {DateTimeException} if the result exceeds the supported date range
876
+ */
877
+ minusYears(years) {
878
+ return this._with(this._date.minusYears(years), this._offset);
879
+ }
880
+
881
+ /**
882
+ * Returns a copy of this `OffsetDate` with the specified number of months subtracted.
883
+ *
884
+ * This uses {@link LocalDate.minusMonths} to subtract the months.
885
+ * The offset does not affect the calculation and will be the same in the result.
886
+ *
887
+ * This instance is immutable and unaffected by this method call.
888
+ *
889
+ * @param {number} months - the months to subtract, may be negative
890
+ * @return {OffsetDate} an `OffsetDate` based on this date with the months subtracted, not null
891
+ * @throws {DateTimeException} if the result exceeds the supported date range
892
+ */
893
+ minusMonths(months) {
894
+ return this._with(this._date.minusMonths(months), this._offset);
895
+ }
896
+
897
+ /**
898
+ * Returns a copy of this `OffsetDate` with the specified number of weeks subtracted.
899
+ *
900
+ * This uses {@link LocalDate.minusWeeks} to subtract the weeks.
901
+ * The offset does not affect the calculation and will be the same in the result.
902
+ *
903
+ * This instance is immutable and unaffected by this method call.
904
+ *
905
+ * @param {number} weeks - the weeks to subtract, may be negative
906
+ * @return {OffsetDate} an `OffsetDate` based on this date with the weeks subtracted, not null
907
+ * @throws {DateTimeException} if the result exceeds the supported date range
908
+ */
909
+ minusWeeks(weeks) {
910
+ return this._with(this._date.minusWeeks(weeks), this._offset);
911
+ }
912
+
913
+ /**
914
+ * Returns a copy of this `OffsetDate` with the specified number of days subtracted.
915
+ *
916
+ * This uses {@link LocalDate.minusDays} to subtract the days.
917
+ * The offset does not affect the calculation and will be the same in the result.
918
+ *
919
+ * This instance is immutable and unaffected by this method call.
920
+ *
921
+ * @param {number} days - the days to subtract, may be negative
922
+ * @return {OffsetDate} an `OffsetDate` based on this date with the days subtracted, not null
923
+ * @throws {DateTimeException} if the result exceeds the supported date range
924
+ */
925
+ minusDays(days) {
926
+ return this._with(this._date.minusDays(days), this._offset);
927
+ }
928
+
929
+ //-----------------------------------------------------------------------
930
+ /**
931
+ * Queries this date using the specified query.
932
+ *
933
+ * This queries this date using the specified query strategy object.
934
+ * The `TemporalQuery` object defines the logic to be used to
935
+ * obtain the result. Read the documentation of the query to understand
936
+ * what the result of this method will be.
937
+ *
938
+ * The result of this method is obtained by invoking the
939
+ * {@link TemporalQuery.queryFrom} method on the
940
+ * specified query passing `this` as the argument.
941
+ *
942
+ * @param {TemporalQuery} query - the query to invoke, not null
943
+ * @return {*} the query result, null may be returned (defined by the query)
944
+ * @throws {DateTimeException} if unable to query (defined by the query)
945
+ * @throws {ArithmeticException} if numeric overflow occurs (defined by the query)
946
+ */
947
+ query(query) {
948
+ requireNonNull(query, 'query');
949
+ requireInstance(query, TemporalQuery, 'query');
950
+ if (query === TemporalQueries.chronology()) {
951
+ return IsoChronology.INSTANCE;
952
+ } else if (query === TemporalQueries.precision()) {
953
+ return ChronoUnit.DAYS;
954
+ } else if (query === TemporalQueries.offset() || query === TemporalQueries.zone()) {
955
+ return this.offset();
956
+ }
957
+ return super.query(query);
958
+ }
959
+
960
+ /**
961
+ * Adjusts the specified temporal object to have the same offset and date
962
+ * as this object.
963
+ *
964
+ * This returns a temporal object of the same observable type as the input
965
+ * with the offset and date changed to be the same as this.
966
+ *
967
+ * The adjustment is equivalent to using {@link Temporal.with}
968
+ * twice, passing {@link ChronoField.OFFSET_SECONDS} and
969
+ * {@link ChronoField.EPOCH_DAY} as the fields.
970
+ *
971
+ * In most cases, it is clearer to reverse the calling pattern by using
972
+ * {@link Temporal.with(TemporalAdjuster)}:
973
+ * <pre>
974
+ * // these two lines are equivalent, but the second approach is recommended
975
+ * temporal = thisOffsetDate.adjustInto(temporal);
976
+ * temporal = temporal.with(thisOffsetDate);
977
+ * </pre>
978
+ *
979
+ * This instance is immutable and unaffected by this method call.
980
+ *
981
+ * @param {Temporal} temporal - the target object to be adjusted, not null
982
+ * @return {Temporal} the adjusted object, not null
983
+ * @throws {DateTimeException} if unable to make the adjustment
984
+ * @throws {ArithmeticException} if numeric overflow occurs
985
+ */
986
+ adjustInto(temporal) {
987
+ return temporal
988
+ .with(ChronoField.OFFSET_SECONDS, this.offset().totalSeconds())
989
+ .with(ChronoField.EPOCH_DAY, this.toLocalDate().toEpochDay());
990
+ }
991
+
992
+ /**
993
+ * Calculates the period between this date and another date in
994
+ * terms of the specified unit.
995
+ *
996
+ * This calculates the period between two dates in terms of a single unit.
997
+ * The start and end points are `this` and the specified date.
998
+ * The result will be negative if the end is before the start.
999
+ * For example, the period in days between two dates can be calculated
1000
+ * using `startDate.until(endDate, DAYS)`.
1001
+ *
1002
+ * The `Temporal` passed to this method is converted to a
1003
+ * `OffsetDate` using {@link OffsetDate.from}.
1004
+ * If the offset differs between the two times, then the specified
1005
+ * end time is normalized to have the same offset as this time.
1006
+ *
1007
+ * The calculation returns a whole number, representing the number of
1008
+ * complete units between the two dates.
1009
+ * For example, the period in months between 2012-06-15Z and 2012-08-14Z
1010
+ * will only be one month as it is one day short of two months.
1011
+ *
1012
+ * There are two equivalent ways of using this method.
1013
+ * The first is to invoke this method.
1014
+ * The second is to use {@link TemporalUnit.between}:
1015
+ * <pre>
1016
+ * // these two lines are equivalent
1017
+ * amount = start.until(end, DAYS);
1018
+ * amount = DAYS.between(start, end);
1019
+ * </pre>
1020
+ * The choice should be made based on which makes the code more readable.
1021
+ *
1022
+ * The calculation is implemented in this method for {@link ChronoUnit}.
1023
+ * The units `DAYS`, `WEEKS`, `MONTHS`, `YEARS`,
1024
+ * `DECADES`, `CENTURIES`, `MILLENNIA` and `ERAS`
1025
+ * are supported. Other `ChronoUnit` values will throw an exception.
1026
+ *
1027
+ * If the unit is not a `ChronoUnit`, then the result of this method
1028
+ * is obtained by invoking `TemporalUnit.between(Temporal, Temporal)`
1029
+ * passing `this` as the first argument and the converted input temporal
1030
+ * as the second argument.
1031
+ *
1032
+ * This instance is immutable and unaffected by this method call.
1033
+ *
1034
+ * @param {Temporal} endExclusive - the end time, exclusive, which is converted to an `OffsetDate`, not null
1035
+ * @param {TemporalUnit} unit - the unit to measure the amount in, not null
1036
+ * @return {number} the amount of time between this date and the end date
1037
+ * @throws {DateTimeException} if the amount cannot be calculated, or the end
1038
+ * temporal cannot be converted to an `OffsetDate`
1039
+ * @throws {UnsupportedTemporalTypeException} if the unit is not supported
1040
+ * @throws {ArithmeticException} if numeric overflow occurs
1041
+ */
1042
+ until(endExclusive, unit) {
1043
+ const end = OffsetDate.from(endExclusive);
1044
+ if (unit instanceof ChronoUnit) {
1045
+ const offsetDiff = end._offset.totalSeconds() - this._offset.totalSeconds();
1046
+ const endLocal = end._date.plusDays(MathUtil.intDiv(-offsetDiff, SECONDS_PER_DAY));
1047
+ return this._date.until(endLocal, unit);
1048
+ }
1049
+ return unit.between(this, end);
1050
+ }
1051
+
1052
+ /**
1053
+ * Formats this date using the specified formatter.
1054
+ *
1055
+ * This date will be passed to the formatter to produce a string.
1056
+ *
1057
+ * @param {DateTimeFormatter} formatter - the formatter to use, not null
1058
+ * @return {string} the formatted date string, not null
1059
+ * @throws {DateTimeException} if an error occurs during printing
1060
+ */
1061
+ format(formatter) {
1062
+ requireNonNull(formatter, 'formatter');
1063
+ return formatter.format(this);
1064
+ }
1065
+
1066
+ //-----------------------------------------------------------------------
1067
+ /**
1068
+ * Returns an offset date-time formed from this date at the specified time.
1069
+ *
1070
+ * This combines this date with the specified time to form an `OffsetDateTime`.
1071
+ * All possible combinations of date and time are valid.
1072
+ *
1073
+ * This instance is immutable and unaffected by this method call.
1074
+ *
1075
+ * @param {LocalTime} time - the time to combine with, not null
1076
+ * @return {OffsetDateTime} the offset date-time formed from this date and the specified time, not null
1077
+ */
1078
+ atTime(time) {
1079
+ return OffsetDateTime.of(this._date, time, this._offset);
1080
+ }
1081
+
1082
+ //-----------------------------------------------------------------------
1083
+ /**
1084
+ * Converts this date to midnight at the start of day in epoch seconds.
1085
+ *
1086
+ * @return {number} the epoch seconds value
1087
+ * @private
1088
+ */
1089
+ _toEpochSecond() {
1090
+ const epochDay = this._date.toEpochDay();
1091
+ const secs = epochDay * SECONDS_PER_DAY;
1092
+ return secs - this._offset.totalSeconds();
1093
+ }
1094
+
1095
+ /**
1096
+ * Converts this `OffsetDate` to the number of seconds since the epoch
1097
+ * of 1970-01-01T00:00:00Z.
1098
+ *
1099
+ * This combines this offset date with the specified time
1100
+ * to calculate the epoch-second value, which is the
1101
+ * number of elapsed seconds from 1970-01-01T00:00:00Z.
1102
+ * Instants on the time-line after the epoch are positive, earlier
1103
+ * are negative.
1104
+ *
1105
+ * @param {LocalTime} time - the local time, not null
1106
+ * @return {number} the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1107
+ */
1108
+ toEpochSecond(time) {
1109
+ requireNonNull(time, 'time');
1110
+ return this._toEpochSecond() + time.toSecondOfDay();
1111
+ }
1112
+
1113
+ //-----------------------------------------------------------------------
1114
+ /**
1115
+ * Compares this `OffsetDate` to another date.
1116
+ *
1117
+ * The comparison is based first on the UTC equivalent instant, then on the local date.
1118
+ * It is 'consistent with equals', as defined by {@link Comparable}.
1119
+ *
1120
+ * For example, the following is the comparator order:
1121
+ * 1. 2008-06-29-11:00</li>
1122
+ * 2. 2008-06-29-12:00</li>
1123
+ * 3. 2008-06-30+12:00</li>
1124
+ * 4. 2008-06-29-13:00</li>
1125
+ *
1126
+ * Values #2 and #3 represent the same instant on the time-line.
1127
+ * When two values represent the same instant, the local date is compared
1128
+ * to distinguish them. This step is needed to make the ordering
1129
+ * consistent with `equals()`.
1130
+ *
1131
+ * To compare the underlying local date of two `TemporalAccessor` instances,
1132
+ * use {@link ChronoField.EPOCH_DAY} as a comparator.
1133
+ *
1134
+ * @param {OffsetDate} other - the other date to compare to, not null
1135
+ * @return {number} the comparator value, negative if less, positive if greater
1136
+ */
1137
+ compareTo(other) {
1138
+ requireNonNull(other, 'other');
1139
+ requireInstance(other, OffsetDate, 'other');
1140
+ if (this._offset.equals(other._offset)) {
1141
+ return this._date.compareTo(other._date);
1142
+ }
1143
+ let compare = this._toEpochSecond() - other._toEpochSecond();
1144
+ if (compare === 0) {
1145
+ compare = this._date.compareTo(other._date);
1146
+ }
1147
+ return compare;
1148
+ }
1149
+
1150
+ //-----------------------------------------------------------------------
1151
+ /**
1152
+ * Checks if the instant of midnight at the start of this `OffsetDate`
1153
+ * is after midnight at the start of the specified date.
1154
+ *
1155
+ * This method differs from the comparison in {@link OffsetDate.compareTo} in that it
1156
+ * only compares the instant of the date. This is equivalent to using
1157
+ * `date1.toEpochSecond().isAfter(date2.toEpochSecond())`.
1158
+ *
1159
+ * @param {OffsetDate} other - the other date to compare to, not null
1160
+ * @return {boolean} true if this is after the instant of the specified date
1161
+ */
1162
+ isAfter(other) {
1163
+ requireNonNull(other, 'other');
1164
+ requireInstance(other, OffsetDate, 'other');
1165
+ return this._toEpochSecond() > other._toEpochSecond();
1166
+ }
1167
+
1168
+ /**
1169
+ * Checks if the instant of midnight at the start of this `OffsetDate`
1170
+ * is before midnight at the start of the specified date.
1171
+ *
1172
+ * This method differs from the comparison in {@link OffsetDate.compareTo} in that it
1173
+ * only compares the instant of the date. This is equivalent to using
1174
+ * `date1.toEpochSecond().isBefore(date2.toEpochSecond())`.
1175
+ *
1176
+ * @param {OffsetDate} other - the other date to compare to, not null
1177
+ * @return {boolean} true if this is before the instant of the specified date
1178
+ */
1179
+ isBefore(other) {
1180
+ requireNonNull(other, 'other');
1181
+ requireInstance(other, OffsetDate, 'other');
1182
+ return this._toEpochSecond() < other._toEpochSecond();
1183
+ }
1184
+
1185
+ /**
1186
+ * Checks if the instant of midnight at the start of this `OffsetDate`
1187
+ * equals midnight at the start of the specified date.
1188
+ *
1189
+ * This method differs from the comparison in {@link OffsetDate.compareTo} and {@link OffsetDate.equals}
1190
+ * in that it only compares the instant of the date. This is equivalent to using
1191
+ * `date1.toEpochSecond().equals(date2.toEpochSecond())`.
1192
+ *
1193
+ * @param {OffsetDate} other - the other date to compare to, not null
1194
+ * @return {boolean} true if the instant equals the instant of the specified date
1195
+ */
1196
+ isEqual(other) {
1197
+ requireNonNull(other, 'other');
1198
+ requireInstance(other, OffsetDate, 'other');
1199
+ return this._toEpochSecond() === other._toEpochSecond();
1200
+ }
1201
+
1202
+ //-----------------------------------------------------------------------
1203
+ /**
1204
+ * Checks if this date is equal to another date.
1205
+ *
1206
+ * The comparison is based on the local-date and the offset.
1207
+ * To compare for the same instant on the time-line, use {@link OffsetDate.isEqual}.
1208
+ *
1209
+ * Only objects of type `OffsetDate` are compared, other types return false.
1210
+ * To compare the underlying local date of two `TemporalAccessor` instances,
1211
+ * use {@link ChronoField.EPOCH_DAY} as a comparator.
1212
+ *
1213
+ * @param {Object} obj - the object to check, null returns false
1214
+ * @return {boolean} true if this is equal to the other date
1215
+ */
1216
+ equals(obj) {
1217
+ if (this === obj) {
1218
+ return true;
1219
+ }
1220
+ if (obj instanceof OffsetDate) {
1221
+ const other = obj;
1222
+ return this._date.equals(other._date) && this._offset.equals(other._offset);
1223
+ }
1224
+ return false;
1225
+ }
1226
+
1227
+ /**
1228
+ * A hash code for this date.
1229
+ *
1230
+ * @return {number} a suitable hash code
1231
+ */
1232
+ hashCode() {
1233
+ return this._date.hashCode() ^ this._offset.hashCode();
1234
+ }
1235
+
1236
+ //-----------------------------------------------------------------------
1237
+ /**
1238
+ * Outputs this date as a `String`, such as `2007-12-03+01:00`.
1239
+ *
1240
+ * The output will be in the ISO-8601 format `yyyy-MM-ddXXXXX`.
1241
+ *
1242
+ * @return {String} a string representation of this date, not null
1243
+ */
1244
+ toString() {
1245
+ return this._date.toString() + this._offset.toString();
1246
+ }
1247
+ }
1248
+
1249
+ export function _init() {
1250
+ /**
1251
+ * The minimum supported `OffsetDate`, '-999999999-01-01+18:00'.
1252
+ * This is the minimum local date in the maximum offset
1253
+ * (larger offsets are earlier on the time-line).
1254
+ * This combines {@link LocalDate.MIN} and {@link ZoneOffset.MAX}.
1255
+ * This could be used by an application as a 'far past' date.
1256
+ */
1257
+ OffsetDate.MIN = OffsetDate.of(LocalDate.MIN, ZoneOffset.MAX);
1258
+ /**
1259
+ * The maximum supported `OffsetDate`, '+999999999-12-31-18:00'.
1260
+ * This is the maximum local date in the minimum offset
1261
+ * (larger negative offsets are later on the time-line).
1262
+ * This combines {@link LocalDate.MAX} and {@link ZoneOffset.MIN}.
1263
+ * This could be used by an application as a 'far future' date.
1264
+ */
1265
+ OffsetDate.MAX = OffsetDate.of(LocalDate.MAX, ZoneOffset.MIN);
1266
+
1267
+ OffsetDate.FROM = createTemporalQuery('OffsetDate.FROM', (temporal) => {
1268
+ return OffsetDate.from(temporal);
1269
+ });
1270
+ }
1271
+
1272
+ // copied from packages/core/src/temporal/TemporalQuery.js
1273
+ function createTemporalQuery(name, queryFromFunction) {
1274
+ class ExtendedTemporalQuery extends TemporalQuery {
1275
+ }
1276
+
1277
+ ExtendedTemporalQuery.prototype.queryFrom = queryFromFunction;
1278
+ return new ExtendedTemporalQuery(name);
1279
+ }