@@ -161,7 +161,7 @@ private fun Instant.toZonedDateTimeFailing(zone: TimeZone): ZonedDateTime = try
161
161
*/
162
162
private fun Instant.toZonedDateTime (zone : TimeZone ): ZonedDateTime {
163
163
val currentOffset = zone.offsetAt(this )
164
- return ZonedDateTime (toLocalDateTimeImpl(currentOffset), zone, currentOffset)
164
+ return ZonedDateTime (toLocalDateTimeImpl(currentOffset), currentOffset)
165
165
}
166
166
167
167
/* * Check that [Instant] fits in [ZonedDateTime].
@@ -174,8 +174,8 @@ private fun Instant.check(zone: TimeZone): Instant =
[email protected] {
174
174
public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
175
175
with (period) {
176
176
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 }
179
179
withDate.toInstant()
180
180
.run { if (totalNanoseconds != 0L ) plus(0 , totalNanoseconds).check(timeZone) else this }
181
181
}.check(timeZone)
@@ -197,7 +197,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
197
197
is DateTimeUnit .DateBased -> {
198
198
if (value < Int .MIN_VALUE || value > Int .MAX_VALUE )
199
199
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()
201
205
}
202
206
is DateTimeUnit .TimeBased ->
203
207
check(timeZone).plus(value, unit).check(timeZone)
@@ -223,11 +227,20 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
223
227
var thisLdt = toZonedDateTimeFailing(timeZone)
224
228
val otherLdt = other.toZonedDateTimeFailing(timeZone)
225
229
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
231
244
232
245
return buildDateTimePeriod(months, days, nanoseconds)
233
246
}
0 commit comments