@@ -234,22 +234,30 @@ public actual class Instant internal constructor(public actual val epochSeconds:
234234
235235}
236236
237- private fun Instant.toZonedLocalDateTimeFailing (zone : TimeZone ): ZonedDateTime = try {
238- toZonedLocalDateTime (zone)
237+ private fun Instant.toZonedDateTimeFailing (zone : TimeZone ): ZonedDateTime = try {
238+ toZonedDateTime (zone)
239239} catch (e: IllegalArgumentException ) {
240240 throw DateTimeArithmeticException (" Can not convert instant $this to LocalDateTime to perform computations" , e)
241241}
242242
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+
243251/* * Check that [Instant] fits in [ZonedDateTime].
244252 * This is done on the results of computations for consistency with other platforms.
245253 */
246254private fun Instant.check (zone : TimeZone ): Instant = this @check.also {
247- toZonedLocalDateTimeFailing (zone)
255+ toZonedDateTimeFailing (zone)
248256}
249257
250258public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
251259 with (period) {
252- val withDate = toZonedLocalDateTimeFailing (timeZone)
260+ val withDate = toZonedDateTimeFailing (timeZone)
253261 .run { if (totalMonths != 0 ) plus(totalMonths, DateTimeUnit .MONTH ) else this }
254262 .run { if (days != 0 ) plus(days, DateTimeUnit .DAY ) else this }
255263 withDate.toInstant()
@@ -272,7 +280,7 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
272280 is DateTimeUnit .DateBased -> {
273281 if (value < Int .MIN_VALUE || value > Int .MAX_VALUE )
274282 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()
276284 }
277285 is DateTimeUnit .TimeBased ->
278286 check(timeZone).plus(value, unit).check(timeZone)
@@ -296,8 +304,8 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
296304
297305@OptIn(ExperimentalTime ::class )
298306public 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)
301309
302310 val months = thisLdt.until(otherLdt, DateTimeUnit .MONTH ).toInt() // `until` on dates never fails
303311 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
311319public actual fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long =
312320 when (unit) {
313321 is DateTimeUnit .DateBased ->
314- toZonedLocalDateTimeFailing (timeZone).dateTime.until(other.toZonedLocalDateTimeFailing (timeZone).dateTime, unit)
322+ toZonedDateTimeFailing (timeZone).dateTime.until(other.toZonedDateTimeFailing (timeZone).dateTime, unit)
315323 .toLong()
316324 is DateTimeUnit .TimeBased -> {
317325 check(timeZone); other.check(timeZone)
0 commit comments