-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
155 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
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 | ||
|
||
companion object { | ||
val System: Clock | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
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 companion object { | ||
actual val System = object : Clock { | ||
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 |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
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 companion object { | ||
actual val System: com.pubnub.api.utils.Clock = object : com.pubnub.api.utils.Clock { | ||
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