diff --git a/core/common/test/InstantTest.kt b/core/common/test/InstantTest.kt index 4c9814a6..8eb90359 100644 --- a/core/common/test/InstantTest.kt +++ b/core/common/test/InstantTest.kt @@ -609,4 +609,13 @@ class InstantRangeTest { (maxValidInstant + 1.nanoseconds).until(maxValidInstant, DateTimeUnit.NANOSECOND) maxValidInstant.until(maxValidInstant + 1.nanoseconds, DateTimeUnit.NANOSECOND) } + + // https://github.com/Kotlin/kotlinx-datetime/issues/263 + @Test + fun addSmallDurationsToLargeInstants() { + for (smallDuration in listOf(1.nanoseconds, 999_999.nanoseconds, 1.seconds - 1.nanoseconds)) { + assertEquals(expected = Instant.MAX, actual = Instant.MAX + smallDuration) + assertEquals(expected = Instant.MIN, actual = Instant.MIN - smallDuration) + } + } } diff --git a/core/js/src/Instant.kt b/core/js/src/Instant.kt index 35faaab9..e754a674 100644 --- a/core/js/src/Instant.kt +++ b/core/js/src/Instant.kt @@ -35,7 +35,7 @@ public actual class Instant internal constructor(internal val value: jtInstant) Instant(plusFix(seconds.toDouble(), nanoseconds)) } catch (e: Throwable) { if (!e.isJodaDateTimeException()) throw e - if (seconds > 0) MAX else MIN + if (duration.isPositive()) MAX else MIN } } diff --git a/core/native/src/Instant.kt b/core/native/src/Instant.kt index bada48cb..cfc97c96 100644 --- a/core/native/src/Instant.kt +++ b/core/native/src/Instant.kt @@ -159,9 +159,9 @@ public actual class Instant internal constructor(public actual val epochSeconds: try { plus(secondsToAdd, nanosecondsToAdd.toLong()) } catch (e: IllegalArgumentException) { - if (secondsToAdd > 0) MAX else MIN + if (duration.isPositive()) MAX else MIN } catch (e: ArithmeticException) { - if (secondsToAdd > 0) MAX else MIN + if (duration.isPositive()) MAX else MIN } }