-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove kotlinx.datetime from JS target (#316)
Add JS actual implementation
- Loading branch information
1 parent
986142b
commit e117611
Showing
11 changed files
with
221 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,6 @@ | |
# yarn lockfile v1 | ||
|
||
|
||
"@js-joda/[email protected]": | ||
version "3.2.0" | ||
resolved "https://registry.yarnpkg.com/@js-joda/core/-/core-3.2.0.tgz#3e61e21b7b2b8a6be746df1335cf91d70db2a273" | ||
integrity sha512-PMqgJ0sw5B7FKb2d5bWYIoxjri+QlW/Pys7+Rw82jSH0QN3rB05jZ/VrrsUdh1w4+i2kw9JOejXGq/KhDOX7Kg== | ||
|
||
"@tootallnate/quickjs-emscripten@^0.23.0": | ||
version "0.23.0" | ||
resolved "https://registry.yarnpkg.com/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz#db4ecfd499a9765ab24002c3b696d02e6d32a12c" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
pubnub-kotlin/pubnub-kotlin-api/src/commonMain/kotlin/com/pubnub/api/utils/TimetokenUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
pubnub-kotlin/pubnub-kotlin-api/src/commonMain/kotlin/com/pubnub/api/utils/datetime.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.pubnub.api.utils | ||
|
||
import kotlin.time.Duration | ||
|
||
expect class Instant : Comparable<Instant> { | ||
val epochSeconds: Long | ||
val nanosecondsOfSecond: Int | ||
|
||
fun toEpochMilliseconds(): Long | ||
|
||
operator fun plus(duration: Duration): Instant | ||
|
||
operator fun minus(duration: Duration): Instant | ||
|
||
operator fun minus(other: Instant): Duration | ||
|
||
override operator fun compareTo(other: Instant): Int | ||
|
||
companion object { | ||
fun fromEpochMilliseconds(epochMilliseconds: Long): Instant | ||
|
||
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant | ||
} | ||
} | ||
|
||
expect interface Clock { | ||
fun now(): Instant | ||
|
||
object System : Clock { | ||
override fun now(): Instant | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
pubnub-kotlin/pubnub-kotlin-api/src/commonTest/kotlin/com/pubnub/api/util/InstantTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.pubnub.api.util | ||
|
||
import com.pubnub.api.utils.Instant | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class InstantTest { | ||
@Test | ||
fun plusDuration() { | ||
val nowMillis = 1732793616984 | ||
val now = Instant.fromEpochMilliseconds(nowMillis) | ||
val later = now + 1500.milliseconds | ||
|
||
assertEquals(nowMillis + 1500, later.toEpochMilliseconds()) | ||
|
||
val laterWithNanos = now + 2000.nanoseconds | ||
assertEquals(now.epochSeconds, laterWithNanos.epochSeconds) | ||
assertEquals(now.nanosecondsOfSecond + 2000, laterWithNanos.nanosecondsOfSecond) | ||
|
||
val laterWithSecondsAndNanos = now + (1.seconds + 2000.nanoseconds) | ||
assertEquals(now.epochSeconds + 1, laterWithSecondsAndNanos.epochSeconds) | ||
assertEquals(now.nanosecondsOfSecond + 2000, laterWithSecondsAndNanos.nanosecondsOfSecond) | ||
} | ||
|
||
@Test | ||
fun minusDuration() { | ||
val nowMillis = 1732793616984 | ||
val now = Instant.fromEpochMilliseconds(nowMillis) | ||
val later = now - 1500.milliseconds | ||
|
||
assertEquals(nowMillis - 1500, later.toEpochMilliseconds()) | ||
|
||
val laterWithNanos = now - 2000.nanoseconds | ||
assertEquals(now.epochSeconds, laterWithNanos.epochSeconds) | ||
assertEquals(now.nanosecondsOfSecond - 2000, laterWithNanos.nanosecondsOfSecond) | ||
|
||
val laterWithSecondsAndNanos = now - (1.seconds + 2000.nanoseconds) | ||
assertEquals(now.epochSeconds - 1, laterWithSecondsAndNanos.epochSeconds) | ||
assertEquals(now.nanosecondsOfSecond - 2000, laterWithSecondsAndNanos.nanosecondsOfSecond) | ||
} | ||
|
||
@Test | ||
fun minusInstant() { | ||
val nowMillis = 1732793616984 | ||
val laterMillis = 1732793616984 + 1500 | ||
val now = Instant.fromEpochMilliseconds(nowMillis) | ||
val later = Instant.fromEpochMilliseconds(laterMillis) | ||
|
||
assertEquals(1500.milliseconds, later - now) | ||
assertEquals(-1500.milliseconds, now - later) | ||
} | ||
|
||
@Test | ||
fun compareTo() { | ||
val nowMillis = 1732793616984 | ||
val laterMillis = 1732793616984 + 1500 | ||
val now = Instant.fromEpochMilliseconds(nowMillis) | ||
val later = Instant.fromEpochMilliseconds(laterMillis) | ||
|
||
assertTrue(now < later) | ||
assertTrue(later > now) | ||
|
||
assertTrue(now + 100.nanoseconds > now) | ||
assertTrue(now - 100.nanoseconds < now) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
pubnub-kotlin/pubnub-kotlin-api/src/jsMain/kotlin/com/pubnub/api/utils/datetime.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.pubnub.api.utils | ||
|
||
import kotlin.js.Date | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
actual class Instant( | ||
actual val epochSeconds: Long, | ||
actual val nanosecondsOfSecond: Int = 0, | ||
) : Comparable<Instant> { | ||
actual fun toEpochMilliseconds(): Long { | ||
return epochSeconds.seconds.inWholeMilliseconds + nanosecondsOfSecond.nanoseconds.inWholeMilliseconds | ||
} | ||
|
||
actual operator fun plus(duration: Duration): Instant { | ||
val durationWholeSecondsOnly = duration.inWholeSeconds.seconds | ||
val durationNanosOnly = duration - durationWholeSecondsOnly | ||
val sum = add(epochSeconds to nanosecondsOfSecond, duration.inWholeSeconds to durationNanosOnly.inWholeNanoseconds.toInt()) | ||
return Instant(sum.first, sum.second) | ||
} | ||
|
||
actual operator fun minus(duration: Duration): Instant { | ||
return plus(-duration) | ||
} | ||
|
||
actual operator fun minus(other: Instant): Duration { | ||
return epochSeconds.seconds + nanosecondsOfSecond.nanoseconds - other.epochSeconds.seconds - other.nanosecondsOfSecond.nanoseconds | ||
} | ||
|
||
actual override operator fun compareTo(other: Instant): Int { | ||
return epochSeconds.compareTo(other.epochSeconds) | ||
.takeIf { it != 0 } | ||
?: nanosecondsOfSecond.compareTo(other.nanosecondsOfSecond) | ||
} | ||
|
||
private fun add(secondsAndNanos: SecondsAndNanos, secondsAndNanos2: SecondsAndNanos): Pair<Long, Int> { | ||
val nanosSum = secondsAndNanos.nanos + secondsAndNanos2.nanos | ||
val secondsFromNanos = nanosSum.inWholeSeconds.seconds | ||
|
||
val secondsResult = secondsAndNanos.seconds + secondsAndNanos2.seconds + secondsFromNanos | ||
val nanosResult = nanosSum - secondsFromNanos | ||
return secondsResult.inWholeSeconds to nanosResult.inWholeNanoseconds.toInt() | ||
} | ||
|
||
actual companion object { | ||
actual fun fromEpochMilliseconds(epochMilliseconds: Long): Instant { | ||
val wholeSeconds = epochMilliseconds.milliseconds.inWholeSeconds | ||
val nanos = (epochMilliseconds.milliseconds - wholeSeconds.seconds).inWholeNanoseconds | ||
return Instant(wholeSeconds, nanos.toInt()) | ||
} | ||
|
||
actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant { | ||
return Instant(epochSeconds, nanosecondAdjustment) | ||
} | ||
} | ||
} | ||
|
||
actual interface Clock { | ||
actual fun now(): Instant | ||
|
||
actual object System : Clock { | ||
actual override fun now(): Instant { | ||
return Instant.fromEpochMilliseconds(Date.now().toLong()) | ||
} | ||
} | ||
} | ||
|
||
typealias SecondsAndNanos = Pair<Long, Int> | ||
|
||
val SecondsAndNanos.seconds get() = this.first.seconds | ||
val SecondsAndNanos.nanos get() = this.second.nanoseconds |
16 changes: 16 additions & 0 deletions
16
pubnub-kotlin/pubnub-kotlin-api/src/nonJs/kotlin/com/pubnub/api/utils/datetime.nonJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.pubnub.api.utils | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
|
||
actual typealias Instant = Instant | ||
|
||
actual interface Clock { | ||
actual fun now(): com.pubnub.api.utils.Instant | ||
|
||
actual object System : com.pubnub.api.utils.Clock { | ||
actual override fun now(): com.pubnub.api.utils.Instant { | ||
return Clock.System.now() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters