@@ -234,22 +234,30 @@ public actual class Instant internal constructor(public actual val epochSeconds:
234
234
235
235
}
236
236
237
- private fun Instant.toZonedLocalDateTimeFailing (zone : TimeZone ): ZonedDateTime = try {
238
- toZonedLocalDateTime (zone)
237
+ private fun Instant.toZonedDateTimeFailing (zone : TimeZone ): ZonedDateTime = try {
238
+ toZonedDateTime (zone)
239
239
} catch (e: IllegalArgumentException ) {
240
240
throw DateTimeArithmeticException (" Can not convert instant $this to LocalDateTime to perform computations" , e)
241
241
}
242
242
243
+ /* *
244
+ * @throws IllegalArgumentException if the [Instant] exceeds the boundaries of [LocalDateTime]
245
+ */
246
+ private fun Instant.toZonedDateTime (zone : TimeZone ): ZonedDateTime {
247
+ val currentOffset = zone.offsetAt(this )
248
+ return ZonedDateTime (toLocalDateTimeImpl(currentOffset), zone, currentOffset)
249
+ }
250
+
243
251
/* * Check that [Instant] fits in [ZonedDateTime].
244
252
* This is done on the results of computations for consistency with other platforms.
245
253
*/
246
254
private fun Instant.check (zone : TimeZone ): Instant = this @check.also {
247
- toZonedLocalDateTimeFailing (zone)
255
+ toZonedDateTimeFailing (zone)
248
256
}
249
257
250
258
public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
251
259
with (period) {
252
- val withDate = toZonedLocalDateTimeFailing (timeZone)
260
+ val withDate = toZonedDateTimeFailing (timeZone)
253
261
.run { if (totalMonths != 0 ) plus(totalMonths, DateTimeUnit .MONTH ) else this }
254
262
.run { if (days != 0 ) plus(days, DateTimeUnit .DAY ) else this }
255
263
withDate.toInstant()
@@ -272,7 +280,7 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
272
280
is DateTimeUnit .DateBased -> {
273
281
if (value < Int .MIN_VALUE || value > Int .MAX_VALUE )
274
282
throw ArithmeticException (" Can't add a Long date-based value, as it would cause an overflow" )
275
- toZonedLocalDateTimeFailing (timeZone).plus(value.toInt(), unit).toInstant()
283
+ toZonedDateTimeFailing (timeZone).plus(value.toInt(), unit).toInstant()
276
284
}
277
285
is DateTimeUnit .TimeBased ->
278
286
check(timeZone).plus(value, unit).check(timeZone)
@@ -296,8 +304,8 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
296
304
297
305
@OptIn(ExperimentalTime ::class )
298
306
public actual fun Instant.periodUntil (other : Instant , timeZone : TimeZone ): DateTimePeriod {
299
- var thisLdt = toZonedLocalDateTimeFailing (timeZone)
300
- val otherLdt = other.toZonedLocalDateTimeFailing (timeZone)
307
+ var thisLdt = toZonedDateTimeFailing (timeZone)
308
+ val otherLdt = other.toZonedDateTimeFailing (timeZone)
301
309
302
310
val months = thisLdt.until(otherLdt, DateTimeUnit .MONTH ).toInt() // `until` on dates never fails
303
311
thisLdt = thisLdt.plus(months, DateTimeUnit .MONTH ) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
@@ -311,7 +319,7 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
311
319
public actual fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long =
312
320
when (unit) {
313
321
is DateTimeUnit .DateBased ->
314
- toZonedLocalDateTimeFailing (timeZone).dateTime.until(other.toZonedLocalDateTimeFailing (timeZone).dateTime, unit)
322
+ toZonedDateTimeFailing (timeZone).dateTime.until(other.toZonedDateTimeFailing (timeZone).dateTime, unit)
315
323
.toLong()
316
324
is DateTimeUnit .TimeBased -> {
317
325
check(timeZone); other.check(timeZone)
0 commit comments