-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-joda.js
More file actions
12571 lines (9718 loc) · 401 KB
/
js-joda.js
File metadata and controls
12571 lines (9718 loc) · 401 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! @version @js-joda/core - 2.0.0
//! @copyright (c) 2015-present, Philipp Thürwächter, Pattrick Hüper & js-joda contributors
//! @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
//! @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.JSJoda = {}));
}(this, (function (exports) { 'use strict';
/**
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
function createErrorType(name, init, superErrorClass) {
if (superErrorClass === void 0) {
superErrorClass = Error;
}
function E(message) {
if (!Error.captureStackTrace) {
this.stack = new Error().stack;
} else {
Error.captureStackTrace(this, this.constructor);
}
this.message = message;
init && init.apply(this, arguments);
this.toString = function () {
return this.name + ": " + this.message;
};
}
E.prototype = new superErrorClass();
E.prototype.name = name;
E.prototype.constructor = E;
return E;
}
var DateTimeException = createErrorType('DateTimeException', messageWithCause);
var DateTimeParseException = createErrorType('DateTimeParseException', messageForDateTimeParseException);
var UnsupportedTemporalTypeException = createErrorType('UnsupportedTemporalTypeException', null, DateTimeException);
var ArithmeticException = createErrorType('ArithmeticException');
var IllegalArgumentException = createErrorType('IllegalArgumentException');
var IllegalStateException = createErrorType('IllegalStateException');
var NullPointerException = createErrorType('NullPointerException');
function messageWithCause(message, cause) {
if (cause === void 0) {
cause = null;
}
var msg = message || this.name;
if (cause !== null && cause instanceof Error) {
msg += '\n-------\nCaused by: ' + cause.stack + '\n-------\n';
}
this.message = msg;
}
function messageForDateTimeParseException(message, text, index, cause) {
if (text === void 0) {
text = '';
}
if (index === void 0) {
index = 0;
}
if (cause === void 0) {
cause = null;
}
var msg = message || this.name;
msg += ': ' + text + ', at index: ' + index;
if (cause !== null && cause instanceof Error) {
msg += '\n-------\nCaused by: ' + cause.stack + '\n-------\n';
}
this.message = msg;
this.parsedString = function () {
return text;
};
this.errorIndex = function () {
return index;
};
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
/**
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
function assert(assertion, msg, error) {
if (!assertion) {
if (error) {
throw new error(msg);
} else {
throw new Error(msg);
}
}
}
function requireNonNull(value, parameterName) {
if (value == null) {
throw new NullPointerException(parameterName + ' must not be null');
}
return value;
}
function requireInstance(value, _class, parameterName) {
if (!(value instanceof _class)) {
throw new IllegalArgumentException(parameterName + ' must be an instance of ' + (_class.name ? _class.name : _class) + (value && value.constructor && value.constructor.name ? ', but is ' + value.constructor.name : ''));
}
return value;
}
function abstractMethodFail(methodName) {
throw new TypeError('abstract method "' + methodName + '" is not implemented');
}
var assert$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
assert: assert,
requireNonNull: requireNonNull,
requireInstance: requireInstance,
abstractMethodFail: abstractMethodFail
});
/**
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
var MAX_SAFE_INTEGER = 9007199254740991;
var MIN_SAFE_INTEGER = -9007199254740991;
var MathUtil = function () {
function MathUtil() {}
MathUtil.intDiv = function intDiv(x, y) {
var r = x / y;
r = MathUtil.roundDown(r);
return MathUtil.safeZero(r);
};
MathUtil.intMod = function intMod(x, y) {
var r = x - MathUtil.intDiv(x, y) * y;
r = MathUtil.roundDown(r);
return MathUtil.safeZero(r);
};
MathUtil.roundDown = function roundDown(r) {
if (r < 0) {
return Math.ceil(r);
} else {
return Math.floor(r);
}
};
MathUtil.floorDiv = function floorDiv(x, y) {
var r = Math.floor(x / y);
return MathUtil.safeZero(r);
};
MathUtil.floorMod = function floorMod(x, y) {
var r = x - MathUtil.floorDiv(x, y) * y;
return MathUtil.safeZero(r);
};
MathUtil.safeAdd = function safeAdd(x, y) {
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 0) {
return MathUtil.safeZero(y);
}
if (y === 0) {
return MathUtil.safeZero(x);
}
var r = MathUtil.safeToInt(x + y);
if (r === x || r === y) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');
}
return r;
};
MathUtil.safeSubtract = function safeSubtract(x, y) {
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 0 && y === 0) {
return 0;
} else if (x === 0) {
return MathUtil.safeZero(-1 * y);
} else if (y === 0) {
return MathUtil.safeZero(x);
}
return MathUtil.safeToInt(x - y);
};
MathUtil.safeMultiply = function safeMultiply(x, y) {
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 1) {
return MathUtil.safeZero(y);
}
if (y === 1) {
return MathUtil.safeZero(x);
}
if (x === 0 || y === 0) {
return 0;
}
var r = MathUtil.safeToInt(x * y);
if (r / y !== x || x === MIN_SAFE_INTEGER && y === -1 || y === MIN_SAFE_INTEGER && x === -1) {
throw new ArithmeticException('Multiplication overflows: ' + x + ' * ' + y);
}
return r;
};
MathUtil.parseInt = function (_parseInt) {
function parseInt(_x) {
return _parseInt.apply(this, arguments);
}
parseInt.toString = function () {
return _parseInt.toString();
};
return parseInt;
}(function (value) {
var r = parseInt(value);
return MathUtil.safeToInt(r);
});
MathUtil.safeToInt = function safeToInt(value) {
MathUtil.verifyInt(value);
return MathUtil.safeZero(value);
};
MathUtil.verifyInt = function verifyInt(value) {
if (value == null) {
throw new ArithmeticException("Invalid value: '" + value + "', using null or undefined as argument");
}
if (isNaN(value)) {
throw new ArithmeticException('Invalid int value, using NaN as argument');
}
if (value % 1 !== 0) {
throw new ArithmeticException("Invalid value: '" + value + "' is a float");
}
if (value > MAX_SAFE_INTEGER || value < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Calculation overflows an int: ' + value);
}
};
MathUtil.safeZero = function safeZero(value) {
return value === 0 ? 0 : +value;
};
MathUtil.compareNumbers = function compareNumbers(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};
MathUtil.smi = function smi(int) {
return int >>> 1 & 0x40000000 | int & 0xBFFFFFFF;
};
MathUtil.hash = function hash(number) {
if (number !== number || number === Infinity) {
return 0;
}
var result = number;
while (number > 0xFFFFFFFF) {
number /= 0xFFFFFFFF;
result ^= number;
}
return MathUtil.smi(result);
};
MathUtil.hashCode = function hashCode() {
var result = 17;
for (var _len = arguments.length, numbers = new Array(_len), _key = 0; _key < _len; _key++) {
numbers[_key] = arguments[_key];
}
for (var _i = 0, _numbers = numbers; _i < _numbers.length; _i++) {
var n = _numbers[_i];
result = (result << 5) - result + MathUtil.hash(n);
}
return MathUtil.hash(result);
};
return MathUtil;
}();
MathUtil.MAX_SAFE_INTEGER = MAX_SAFE_INTEGER;
MathUtil.MIN_SAFE_INTEGER = MIN_SAFE_INTEGER;
/**
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
var Enum = function () {
function Enum(name) {
this._name = name;
}
var _proto = Enum.prototype;
_proto.equals = function equals(other) {
return this === other;
};
_proto.toString = function toString() {
return this._name;
};
_proto.toJSON = function toJSON() {
return this.toString();
};
return Enum;
}();
/*
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
var TemporalAmount = function () {
function TemporalAmount() {}
var _proto = TemporalAmount.prototype;
_proto.get = function get(unit) {
abstractMethodFail('get');
};
_proto.units = function units() {
abstractMethodFail('units');
};
_proto.addTo = function addTo(temporal) {
abstractMethodFail('addTo');
};
_proto.subtractFrom = function subtractFrom(temporal) {
abstractMethodFail('subtractFrom');
};
return TemporalAmount;
}();
/*
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
* @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
*/
var TemporalUnit = function () {
function TemporalUnit() {}
var _proto = TemporalUnit.prototype;
_proto.duration = function duration() {
abstractMethodFail('duration');
};
_proto.isDurationEstimated = function isDurationEstimated() {
abstractMethodFail('isDurationEstimated');
};
_proto.isDateBased = function isDateBased() {
abstractMethodFail('isDateBased');
};
_proto.isTimeBased = function isTimeBased() {
abstractMethodFail('isTimeBased');
};
_proto.isSupportedBy = function isSupportedBy(temporal) {
abstractMethodFail('isSupportedBy');
};
_proto.addTo = function addTo(dateTime, periodToAdd) {
abstractMethodFail('addTo');
};
_proto.between = function between(temporal1, temporal2) {
abstractMethodFail('between');
};
return TemporalUnit;
}();
var Duration = function (_TemporalAmount) {
_inheritsLoose(Duration, _TemporalAmount);
function Duration(seconds, nanos) {
var _this;
_this = _TemporalAmount.call(this) || this;
_this._seconds = MathUtil.safeToInt(seconds);
_this._nanos = MathUtil.safeToInt(nanos);
return _this;
}
Duration.ofDays = function ofDays(days) {
return Duration._create(MathUtil.safeMultiply(days, LocalTime.SECONDS_PER_DAY), 0);
};
Duration.ofHours = function ofHours(hours) {
return Duration._create(MathUtil.safeMultiply(hours, LocalTime.SECONDS_PER_HOUR), 0);
};
Duration.ofMinutes = function ofMinutes(minutes) {
return Duration._create(MathUtil.safeMultiply(minutes, LocalTime.SECONDS_PER_MINUTE), 0);
};
Duration.ofSeconds = function ofSeconds(seconds, nanoAdjustment) {
if (nanoAdjustment === void 0) {
nanoAdjustment = 0;
}
var secs = MathUtil.safeAdd(seconds, MathUtil.floorDiv(nanoAdjustment, LocalTime.NANOS_PER_SECOND));
var nos = MathUtil.floorMod(nanoAdjustment, LocalTime.NANOS_PER_SECOND);
return Duration._create(secs, nos);
};
Duration.ofMillis = function ofMillis(millis) {
var secs = MathUtil.intDiv(millis, 1000);
var mos = MathUtil.intMod(millis, 1000);
if (mos < 0) {
mos += 1000;
secs--;
}
return Duration._create(secs, mos * 1000000);
};
Duration.ofNanos = function ofNanos(nanos) {
var secs = MathUtil.intDiv(nanos, LocalTime.NANOS_PER_SECOND);
var nos = MathUtil.intMod(nanos, LocalTime.NANOS_PER_SECOND);
if (nos < 0) {
nos += LocalTime.NANOS_PER_SECOND;
secs--;
}
return this._create(secs, nos);
};
Duration.of = function of(amount, unit) {
return Duration.ZERO.plus(amount, unit);
};
Duration.from = function from(amount) {
requireNonNull(amount, 'amount');
requireInstance(amount, TemporalAmount);
var duration = Duration.ZERO;
amount.units().forEach(function (unit) {
duration = duration.plus(amount.get(unit), unit);
});
return duration;
};
Duration.between = function between(startInclusive, endExclusive) {
requireNonNull(startInclusive, 'startInclusive');
requireNonNull(endExclusive, 'endExclusive');
var secs = startInclusive.until(endExclusive, ChronoUnit.SECONDS);
var nanos = 0;
if (startInclusive.isSupported(ChronoField.NANO_OF_SECOND) && endExclusive.isSupported(ChronoField.NANO_OF_SECOND)) {
try {
var startNos = startInclusive.getLong(ChronoField.NANO_OF_SECOND);
nanos = endExclusive.getLong(ChronoField.NANO_OF_SECOND) - startNos;
if (secs > 0 && nanos < 0) {
nanos += LocalTime.NANOS_PER_SECOND;
} else if (secs < 0 && nanos > 0) {
nanos -= LocalTime.NANOS_PER_SECOND;
} else if (secs === 0 && nanos !== 0) {
var adjustedEnd = endExclusive.with(ChronoField.NANO_OF_SECOND, startNos);
secs = startInclusive.until(adjustedEnd, ChronoUnit.SECONDS);
}
} catch (e) {}
}
return this.ofSeconds(secs, nanos);
};
Duration.parse = function parse(text) {
requireNonNull(text, 'text');
var PATTERN = new RegExp('([-+]?)P(?:([-+]?[0-9]+)D)?(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?', 'i');
var matches = PATTERN.exec(text);
if (matches !== null) {
if ('T' === matches[3] === false) {
var negate = '-' === matches[1];
var dayMatch = matches[2];
var hourMatch = matches[4];
var minuteMatch = matches[5];
var secondMatch = matches[6];
var fractionMatch = matches[7];
if (dayMatch != null || hourMatch != null || minuteMatch != null || secondMatch != null) {
var daysAsSecs = Duration._parseNumber(text, dayMatch, LocalTime.SECONDS_PER_DAY, 'days');
var hoursAsSecs = Duration._parseNumber(text, hourMatch, LocalTime.SECONDS_PER_HOUR, 'hours');
var minsAsSecs = Duration._parseNumber(text, minuteMatch, LocalTime.SECONDS_PER_MINUTE, 'minutes');
var seconds = Duration._parseNumber(text, secondMatch, 1, 'seconds');
var negativeSecs = secondMatch != null && secondMatch.charAt(0) === '-';
var nanos = Duration._parseFraction(text, fractionMatch, negativeSecs ? -1 : 1);
try {
return Duration._create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
} catch (ex) {
throw new DateTimeParseException('Text cannot be parsed to a Duration: overflow', text, 0, ex);
}
}
}
}
throw new DateTimeParseException('Text cannot be parsed to a Duration', text, 0);
};
Duration._parseNumber = function _parseNumber(text, parsed, multiplier, errorText) {
if (parsed == null) {
return 0;
}
try {
if (parsed[0] === '+') {
parsed = parsed.substring(1);
}
return MathUtil.safeMultiply(parseFloat(parsed), multiplier);
} catch (ex) {
throw new DateTimeParseException('Text cannot be parsed to a Duration: ' + errorText, text, 0, ex);
}
};
Duration._parseFraction = function _parseFraction(text, parsed, negate) {
if (parsed == null || parsed.length === 0) {
return 0;
}
parsed = (parsed + '000000000').substring(0, 9);
return parseFloat(parsed) * negate;
};
Duration._create = function _create() {
if (arguments.length <= 2) {
return Duration._createSecondsNanos(arguments[0], arguments[1]);
} else {
return Duration._createNegateDaysHoursMinutesSecondsNanos(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
}
};
Duration._createNegateDaysHoursMinutesSecondsNanos = function _createNegateDaysHoursMinutesSecondsNanos(negate, daysAsSecs, hoursAsSecs, minsAsSecs, secs, nanos) {
var seconds = MathUtil.safeAdd(daysAsSecs, MathUtil.safeAdd(hoursAsSecs, MathUtil.safeAdd(minsAsSecs, secs)));
if (negate) {
return Duration.ofSeconds(seconds, nanos).negated();
}
return Duration.ofSeconds(seconds, nanos);
};
Duration._createSecondsNanos = function _createSecondsNanos(seconds, nanoAdjustment) {
if (seconds === void 0) {
seconds = 0;
}
if (nanoAdjustment === void 0) {
nanoAdjustment = 0;
}
if ((seconds | nanoAdjustment) === 0) {
return Duration.ZERO;
}
return new Duration(seconds, nanoAdjustment);
};
var _proto = Duration.prototype;
_proto.get = function get(unit) {
if (unit === ChronoUnit.SECONDS) {
return this._seconds;
} else if (unit === ChronoUnit.NANOS) {
return this._nanos;
} else {
throw new UnsupportedTemporalTypeException('Unsupported unit: ' + unit);
}
};
_proto.units = function units() {
return [ChronoUnit.SECONDS, ChronoUnit.NANOS];
};
_proto.isZero = function isZero() {
return (this._seconds | this._nanos) === 0;
};
_proto.isNegative = function isNegative() {
return this._seconds < 0;
};
_proto.seconds = function seconds() {
return this._seconds;
};
_proto.nano = function nano() {
return this._nanos;
};
_proto.withSeconds = function withSeconds(seconds) {
return Duration._create(seconds, this._nanos);
};
_proto.withNanos = function withNanos(nanoOfSecond) {
ChronoField.NANO_OF_SECOND.checkValidIntValue(nanoOfSecond);
return Duration._create(this._seconds, nanoOfSecond);
};
_proto.plusDuration = function plusDuration(duration) {
requireNonNull(duration, 'duration');
return this.plus(duration.seconds(), duration.nano());
};
_proto.plus = function plus(durationOrNumber, unitOrNumber) {
if (arguments.length === 1) {
return this.plusDuration(durationOrNumber);
} else if (arguments.length === 2 && unitOrNumber instanceof TemporalUnit) {
return this.plusAmountUnit(durationOrNumber, unitOrNumber);
} else {
return this.plusSecondsNanos(durationOrNumber, unitOrNumber);
}
};
_proto.plusAmountUnit = function plusAmountUnit(amountToAdd, unit) {
requireNonNull(amountToAdd, 'amountToAdd');
requireNonNull(unit, 'unit');
if (unit === ChronoUnit.DAYS) {
return this.plusSecondsNanos(MathUtil.safeMultiply(amountToAdd, LocalTime.SECONDS_PER_DAY), 0);
}
if (unit.isDurationEstimated()) {
throw new UnsupportedTemporalTypeException('Unit must not have an estimated duration');
}
if (amountToAdd === 0) {
return this;
}
if (unit instanceof ChronoUnit) {
switch (unit) {
case ChronoUnit.NANOS:
return this.plusNanos(amountToAdd);
case ChronoUnit.MICROS:
return this.plusSecondsNanos(MathUtil.intDiv(amountToAdd, 1000000 * 1000) * 1000, MathUtil.intMod(amountToAdd, 1000000 * 1000) * 1000);
case ChronoUnit.MILLIS:
return this.plusMillis(amountToAdd);
case ChronoUnit.SECONDS:
return this.plusSeconds(amountToAdd);
}
return this.plusSecondsNanos(MathUtil.safeMultiply(unit.duration().seconds(), amountToAdd), 0);
}
var duration = unit.duration().multipliedBy(amountToAdd);
return this.plusSecondsNanos(duration.seconds(), duration.nano());
};
_proto.plusDays = function plusDays(daysToAdd) {
return this.plusSecondsNanos(MathUtil.safeMultiply(daysToAdd, LocalTime.SECONDS_PER_DAY), 0);
};
_proto.plusHours = function plusHours(hoursToAdd) {
return this.plusSecondsNanos(MathUtil.safeMultiply(hoursToAdd, LocalTime.SECONDS_PER_HOUR), 0);
};
_proto.plusMinutes = function plusMinutes(minutesToAdd) {
return this.plusSecondsNanos(MathUtil.safeMultiply(minutesToAdd, LocalTime.SECONDS_PER_MINUTE), 0);
};
_proto.plusSeconds = function plusSeconds(secondsToAdd) {
return this.plusSecondsNanos(secondsToAdd, 0);
};
_proto.plusMillis = function plusMillis(millisToAdd) {
return this.plusSecondsNanos(MathUtil.intDiv(millisToAdd, 1000), MathUtil.intMod(millisToAdd, 1000) * 1000000);
};
_proto.plusNanos = function plusNanos(nanosToAdd) {
return this.plusSecondsNanos(0, nanosToAdd);
};
_proto.plusSecondsNanos = function plusSecondsNanos(secondsToAdd, nanosToAdd) {
requireNonNull(secondsToAdd, 'secondsToAdd');
requireNonNull(nanosToAdd, 'nanosToAdd');
if ((secondsToAdd | nanosToAdd) === 0) {
return this;
}
var epochSec = MathUtil.safeAdd(this._seconds, secondsToAdd);
epochSec = MathUtil.safeAdd(epochSec, MathUtil.intDiv(nanosToAdd, LocalTime.NANOS_PER_SECOND));
nanosToAdd = MathUtil.intMod(nanosToAdd, LocalTime.NANOS_PER_SECOND);
var nanoAdjustment = MathUtil.safeAdd(this._nanos, nanosToAdd);
return Duration.ofSeconds(epochSec, nanoAdjustment);
};
_proto.minus = function minus(durationOrNumber, unit) {
if (arguments.length === 1) {
return this.minusDuration(durationOrNumber);
} else {
return this.minusAmountUnit(durationOrNumber, unit);
}
};
_proto.minusDuration = function minusDuration(duration) {
requireNonNull(duration, 'duration');
var secsToSubtract = duration.seconds();
var nanosToSubtract = duration.nano();
if (secsToSubtract === MIN_SAFE_INTEGER) {
return this.plus(MAX_SAFE_INTEGER, -nanosToSubtract);
}
return this.plus(-secsToSubtract, -nanosToSubtract);
};
_proto.minusAmountUnit = function minusAmountUnit(amountToSubtract, unit) {
requireNonNull(amountToSubtract, 'amountToSubtract');
requireNonNull(unit, 'unit');
return amountToSubtract === MIN_SAFE_INTEGER ? this.plusAmountUnit(MAX_SAFE_INTEGER, unit) : this.plusAmountUnit(-amountToSubtract, unit);
};
_proto.minusDays = function minusDays(daysToSubtract) {
return daysToSubtract === MIN_SAFE_INTEGER ? this.plusDays(MAX_SAFE_INTEGER) : this.plusDays(-daysToSubtract);
};
_proto.minusHours = function minusHours(hoursToSubtract) {
return hoursToSubtract === MIN_SAFE_INTEGER ? this.plusHours(MAX_SAFE_INTEGER) : this.plusHours(-hoursToSubtract);
};
_proto.minusMinutes = function minusMinutes(minutesToSubtract) {
return minutesToSubtract === MIN_SAFE_INTEGER ? this.plusMinutes(MAX_SAFE_INTEGER) : this.plusMinutes(-minutesToSubtract);
};
_proto.minusSeconds = function minusSeconds(secondsToSubtract) {
return secondsToSubtract === MIN_SAFE_INTEGER ? this.plusSeconds(MAX_SAFE_INTEGER) : this.plusSeconds(-secondsToSubtract);
};
_proto.minusMillis = function minusMillis(millisToSubtract) {
return millisToSubtract === MIN_SAFE_INTEGER ? this.plusMillis(MAX_SAFE_INTEGER) : this.plusMillis(-millisToSubtract);
};
_proto.minusNanos = function minusNanos(nanosToSubtract) {
return nanosToSubtract === MIN_SAFE_INTEGER ? this.plusNanos(MAX_SAFE_INTEGER) : this.plusNanos(-nanosToSubtract);
};
_proto.multipliedBy = function multipliedBy(multiplicand) {
if (multiplicand === 0) {
return Duration.ZERO;
}
if (multiplicand === 1) {
return this;
}
var secs = MathUtil.safeMultiply(this._seconds, multiplicand);
var nos = MathUtil.safeMultiply(this._nanos, multiplicand);
secs = secs + MathUtil.intDiv(nos, LocalTime.NANOS_PER_SECOND);
nos = MathUtil.intMod(nos, LocalTime.NANOS_PER_SECOND);
return Duration.ofSeconds(secs, nos);
};
_proto.dividedBy = function dividedBy(divisor) {
if (divisor === 0) {
throw new ArithmeticException('Cannot divide by zero');
}
if (divisor === 1) {
return this;
}
var secs = MathUtil.intDiv(this._seconds, divisor);
var secsMod = MathUtil.roundDown((this._seconds / divisor - secs) * LocalTime.NANOS_PER_SECOND);
var nos = MathUtil.intDiv(this._nanos, divisor);
nos = secsMod + nos;
return Duration.ofSeconds(secs, nos);
};
_proto.negated = function negated() {
return this.multipliedBy(-1);
};
_proto.abs = function abs() {
return this.isNegative() ? this.negated() : this;
};
_proto.addTo = function addTo(temporal) {
requireNonNull(temporal, 'temporal');
if (this._seconds !== 0) {
temporal = temporal.plus(this._seconds, ChronoUnit.SECONDS);
}
if (this._nanos !== 0) {
temporal = temporal.plus(this._nanos, ChronoUnit.NANOS);
}
return temporal;
};
_proto.subtractFrom = function subtractFrom(temporal) {
requireNonNull(temporal, 'temporal');
if (this._seconds !== 0) {
temporal = temporal.minus(this._seconds, ChronoUnit.SECONDS);
}
if (this._nanos !== 0) {
temporal = temporal.minus(this._nanos, ChronoUnit.NANOS);
}
return temporal;
};
_proto.toDays = function toDays() {
return MathUtil.intDiv(this._seconds, LocalTime.SECONDS_PER_DAY);
};
_proto.toHours = function toHours() {
return MathUtil.intDiv(this._seconds, LocalTime.SECONDS_PER_HOUR);
};
_proto.toMinutes = function toMinutes() {
return MathUtil.intDiv(this._seconds, LocalTime.SECONDS_PER_MINUTE);
};
_proto.toMillis = function toMillis() {
var millis = Math.round(MathUtil.safeMultiply(this._seconds, 1000));
millis = MathUtil.safeAdd(millis, MathUtil.intDiv(this._nanos, 1000000));
return millis;
};
_proto.toNanos = function toNanos() {
var totalNanos = MathUtil.safeMultiply(this._seconds, LocalTime.NANOS_PER_SECOND);
totalNanos = MathUtil.safeAdd(totalNanos, this._nanos);
return totalNanos;
};
_proto.compareTo = function compareTo(otherDuration) {
requireNonNull(otherDuration, 'otherDuration');
requireInstance(otherDuration, Duration, 'otherDuration');
var cmp = MathUtil.compareNumbers(this._seconds, otherDuration.seconds());
if (cmp !== 0) {
return cmp;
}
return this._nanos - otherDuration.nano();
};
_proto.equals = function equals(otherDuration) {
if (this === otherDuration) {
return true;
}
if (otherDuration instanceof Duration) {
return this.seconds() === otherDuration.seconds() && this.nano() === otherDuration.nano();
}
return false;
};
_proto.toString = function toString() {
if (this === Duration.ZERO) {
return 'PT0S';
}
var hours = MathUtil.intDiv(this._seconds, LocalTime.SECONDS_PER_HOUR);
var minutes = MathUtil.intDiv(MathUtil.intMod(this._seconds, LocalTime.SECONDS_PER_HOUR), LocalTime.SECONDS_PER_MINUTE);
var secs = MathUtil.intMod(this._seconds, LocalTime.SECONDS_PER_MINUTE);
var rval = 'PT';
if (hours !== 0) {
rval += hours + 'H';
}
if (minutes !== 0) {
rval += minutes + 'M';
}
if (secs === 0 && this._nanos === 0 && rval.length > 2) {
return rval;
}
if (secs < 0 && this._nanos > 0) {
if (secs === -1) {
rval += '-0';
} else {
rval += secs + 1;
}
} else {
rval += secs;
}
if (this._nanos > 0) {
rval += '.';
var nanoString;
if (secs < 0) {
nanoString = '' + (2 * LocalTime.NANOS_PER_SECOND - this._nanos);
} else {
nanoString = '' + (LocalTime.NANOS_PER_SECOND + this._nanos);
}
nanoString = nanoString.slice(1, nanoString.length);
rval += nanoString;
while (rval.charAt(rval.length - 1) === '0') {
rval = rval.slice(0, rval.length - 1);
}
}
rval += 'S';
return rval;
};
_proto.toJSON = function toJSON() {
return this.toString();
};
return Duration;
}(TemporalAmount);
function _init() {
Duration.ZERO = new Duration(0, 0);
}
/*
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @license BSD-3-Clause (see LICENSE.md in the root directory of this source tree)
*/
var YearConstants = function YearConstants() {};