Repository navigation
Expand file tree
/
Copy pathDuration.java
More file actions
732 lines (663 loc) · 26.3 KB
/
Copy pathDuration.java
File metadata and controls
732 lines (663 loc) · 26.3 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
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package java.time;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/// An amount of time in seconds and nanoseconds, such as 34.5 seconds.
///
/// Arithmetic is exact: a result longer than a `Duration` holds throws
/// `ArithmeticException` rather than wrapping around, as the JDK's does.
public final class Duration implements TemporalAmount, Comparable<Duration> {
/// A duration of zero.
public static final Duration ZERO = new Duration(0L, 0);
private static final long NANOS_PER_SECOND = TimeArithmetic.NANOS_PER_SECOND;
private final long seconds;
private final int nanos;
private Duration(long seconds, int nanos) {
this.seconds = seconds;
this.nanos = nanos;
}
private static Duration create(long seconds, int nanos) {
if ((seconds | nanos) == 0) {
return ZERO;
}
return new Duration(seconds, nanos);
}
/// A duration of `days` standard 24 hour days.
public static Duration ofDays(long days) {
return create(TimeArithmetic.multiplyExact(days, 86400L), 0);
}
/// A duration of `hours` hours.
public static Duration ofHours(long hours) {
return create(TimeArithmetic.multiplyExact(hours, 3600L), 0);
}
/// A duration of `minutes` minutes.
public static Duration ofMinutes(long minutes) {
return create(TimeArithmetic.multiplyExact(minutes, 60L), 0);
}
/// A duration of `seconds` seconds.
public static Duration ofSeconds(long seconds) {
return create(seconds, 0);
}
/// A duration of `seconds` seconds adjusted by `nanoAdjustment`
/// nanoseconds, which may take it across any number of seconds.
public static Duration ofSeconds(long seconds, long nanoAdjustment) {
long secs = TimeArithmetic.addExact(seconds, TimeArithmetic.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
int nos = (int) TimeArithmetic.floorMod(nanoAdjustment, NANOS_PER_SECOND);
return create(secs, nos);
}
/// A duration of `millis` milliseconds.
public static Duration ofMillis(long millis) {
long secs = millis / 1000;
int mos = (int) (millis % 1000);
if (mos < 0) {
mos += 1000;
secs--;
}
return create(secs, mos * 1000000);
}
/// A duration of `nanos` nanoseconds.
public static Duration ofNanos(long nanos) {
long secs = nanos / NANOS_PER_SECOND;
int nos = (int) (nanos % NANOS_PER_SECOND);
if (nos < 0) {
nos += (int) NANOS_PER_SECOND;
secs--;
}
return create(secs, nos);
}
/// A duration of `amount` of `unit`. The unit must have an exact length:
/// a day or anything shorter.
public static Duration of(long amount, TemporalUnit unit) {
return ZERO.plus(amount, unit);
}
/// The duration `amount` adds up to. Every unit it is made of must have
/// an exact length.
public static Duration from(TemporalAmount amount) {
if (amount == null) {
throw new NullPointerException("amount");
}
Duration duration = ZERO;
List<TemporalUnit> units = amount.getUnits();
for (int i = 0; i < units.size(); i++) {
TemporalUnit unit = units.get(i);
duration = duration.plus(amount.get(unit), unit);
}
return duration;
}
/// The duration from one value to another of the same kind, negative
/// when `endExclusive` comes first.
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
try {
return ofNanos(startInclusive.until(endExclusive, ChronoUnit.NANOS));
} catch (DateTimeException ex) {
return betweenInSeconds(startInclusive, endExclusive);
} catch (ArithmeticException ex) {
return betweenInSeconds(startInclusive, endExclusive);
}
}
private static Duration betweenInSeconds(Temporal startInclusive, Temporal endExclusive) {
long secs = startInclusive.until(endExclusive, ChronoUnit.SECONDS);
long nanoDiff = TimeArithmetic.nanoOfSecond(endExclusive) - TimeArithmetic.nanoOfSecond(startInclusive);
if (secs > 0 && nanoDiff < 0) {
secs++;
} else if (secs < 0 && nanoDiff > 0) {
secs--;
}
return ofSeconds(secs, nanoDiff);
}
public static Duration parse(CharSequence text) {
if (text == null) {
throw new NullPointerException();
}
String value = text.toString();
int length = value.length();
int position = 0;
int overallSign = 1;
if (position < length && (value.charAt(position) == '+' || value.charAt(position) == '-')) {
if (value.charAt(position++) == '-') {
overallSign = -1;
}
}
if (position >= length) {
throw invalidDuration(value);
}
char prefix = value.charAt(position++);
if (prefix != 'P' && prefix != 'p') {
throw invalidDuration(value);
}
boolean time = false;
boolean found = false;
boolean foundTime = false;
boolean foundDays = false;
int lastTimeUnit = 0;
long seconds = 0L;
int nanos = 0;
while (position < length) {
if (value.charAt(position) == 'T' || value.charAt(position) == 't') {
if (time) {
throw invalidDuration(value);
}
time = true;
position++;
continue;
}
int numberStart = position;
if (value.charAt(position) == '+' || value.charAt(position) == '-') {
position++;
}
int digitStart = position;
while (position < length && Character.isDigit(value.charAt(position))) {
position++;
}
boolean hasWholeDigits = position > digitStart;
int fractionStart = -1;
if (position < length && (value.charAt(position) == '.' || value.charAt(position) == ',')) {
fractionStart = ++position;
while (position < length && Character.isDigit(value.charAt(position))) {
position++;
}
}
if (!hasWholeDigits || position >= length) {
throw invalidDuration(value);
}
char unit = value.charAt(position++);
if (unit >= 'a' && unit <= 'z') {
unit = (char) (unit - ('a' - 'A'));
}
String wholeText = value.substring(numberStart, fractionStart < 0 ? position - 1 : fractionStart - 1);
long amount;
try {
amount = Long.parseLong(wholeText);
} catch (NumberFormatException ex) {
throw invalidDuration(value);
}
if (unit == 'D' && !time && !foundDays && fractionStart < 0) {
seconds = parsedSum(value, seconds, amount, 86400L);
foundDays = true;
} else if (unit == 'H' && time && lastTimeUnit < 1 && fractionStart < 0) {
seconds = parsedSum(value, seconds, amount, 3600L);
lastTimeUnit = 1;
foundTime = true;
} else if (unit == 'M' && time && lastTimeUnit < 2 && fractionStart < 0) {
seconds = parsedSum(value, seconds, amount, 60L);
lastTimeUnit = 2;
foundTime = true;
} else if (unit == 'S' && time && lastTimeUnit < 3) {
seconds = parsedSum(value, seconds, amount, 1L);
lastTimeUnit = 3;
foundTime = true;
if (fractionStart >= 0) {
String fraction = value.substring(fractionStart, position - 1);
if (fraction.length() == 0 || fraction.length() > 9) {
throw invalidDuration(value);
}
while (fraction.length() < 9) {
fraction += "0";
}
nanos = Integer.parseInt(fraction);
if (amount < 0 || wholeText.startsWith("-")) {
nanos = -nanos;
}
}
} else {
throw invalidDuration(value);
}
found = true;
}
if (!found || (time && !foundTime)) {
throw invalidDuration(value);
}
try {
return ofSeconds(TimeArithmetic.multiplyExact(overallSign, seconds), overallSign * (long) nanos);
} catch (ArithmeticException e) {
throw invalidDuration(value);
}
}
/// `seconds` plus `amount` units of `unitSeconds` each. A field too large
/// for a duration is refused as the JDK refuses it, rather than wrapping
/// around to an unrelated one.
private static long parsedSum(String value, long seconds, long amount, long unitSeconds) {
try {
return TimeArithmetic.addExact(seconds, TimeArithmetic.multiplyExact(amount, unitSeconds));
} catch (ArithmeticException e) {
throw invalidDuration(value);
}
}
private static DateTimeParseException invalidDuration(String value) {
return new DateTimeParseException("Text cannot be parsed to a Duration", value, 0);
}
/// The amount of `unit` held: the seconds or the nanoseconds.
@Override
public long get(TemporalUnit unit) {
if (unit == ChronoUnit.SECONDS) {
return seconds;
}
if (unit == ChronoUnit.NANOS) {
return nanos;
}
throw TimeArithmetic.unsupported(unit);
}
/// Seconds and nanoseconds, the two units a duration is made of.
@Override
public List<TemporalUnit> getUnits() {
List<TemporalUnit> units = new ArrayList<TemporalUnit>(2);
units.add(ChronoUnit.SECONDS);
units.add(ChronoUnit.NANOS);
return Collections.unmodifiableList(units);
}
/// A zero duration hands `temporal` back untouched, null included:
/// JDK 8, the conformance host, does the same and throws nothing.
@Override
public Temporal addTo(Temporal temporal) {
if (seconds != 0) {
temporal = temporal.plus(seconds, ChronoUnit.SECONDS);
}
if (nanos != 0) {
temporal = temporal.plus(nanos, ChronoUnit.NANOS);
}
return temporal;
}
@Override
public Temporal subtractFrom(Temporal temporal) {
if (seconds != 0) {
temporal = temporal.minus(seconds, ChronoUnit.SECONDS);
}
if (nanos != 0) {
temporal = temporal.minus(nanos, ChronoUnit.NANOS);
}
return temporal;
}
/// Whether this is zero long.
public boolean isZero() {
return (seconds | nanos) == 0;
}
/// Whether this is shorter than zero.
public boolean isNegative() {
return seconds < 0;
}
/// The whole seconds, rounded towards negative infinity.
public long getSeconds() {
return seconds;
}
/// The nanoseconds within the second, 0 to 999,999,999.
public int getNano() {
return nanos;
}
/// This duration with `seconds` whole seconds.
public Duration withSeconds(long seconds) {
return create(seconds, nanos);
}
/// This duration with `nanoOfSecond` nanoseconds within the second.
public Duration withNanos(int nanoOfSecond) {
if (nanoOfSecond < 0 || nanoOfSecond >= NANOS_PER_SECOND) {
throw new DateTimeException("Invalid value for NanoOfSecond: " + nanoOfSecond);
}
return create(seconds, nanoOfSecond);
}
public Duration plus(Duration duration) {
return plus(duration.getSeconds(), duration.getNano());
}
/// This duration plus `amountToAdd` of `unit`, which must have an exact
/// length.
public Duration plus(long amountToAdd, TemporalUnit unit) {
if (unit == null) {
throw new NullPointerException("unit");
}
if (unit == ChronoUnit.DAYS) {
return plus(TimeArithmetic.multiplyExact(amountToAdd, 86400L), 0);
}
if (unit.isDurationEstimated()) {
throw new java.time.temporal.UnsupportedTemporalTypeException(
"Unit must not have an estimated duration");
}
if (amountToAdd == 0) {
return this;
}
if (unit instanceof ChronoUnit) {
ChronoUnit u = (ChronoUnit) unit;
if (u == ChronoUnit.NANOS) {
return plusNanos(amountToAdd);
}
if (u == ChronoUnit.MICROS) {
return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000)
.plusNanos((amountToAdd % (1000000L * 1000)) * 1000);
}
if (u == ChronoUnit.MILLIS) {
return plusMillis(amountToAdd);
}
if (u == ChronoUnit.SECONDS) {
return plusSeconds(amountToAdd);
}
return plusSeconds(TimeArithmetic.multiplyExact(TimeArithmetic.seconds(u), amountToAdd));
}
Duration duration = unit.getDuration().multipliedBy(amountToAdd);
return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}
public Duration plusDays(long daysToAdd) {
return plus(TimeArithmetic.multiplyExact(daysToAdd, 86400L), 0);
}
public Duration plusHours(long hoursToAdd) {
return plus(TimeArithmetic.multiplyExact(hoursToAdd, 3600L), 0);
}
public Duration plusMinutes(long minutesToAdd) {
return plus(TimeArithmetic.multiplyExact(minutesToAdd, 60L), 0);
}
public Duration plusSeconds(long secondsToAdd) {
return plus(secondsToAdd, 0);
}
public Duration plusMillis(long millisToAdd) {
return plus(millisToAdd / 1000, (millisToAdd % 1000) * 1000000);
}
public Duration plusNanos(long nanosToAdd) {
return plus(0, nanosToAdd);
}
private Duration plus(long secondsToAdd, long nanosToAdd) {
if ((secondsToAdd | nanosToAdd) == 0) {
return this;
}
long epochSec = TimeArithmetic.addExact(seconds, secondsToAdd);
epochSec = TimeArithmetic.addExact(epochSec, nanosToAdd / NANOS_PER_SECOND);
nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
long nanoAdjustment = nanos + nanosToAdd;
return ofSeconds(epochSec, nanoAdjustment);
}
public Duration minus(Duration duration) {
long secsToSubtract = duration.getSeconds();
int nanosToSubtract = duration.getNano();
if (secsToSubtract == Long.MIN_VALUE) {
return plus(Long.MAX_VALUE, -nanosToSubtract).plus(1, 0);
}
return plus(-secsToSubtract, -nanosToSubtract);
}
public Duration minus(long amountToSubtract, TemporalUnit unit) {
return amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit)
: plus(-amountToSubtract, unit);
}
public Duration minusDays(long daysToSubtract) {
return daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract);
}
public Duration minusHours(long hoursToSubtract) {
return hoursToSubtract == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1)
: plusHours(-hoursToSubtract);
}
public Duration minusMinutes(long minutesToSubtract) {
return minutesToSubtract == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1)
: plusMinutes(-minutesToSubtract);
}
public Duration minusSeconds(long secondsToSubtract) {
return secondsToSubtract == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1)
: plusSeconds(-secondsToSubtract);
}
public Duration minusMillis(long millisToSubtract) {
return millisToSubtract == Long.MIN_VALUE ? plusMillis(Long.MAX_VALUE).plusMillis(1)
: plusMillis(-millisToSubtract);
}
public Duration minusNanos(long nanosToSubtract) {
return nanosToSubtract == Long.MIN_VALUE ? plusNanos(Long.MAX_VALUE).plusNanos(1)
: plusNanos(-nanosToSubtract);
}
/// This duration multiplied by `multiplicand`, or `ArithmeticException`
/// when the product is longer than a duration holds.
public Duration multipliedBy(long multiplicand) {
if (multiplicand == 1) {
return this;
}
// A negative duration borrows a second first, so its seconds and
// nanoseconds share a sign and neither partial product overflows while
// the whole fits; the factor is split at a billion so the nanosecond
// product fits a long.
long secs = seconds;
long nos = nanos;
if (secs < 0 && nos > 0) {
secs++;
nos -= NANOS_PER_SECOND;
}
long high = TimeArithmetic.floorDiv(multiplicand, NANOS_PER_SECOND);
long rest = nos * TimeArithmetic.floorMod(multiplicand, NANOS_PER_SECOND);
// The nanosecond terms are summed first: together they are the carry
// of nanos * factor, which is never larger than the factor, while the
// first alone can push a result that fits past the range.
long carry = TimeArithmetic.addExact(nos * high, TimeArithmetic.floorDiv(rest, NANOS_PER_SECOND));
long total = TimeArithmetic.addExact(TimeArithmetic.multiplyExact(secs, multiplicand), carry);
return ofSeconds(total, TimeArithmetic.floorMod(rest, NANOS_PER_SECOND));
}
/// This duration divided by `divisor`, the fraction of a nanosecond
/// dropped towards zero. Exact for every duration; only a quotient
/// that is not a duration throws `ArithmeticException`.
public Duration dividedBy(long divisor) {
if (divisor == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
if (divisor == 1) {
return this;
}
if (seconds > -9000000000L && seconds < 9000000000L) {
return ofNanos(toNanos() / divisor);
}
// Past 292 years the nanosecond count no longer fits a long, so the
// magnitude is divided as an unsigned 128-bit number instead.
boolean negative = seconds < 0;
long magSeconds = negative ? -seconds - (nanos > 0 ? 1 : 0) : seconds;
long magNanos = negative && nanos > 0 ? NANOS_PER_SECOND - nanos : nanos;
long[] value = mulAdd(magSeconds, NANOS_PER_SECOND, magNanos);
long d = divisor < 0 ? -divisor : divisor;
divideUnsigned(value, d);
long rest = divideUnsigned(value, NANOS_PER_SECOND);
boolean resultNegative = negative != divisor < 0;
if (value[0] != 0 || value[1] < 0) {
if (resultNegative && value[0] == 0 && value[1] == Long.MIN_VALUE && rest == 0) {
return ofSeconds(Long.MIN_VALUE, 0);
}
throw new ArithmeticException("Exceeds capacity of Duration");
}
return resultNegative ? ofSeconds(-value[1], -rest) : ofSeconds(value[1], rest);
}
/// `a * b + c` as an unsigned 128-bit {high, low}; `a` is unsigned, `b`
/// and `c` are below 2 to the 32.
private static long[] mulAdd(long a, long b, long c) {
long lowPart = (a & 0xFFFFFFFFL) * b;
long highPart = (a >>> 32) * b;
long hi = highPart >>> 32;
long lo = highPart << 32;
long sum = lo + lowPart;
if (Long.compare(sum ^ Long.MIN_VALUE, lo ^ Long.MIN_VALUE) < 0) {
hi++;
}
long total = sum + c;
if (Long.compare(total ^ Long.MIN_VALUE, sum ^ Long.MIN_VALUE) < 0) {
hi++;
}
return new long[] {hi, total};
}
/// Divides the unsigned 128-bit {high, low} `value` in place by the
/// unsigned `divisor` (at most 2 to the 63) and answers the remainder.
private static long divideUnsigned(long[] value, long divisor) {
long hi = value[0];
long lo = value[1];
long qHi = 0;
long qLo = 0;
long rem = 0;
for (int i = 127; i >= 0; i--) {
long bit = i >= 64 ? (hi >>> (i - 64)) & 1 : (lo >>> i) & 1;
rem = rem << 1 | bit;
if (Long.compare(rem ^ Long.MIN_VALUE, divisor ^ Long.MIN_VALUE) >= 0) {
rem -= divisor;
if (i >= 64) {
qHi |= 1L << (i - 64);
} else {
qLo |= 1L << i;
}
}
}
value[0] = qHi;
value[1] = qLo;
return rem;
}
/// This duration with its sign reversed. The most negative duration has
/// no positive counterpart and throws `ArithmeticException`.
public Duration negated() {
return multipliedBy(-1);
}
/// This duration without its sign.
public Duration abs() {
return isNegative() ? negated() : this;
}
/// The whole days, of 24 hours.
public long toDays() {
return seconds / 86400L;
}
/// The whole hours.
public long toHours() {
return seconds / 3600L;
}
/// The whole minutes.
public long toMinutes() {
return seconds / 60L;
}
/// The whole seconds; the same as `getSeconds()`.
public long toSeconds() {
return seconds;
}
/// The whole milliseconds, or `ArithmeticException` past what a long
/// holds.
public long toMillis() {
long tempSeconds = seconds;
long tempNanos = nanos;
if (tempSeconds < 0) {
// A negative duration borrows a second first, so the most negative
// value a long holds is reached without an intermediate overflow.
tempSeconds = tempSeconds + 1;
tempNanos = tempNanos - NANOS_PER_SECOND;
}
long millis = TimeArithmetic.multiplyExact(tempSeconds, 1000L);
return TimeArithmetic.addExact(millis, tempNanos / 1000000L);
}
/// The whole nanoseconds, or `ArithmeticException` past what a long
/// holds (about 292 years).
public long toNanos() {
long tempSeconds = seconds;
long tempNanos = nanos;
if (tempSeconds < 0) {
tempSeconds = tempSeconds + 1;
tempNanos = tempNanos - NANOS_PER_SECOND;
}
long totalNanos = TimeArithmetic.multiplyExact(tempSeconds, NANOS_PER_SECOND);
return TimeArithmetic.addExact(totalNanos, tempNanos);
}
/// The whole days; the same as `toDays()`.
public long toDaysPart() {
return seconds / 86400L;
}
/// The hours left over after the whole days.
public int toHoursPart() {
return (int) (toHours() % 24);
}
/// The minutes left over after the whole hours.
public int toMinutesPart() {
return (int) (toMinutes() % 60);
}
/// The seconds left over after the whole minutes.
public int toSecondsPart() {
return (int) (seconds % 60);
}
/// The milliseconds within the second.
public int toMillisPart() {
return nanos / 1000000;
}
/// The nanoseconds within the second; the same as `getNano()`.
public int toNanosPart() {
return nanos;
}
@Override
public int compareTo(Duration other) {
if (seconds != other.seconds) {
return seconds < other.seconds ? -1 : 1;
}
return nanos - other.nanos;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Duration)) {
return false;
}
Duration other = (Duration) obj;
return seconds == other.seconds && nanos == other.nanos;
}
@Override
public int hashCode() {
return ((int) (seconds ^ (seconds >>> 32))) + (51 * nanos);
}
public String toString() {
long effectiveTotalSecs = seconds;
if (seconds < 0L && nanos > 0) {
effectiveTotalSecs++;
}
long hours = effectiveTotalSecs / 3600L;
int minutes = (int) ((effectiveTotalSecs % 3600L) / 60L);
int secs = (int) (effectiveTotalSecs % 60L);
StringBuffer buf = new StringBuffer(24);
buf.append("PT");
if (hours != 0L) {
buf.append(hours).append('H');
}
if (minutes != 0) {
buf.append(minutes).append('M');
}
if (secs == 0 && nanos == 0 && buf.length() > 2) {
return buf.toString();
}
if (seconds < 0L && nanos > 0) {
if (secs == 0) {
buf.append("-0");
} else {
buf.append(secs);
}
} else {
buf.append(secs);
}
if (nanos > 0) {
int pos = buf.length();
if (seconds < 0L) {
buf.append(2000000000L - nanos);
} else {
buf.append(nanos + 1000000000L);
}
while (buf.charAt(buf.length() - 1) == '0') {
buf.setLength(buf.length() - 1);
}
buf.setCharAt(pos, '.');
}
buf.append('S');
return buf.toString();
}
}