@@ -179,7 +179,7 @@ private fun Instant.toZonedDateTimeFailing(zone: TimeZone): ZonedDateTime = try
179
179
*/
180
180
private fun Instant.toZonedDateTime (zone : TimeZone ): ZonedDateTime {
181
181
val currentOffset = zone.offsetAt(this )
182
- return ZonedDateTime (toLocalDateTimeImpl(currentOffset), zone, currentOffset)
182
+ return ZonedDateTime (toLocalDateTimeImpl(currentOffset), currentOffset)
183
183
}
184
184
185
185
/* * Check that [Instant] fits in [ZonedDateTime].
@@ -192,8 +192,8 @@ private fun Instant.check(zone: TimeZone): Instant =
[email protected] {
192
192
public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
193
193
with (period) {
194
194
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 }
197
197
withDate.toInstant()
198
198
.run { if (totalNanoseconds != 0L ) plus(0 , totalNanoseconds).check(timeZone) else this }
199
199
}.check(timeZone)
@@ -212,8 +212,13 @@ public actual fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZo
212
212
plus(- value.toLong(), unit, timeZone)
213
213
public actual fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant = try {
214
214
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
+ }
217
222
is DateTimeUnit .TimeBased ->
218
223
check(timeZone).plus(value, unit).check(timeZone)
219
224
}
@@ -238,11 +243,19 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
238
243
var thisLdt = toZonedDateTimeFailing(timeZone)
239
244
val otherLdt = other.toZonedDateTimeFailing(timeZone)
240
245
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
246
259
247
260
return buildDateTimePeriod(months, days.toInt(), nanoseconds)
248
261
}
@@ -281,3 +294,9 @@ private val ISO_DATE_TIME_OFFSET_WITH_TRAILING_ZEROS = DateTimeComponents.Format
281
294
outputSecond = WhenToOutput .IF_NONZERO
282
295
)
283
296
}
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