-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add kotlinx-datetime module (#198) * Migrate to MPP * Add api * Fix UUIDv7 --------- Co-authored-by: hfhbd <[email protected]>
- Loading branch information
Showing
11 changed files
with
112 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,11 @@ | |
"@jridgewell/resolve-uri" "3.1.0" | ||
"@jridgewell/sourcemap-codec" "1.4.14" | ||
|
||
"@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== | ||
|
||
"@socket.io/component-emitter@~3.1.0": | ||
version "3.1.0" | ||
resolved "https://registry.yarnpkg.com/@socket.io/component-emitter/-/component-emitter-3.1.0.tgz#96116f2a912e0c02817345b3c10751069920d553" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Module kotlinx-uuid-datetime | ||
|
||
Provides support for UUIDv7 using kotlinx-datetime. | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("app.softwork:kotlinx-uuid-datetime:LATEST") | ||
} | ||
``` | ||
|
||
UUIDv7 with the current timestamp using `Clock.System` and default SecureRandom can be created using: | ||
|
||
```kotlin | ||
val uuid = UUIDv7() | ||
``` | ||
|
||
When processing existing UUIDv7s, the timestamp bits can be interpreted as an Instant with millisecond precision using: | ||
|
||
```kotlin | ||
UUIDv7().instant | ||
``` |
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,6 @@ | ||
public final class kotlinx/uuid/datetime/DslKt { | ||
public static final fun UUIDv7 (Lkotlinx/datetime/Instant;Lkotlin/random/Random;)Lkotlinx/uuid/UUID; | ||
public static synthetic fun UUIDv7$default (Lkotlinx/datetime/Instant;Lkotlin/random/Random;ILjava/lang/Object;)Lkotlinx/uuid/UUID; | ||
public static final fun getInstant (Lkotlinx/uuid/UUID;)Lkotlinx/datetime/Instant; | ||
} | ||
|
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,24 @@ | ||
/* | ||
* Copyright 2020-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2021 hfhbd and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
plugins { | ||
id("kotlinMPP") | ||
id("publish") | ||
id("dokkaLicensee") | ||
} | ||
|
||
kotlin.sourceSets { | ||
commonMain { | ||
dependencies { | ||
api(projects.kotlinxUuidCore) | ||
api(libs.datetime) | ||
} | ||
} | ||
commonTest { | ||
dependencies { | ||
implementation(kotlin("test")) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
kotlinx-uuid-datetime/src/commonMain/kotlin/kotlinx/uuid/datetime/Dsl.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,20 @@ | ||
package kotlinx.uuid.datetime | ||
|
||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlinx.uuid.SecureRandom | ||
import kotlinx.uuid.UUID | ||
import kotlinx.uuid.UUIDExperimentalAPI | ||
import kotlinx.uuid.UUIDv7 | ||
import kotlinx.uuid.unixTimeStamp | ||
import kotlin.random.Random | ||
|
||
@UUIDExperimentalAPI | ||
public fun UUIDv7(timeStamp: Instant = Clock.System.now(), random: Random = SecureRandom): UUID = | ||
UUIDv7(timeStamp = timeStamp.toEpochMilliseconds(), random = random) | ||
|
||
/** | ||
* The UUIDv7 48 bit big-endian unsigned number of Unix epoch timestamp in milliseconds | ||
*/ | ||
@UUIDExperimentalAPI | ||
public val UUID.instant: Instant get() = Instant.fromEpochMilliseconds(unixTimeStamp) |
20 changes: 20 additions & 0 deletions
20
kotlinx-uuid-datetime/src/commonTest/kotlin/kotlinx/uuid/datetime/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,20 @@ | ||
package kotlinx.uuid.datetime | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.uuid.UUIDExperimentalAPI | ||
import kotlin.random.Random | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(UUIDExperimentalAPI::class) | ||
class InstantTest { | ||
|
||
@Test | ||
fun testConversionInstant() { | ||
val timestamp = Instant.parse("2020-02-20T10:21:42Z") | ||
val uuid = UUIDv7(timeStamp = timestamp, random = Random(4242)) | ||
assertEquals(timestamp, uuid.instant) | ||
assertEquals("0170621e-0ef0-7493-a342-1cdb22b5d84b", uuid.toString()) | ||
assertEquals(7, uuid.versionNumber) | ||
} | ||
} |
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