@@ -161,7 +161,7 @@ private fun Instant.toZonedDateTimeFailing(zone: TimeZone): ZonedDateTime = try
161161 */
162162private fun Instant.toZonedDateTime (zone : TimeZone ): ZonedDateTime {
163163 val currentOffset = zone.offsetAt(this )
164- return ZonedDateTime (toLocalDateTimeImpl(currentOffset), zone, currentOffset)
164+ return ZonedDateTime (toLocalDateTimeImpl(currentOffset), currentOffset)
165165}
166166
167167/* * Check that [Instant] fits in [ZonedDateTime].
@@ -174,8 +174,8 @@ private fun Instant.check(zone: TimeZone): Instant = this@check.also {
174174public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
175175 with (period) {
176176 val withDate = toZonedDateTimeFailing(timeZone)
177- .run { if (totalMonths != 0 ) plus(totalMonths, DateTimeUnit .MONTH ) else this }
178- .run { if (days != 0 ) plus(days, DateTimeUnit .DAY ) else this }
177+ .run { if (totalMonths != 0 ) timeZone.atZone(dateTime. plus(totalMonths, DateTimeUnit .MONTH ), offset ) else this }
178+ .run { if (days != 0 ) timeZone.atZone(dateTime. plus(days, DateTimeUnit .DAY ), offset ) else this }
179179 withDate.toInstant()
180180 .run { if (totalNanoseconds != 0L ) plus(0 , totalNanoseconds).check(timeZone) else this }
181181 }.check(timeZone)
@@ -197,7 +197,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
197197 is DateTimeUnit .DateBased -> {
198198 if (value < Int .MIN_VALUE || value > Int .MAX_VALUE )
199199 throw ArithmeticException (" Can't add a Long date-based value, as it would cause an overflow" )
200- toZonedDateTimeFailing(timeZone).plus(value.toInt(), unit).toInstant()
200+ val toZonedDateTimeFailing = toZonedDateTimeFailing(timeZone)
201+ timeZone.atZone(
202+ toZonedDateTimeFailing.dateTime.plus(value.toInt(), unit),
203+ toZonedDateTimeFailing.offset
204+ ).toInstant()
201205 }
202206 is DateTimeUnit .TimeBased ->
203207 check(timeZone).plus(value, unit).check(timeZone)
@@ -223,11 +227,20 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
223227 var thisLdt = toZonedDateTimeFailing(timeZone)
224228 val otherLdt = other.toZonedDateTimeFailing(timeZone)
225229
226- val months = thisLdt.until(otherLdt, DateTimeUnit .MONTH ).toInt() // `until` on dates never fails
227- thisLdt = thisLdt.plus(months, DateTimeUnit .MONTH ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
228- val days = thisLdt.until(otherLdt, DateTimeUnit .DAY ).toInt() // `until` on dates never fails
229- thisLdt = thisLdt.plus(days, DateTimeUnit .DAY ) // won't throw: thisLdt + days <= otherLdt
230- val nanoseconds = thisLdt.until(otherLdt, DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
230+ val months =
231+ thisLdt.dateTime.until(otherLdt.dateTime, DateTimeUnit .MONTH ).toLong().toInt() // `until` on dates never fails
232+ thisLdt = timeZone.atZone(
233+ thisLdt.dateTime.plus(months, DateTimeUnit .MONTH ),
234+ thisLdt.offset
235+ ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
236+ val days =
237+ thisLdt.dateTime.until(otherLdt.dateTime, DateTimeUnit .DAY ).toLong().toInt() // `until` on dates never fails
238+ thisLdt = timeZone.atZone(
239+ thisLdt.dateTime.plus(days, DateTimeUnit .DAY ),
240+ thisLdt.offset
241+ ) // won't throw: thisLdt + days <= otherLdt
242+ val nanoseconds =
243+ thisLdt.toInstant().until(otherLdt.toInstant(), DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
231244
232245 return buildDateTimePeriod(months, days, nanoseconds)
233246}
0 commit comments