@@ -268,23 +268,10 @@ private fun Instant.check(zone: TimeZone): Instant =
[email protected] {
268
268
public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
269
269
with (period) {
270
270
val initialOffset = offsetIn(timeZone)
271
- val initialLdt = toLocalDateTimeFailing(initialOffset)
272
- val offsetAfterMonths: UtcOffset
273
- val ldtAfterMonths: LocalDateTime
274
- if (totalMonths != 0 ) {
275
- val (ldt, offset) = timeZone.atZone(initialLdt.plus(totalMonths, DateTimeUnit .MONTH ), initialOffset)
276
- offsetAfterMonths = offset
277
- ldtAfterMonths = ldt
278
- } else {
279
- offsetAfterMonths = initialOffset
280
- ldtAfterMonths = initialLdt
281
- }
282
- val instantAfterMonthsAndDays = if (days != 0 ) {
283
- timeZone.atZone(ldtAfterMonths.plus(days, DateTimeUnit .DAY ), offsetAfterMonths).toInstant()
284
- } else {
285
- ldtAfterMonths.toInstant(offsetAfterMonths)
286
- }
287
- instantAfterMonthsAndDays
271
+ val newLdt = toLocalDateTimeFailing(initialOffset)
272
+ .run { if (totalMonths != 0 ) { plus(totalMonths, DateTimeUnit .MONTH ) } else { this } }
273
+ .run { if (days != 0 ) { plus(days, DateTimeUnit .DAY ) } else { this } }
274
+ timeZone.atZone(newLdt, preferred = initialOffset).toInstant()
288
275
.run { if (totalNanoseconds != 0L ) plus(0 , totalNanoseconds).check(timeZone) else this }
289
276
}.check(timeZone)
290
277
} catch (e: ArithmeticException ) {
@@ -305,9 +292,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
305
292
is DateTimeUnit .DateBased -> {
306
293
if (value < Int .MIN_VALUE || value > Int .MAX_VALUE )
307
294
throw ArithmeticException (" Can't add a Long date-based value, as it would cause an overflow" )
308
- val preferredOffset = offsetIn(timeZone)
309
- val initialLdt = toLocalDateTimeFailing(preferredOffset )
310
- timeZone.atZone(initialLdt.plus(value.toInt(), unit), preferredOffset ).toInstant()
295
+ val initialOffset = offsetIn(timeZone)
296
+ val initialLdt = toLocalDateTimeFailing(initialOffset )
297
+ timeZone.atZone(initialLdt.plus(value.toInt(), unit), preferred = initialOffset ).toInstant()
311
298
}
312
299
is DateTimeUnit .TimeBased ->
313
300
check(timeZone).plus(value, unit).check(timeZone)
@@ -330,15 +317,15 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
330
317
}
331
318
332
319
public actual fun Instant.periodUntil (other : Instant , timeZone : TimeZone ): DateTimePeriod {
333
- val thisOffset1 = offsetIn(timeZone)
334
- val thisLdt1 = toLocalDateTimeFailing(thisOffset1 )
320
+ val initialOffset = offsetIn(timeZone)
321
+ val initialLdt = toLocalDateTimeFailing(initialOffset )
335
322
val otherLdt = other.toLocalDateTimeFailing(other.offsetIn(timeZone))
336
323
337
- val months = thisLdt1 .until(otherLdt, DateTimeUnit .MONTH ).toLong().toInt() // `until` on dates never fails
338
- val (thisLdt2, thisOffset2) = timeZone.atZone(thisLdt1. plus(months, DateTimeUnit .MONTH ), thisOffset1 ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
339
- val days = thisLdt2 .until(otherLdt, DateTimeUnit .DAY ).toLong().toInt() // `until` on dates never fails
340
- val (thisLdt3, thisOffset3) = timeZone.atZone(thisLdt2 .plus(days, DateTimeUnit .DAY ), thisOffset2 ) // won't throw: thisLdt + days <= otherLdt
341
- val nanoseconds = thisLdt3.toInstant(thisOffset3) .until(other, DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
324
+ val months = initialLdt .until(otherLdt, DateTimeUnit .MONTH ).toLong().toInt() // `until` on dates never fails
325
+ val ldtWithMonths = initialLdt. plus(months, DateTimeUnit .MONTH ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
326
+ val days = ldtWithMonths .until(otherLdt, DateTimeUnit .DAY ).toLong().toInt() // `until` on dates never fails
327
+ val newInstant = timeZone.atZone(ldtWithMonths .plus(days, DateTimeUnit .DAY ), preferred = initialOffset).toInstant( ) // won't throw: thisLdt + days <= otherLdt
328
+ val nanoseconds = newInstant .until(other, DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
342
329
343
330
return buildDateTimePeriod(months, days, nanoseconds)
344
331
}
0 commit comments