Skip to content

Commit 1404222

Browse files
committed
Common: add Instant.fromEpochSeconds(Long, Int)
This overload allows to pass an Int as the number of nanoseconds.
1 parent 5dcb6e2 commit 1404222

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

core/commonMain/src/Instant.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public expect class Instant : Comparable<Instant> {
111111
*/
112112
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long = 0): Instant
113113

114+
/**
115+
* Returns an [Instant] that is the [epochSeconds] number of seconds from the epoch instant `1970-01-01T00:00:00Z`
116+
* and the [nanosecondAdjustment] number of nanoseconds from the whole second.
117+
*
118+
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
119+
*/
120+
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant
121+
114122
/**
115123
* Parses a string that represents an instant in ISO-8601 format including date and time components and
116124
* the mandatory `Z` designator of the UTC+0 time zone and returns the parsed [Instant] value.

core/commonTest/src/InstantTest.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,22 +264,34 @@ class InstantTest {
264264
@Test
265265
fun nanosecondAdjustment() {
266266
for (i in -2..2L) {
267-
for (j in 0..9L) {
267+
for (j in 0..9) {
268268
val t: Instant = Instant.fromEpochSeconds(i, j)
269+
val t2: Instant = Instant.fromEpochSeconds(i, j.toLong())
269270
assertEquals(i, t.epochSeconds)
270-
assertEquals(j, t.nanosecondsOfSecond.toLong())
271+
assertEquals(j, t.nanosecondsOfSecond)
272+
assertEquals(t, t2)
271273
}
272-
for (j in -10..-1L) {
274+
for (j in -10..-1) {
273275
val t: Instant = Instant.fromEpochSeconds(i, j)
276+
val t2: Instant = Instant.fromEpochSeconds(i, j.toLong())
274277
assertEquals(i - 1, t.epochSeconds)
275-
assertEquals(j + 1000000000, t.nanosecondsOfSecond.toLong())
278+
assertEquals(j + 1000000000, t.nanosecondsOfSecond)
279+
assertEquals(t, t2)
276280
}
277-
for (j in 999_999_990..999_999_999L) {
281+
for (j in 999_999_990..999_999_999) {
278282
val t: Instant = Instant.fromEpochSeconds(i, j)
283+
val t2: Instant = Instant.fromEpochSeconds(i, j.toLong())
279284
assertEquals(i, t.epochSeconds)
280-
assertEquals(j, t.nanosecondsOfSecond.toLong())
285+
assertEquals(j, t.nanosecondsOfSecond)
286+
assertEquals(t, t2)
281287
}
282288
}
289+
val t = Instant.fromEpochSeconds(0, Int.MAX_VALUE)
290+
assertEquals((Int.MAX_VALUE / 1_000_000_000).toLong(), t.epochSeconds)
291+
assertEquals(Int.MAX_VALUE % 1_000_000_000, t.nanosecondsOfSecond)
292+
val t2 = Instant.fromEpochSeconds(0, Long.MAX_VALUE)
293+
assertEquals(Long.MAX_VALUE / 1_000_000_000, t2.epochSeconds)
294+
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(), t2.nanosecondsOfSecond)
283295
}
284296

285297
/* Based on the ThreeTenBp project.

core/jsMain/src/Instant.kt

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

89+
actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant = try {
90+
Instant(jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
91+
} catch (e: Throwable) {
92+
if (!e.isJodaDateTimeException()) throw e
93+
if (epochSeconds > 0) MAX else MIN
94+
}
95+
8996
actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS, 999_999_999))
9097
actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS, 0))
9198

core/jvmMain/src/Instant.kt

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

75+
actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant =
76+
fromEpochSeconds(epochSeconds, nanosecondAdjustment.toLong())
77+
7578
actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS, 999_999_999))
7679
actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS, 0))
7780

core/nativeMain/src/Instant.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ public actual class Instant internal constructor(actual val epochSeconds: Long,
250250
if (epochSeconds > 0) MAX else MIN
251251
}
252252

253+
actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant =
254+
fromEpochSeconds(epochSeconds, nanosecondAdjustment.toLong())
255+
253256
actual fun parse(isoString: String): Instant =
254257
instantParser.parse(isoString)
255258

0 commit comments

Comments
 (0)