@@ -179,7 +179,7 @@ private fun Instant.toZonedDateTimeFailing(zone: TimeZone): ZonedDateTime = try
179179 */
180180private fun Instant.toZonedDateTime (zone : TimeZone ): ZonedDateTime {
181181 val currentOffset = zone.offsetAt(this )
182- return ZonedDateTime (toLocalDateTimeImpl(currentOffset), zone, currentOffset)
182+ return ZonedDateTime (toLocalDateTimeImpl(currentOffset), currentOffset)
183183}
184184
185185/* * Check that [Instant] fits in [ZonedDateTime].
@@ -192,8 +192,8 @@ private fun Instant.check(zone: TimeZone): Instant = this@check.also {
192192public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
193193 with (period) {
194194 val withDate = toZonedDateTimeFailing(timeZone)
195- .run { if (totalMonths != 0L ) plus(totalMonths, DateTimeUnit .MONTH ) else this }
196- .run { if (days != 0 ) plus(days.toLong() , DateTimeUnit .DAY ) else this }
195+ .run { if (totalMonths != 0L ) timeZone.atZone(dateTime. plus(totalMonths, DateTimeUnit .MONTH ), offset ) else this }
196+ .run { if (days != 0 ) timeZone.atZone(dateTime.plus(days , DateTimeUnit .DAY ), offset ) else this }
197197 withDate.toInstant()
198198 .run { if (totalNanoseconds != 0L ) plus(0 , totalNanoseconds).check(timeZone) else this }
199199 }.check(timeZone)
@@ -212,8 +212,13 @@ public actual fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZo
212212 plus(- value.toLong(), unit, timeZone)
213213public actual fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant = try {
214214 when (unit) {
215- is DateTimeUnit .DateBased ->
216- toZonedDateTimeFailing(timeZone).plus(value, unit).toInstant()
215+ is DateTimeUnit .DateBased -> {
216+ val toZonedDateTimeFailing = toZonedDateTimeFailing(timeZone)
217+ timeZone.atZone(
218+ toZonedDateTimeFailing.dateTime.plus(value, unit),
219+ toZonedDateTimeFailing.offset
220+ ).toInstant()
221+ }
217222 is DateTimeUnit .TimeBased ->
218223 check(timeZone).plus(value, unit).check(timeZone)
219224 }
@@ -238,11 +243,19 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
238243 var thisLdt = toZonedDateTimeFailing(timeZone)
239244 val otherLdt = other.toZonedDateTimeFailing(timeZone)
240245
241- val months = thisLdt.until(otherLdt, DateTimeUnit .MONTH ) // `until` on dates never fails
242- thisLdt = thisLdt.plus(months, DateTimeUnit .MONTH ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
243- val days = thisLdt.until(otherLdt, DateTimeUnit .DAY ) // `until` on dates never fails
244- thisLdt = thisLdt.plus(days, DateTimeUnit .DAY ) // won't throw: thisLdt + days <= otherLdt
245- val nanoseconds = thisLdt.until(otherLdt, DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
246+ val months = thisLdt.dateTime.until(otherLdt.dateTime, DateTimeUnit .MONTH ) // `until` on dates never fails
247+ thisLdt = timeZone.atZone(
248+ thisLdt.dateTime.plus(months, DateTimeUnit .MONTH ),
249+ thisLdt.offset
250+ ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
251+ val days =
252+ thisLdt.dateTime.until(otherLdt.dateTime, DateTimeUnit .DAY ) // `until` on dates never fails
253+ thisLdt = timeZone.atZone(
254+ thisLdt.dateTime.plus(days, DateTimeUnit .DAY ),
255+ thisLdt.offset
256+ ) // won't throw: thisLdt + days <= otherLdt
257+ val nanoseconds =
258+ thisLdt.toInstant().until(otherLdt.toInstant(), DateTimeUnit .NANOSECOND ) // |otherLdt - thisLdt| < 24h
246259
247260 return buildDateTimePeriod(months, days.toInt(), nanoseconds)
248261}
@@ -281,3 +294,9 @@ private val ISO_DATE_TIME_OFFSET_WITH_TRAILING_ZEROS = DateTimeComponents.Format
281294 outputSecond = WhenToOutput .IF_NONZERO
282295 )
283296}
297+
298+ private fun LocalDateTime.plus (value : Long , unit : DateTimeUnit .DateBased ) =
299+ date.plus(value, unit).atTime(time)
300+
301+ private fun LocalDateTime.plus (value : Int , unit : DateTimeUnit .DateBased ) =
302+ date.plus(value, unit).atTime(time)
0 commit comments