Skip to content

Commit 7f6312b

Browse files
authored
Common: introduce DISTANT_PAST and DISTANT_FUTURE (#19)
1 parent 0fdc611 commit 7f6312b

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

core/commonMain/src/Instant.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.datetime
88
import kotlin.time.Duration
99
import kotlin.time.ExperimentalTime
1010

11-
1211
@OptIn(kotlin.time.ExperimentalTime::class)
1312
public expect class Instant : Comparable<Instant> {
1413

@@ -63,11 +62,23 @@ public expect class Instant : Comparable<Instant> {
6362
*/
6463
fun parse(isoString: String): Instant
6564

65+
// -100001-12-31T23:59:59.999999999Z
66+
val DISTANT_PAST: Instant
67+
68+
// +100000-01-01T00:00:00Z
69+
val DISTANT_FUTURE: Instant
70+
6671
internal val MIN: Instant
6772
internal val MAX: Instant
6873
}
6974
}
7075

76+
public val Instant.isDistantPast
77+
get() = this <= Instant.DISTANT_PAST
78+
79+
public val Instant.isDistantFuture
80+
get() = this >= Instant.DISTANT_FUTURE
81+
7182
/**
7283
* @throws DateTimeFormatException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
7384
*/
@@ -155,3 +166,6 @@ public fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instan
155166

156167

157168
public fun Instant.minus(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = other.until(this, unit, zone)
169+
170+
internal const val DISTANT_PAST_SECONDS = -3217862419201
171+
internal const val DISTANT_FUTURE_SECONDS = 3093527980800

core/commonTest/src/InstantTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,29 @@ class InstantTest {
323323
assertEquals("+19999-12-31T23:59:59.000000009Z", LocalDateTime(19999, 12, 31, 23, 59, 59, 9).toInstant(TimeZone.UTC).toString())
324324
}
325325

326+
@ExperimentalTime
327+
@Test
328+
fun distantPastAndFuture() {
329+
val distantFutureString = "+100000-01-01T00:00:00Z"
330+
val distantPastString = "-100001-12-31T23:59:59.999999999Z"
331+
assertEquals(distantFutureString, Instant.DISTANT_FUTURE.toString())
332+
assertEquals(Instant.DISTANT_FUTURE, distantFutureString.toInstant())
333+
assertEquals(distantPastString, Instant.DISTANT_PAST.toString())
334+
assertEquals(Instant.DISTANT_PAST, distantPastString.toInstant())
335+
assertTrue(Instant.DISTANT_PAST.isDistantPast)
336+
assertTrue(Instant.DISTANT_FUTURE.isDistantFuture)
337+
assertFalse(Instant.DISTANT_PAST.isDistantFuture)
338+
assertFalse(Instant.DISTANT_FUTURE.isDistantPast)
339+
assertFalse((Instant.DISTANT_PAST + 1.nanoseconds).isDistantPast)
340+
assertFalse((Instant.DISTANT_FUTURE - 1.nanoseconds).isDistantFuture)
341+
assertTrue((Instant.DISTANT_PAST - 1.nanoseconds).isDistantPast)
342+
assertTrue((Instant.DISTANT_FUTURE + 1.nanoseconds).isDistantFuture)
343+
assertTrue(Instant.MAX.isDistantFuture)
344+
assertFalse(Instant.MAX.isDistantPast)
345+
assertTrue(Instant.MIN.isDistantPast)
346+
assertFalse(Instant.MIN.isDistantFuture)
347+
}
348+
326349
}
327350

328351
@OptIn(ExperimentalTime::class)

core/jsMain/src/Instant.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public actual class Instant internal constructor(internal val value: jtInstant)
8787
if (epochSeconds > 0) MAX else MIN
8888
}
8989

90+
actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS, 999_999_999))
91+
actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS, 0))
92+
9093
internal actual val MIN: Instant = Instant(jtInstant.MIN)
9194
internal actual val MAX: Instant = Instant(jtInstant.MAX)
9295
}

core/jvmMain/src/Instant.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public actual class Instant internal constructor(internal val value: jtInstant)
7373
if (epochSeconds > 0) MAX else MIN
7474
}
7575

76+
actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS, 999_999_999))
77+
actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS, 0))
78+
7679
internal actual val MIN: Instant = Instant(jtInstant.MIN)
7780
internal actual val MAX: Instant = Instant(jtInstant.MAX)
7881
}

core/nativeMain/src/Instant.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ public actual class Instant internal constructor(actual val epochSeconds: Long,
253253

254254
actual fun parse(isoString: String): Instant =
255255
instantParser.parse(isoString)
256+
257+
@SharedImmutable
258+
actual val DISTANT_PAST: Instant = fromEpochSeconds(DISTANT_PAST_SECONDS, 999_999_999)
259+
260+
@SharedImmutable
261+
actual val DISTANT_FUTURE: Instant = fromEpochSeconds(DISTANT_FUTURE_SECONDS, 0)
256262
}
257263

258264
}

0 commit comments

Comments
 (0)