Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kotlinx-datetime module (#198) #351

Merged
merged 9 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
kotlin = "2.0.0"
serialization = "1.7.0"
exposed = "0.51.1"
datetime = "0.6.0"

[libraries]
serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialization" }
Expand All @@ -16,6 +17,8 @@ slf4j = { module = "org.slf4j:slf4j-simple", version = "2.0.13" }

sqldelight-runtime = { module = "app.cash.sqldelight:runtime", version = "2.0.2" }

datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
Expand Down
21 changes: 21 additions & 0 deletions kotlinx-uuid-datetime/README.md
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 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
```
36 changes: 36 additions & 0 deletions kotlinx-uuid-datetime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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("kotlinJvm")
hfhbd marked this conversation as resolved.
Show resolved Hide resolved
id("publish")
id("dokkaLicensee")
id("kover")
}

kotlin.jvmToolchain(11)

dependencies {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch to commonSourceSet

api(projects.kotlinxUuidCore)
api(libs.datetime)

testImplementation(kotlin("test-junit"))
testRuntimeOnly(libs.slf4j)
}

licensee {
allow("MIT")
}

publishing {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, it's not needed with mpp

publications.register<MavenPublication>("maven") {
from(components["java"])
}
}

java {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove too

withJavadocJar()
withSourcesJar()
}
20 changes: 20 additions & 0 deletions kotlinx-uuid-datetime/src/main/kotlin/kotlinx/uuid/datetime/Dsl.kt
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(random: Random = SecureRandom): UUID =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't hardcode Clock System, instead add an Instant parameter, this should be enough.

UUIDv7(timeStamp = Clock.System.now().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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kotlinx.uuid.datetime

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.uuid.UUIDExperimentalAPI
import kotlinx.uuid.UUIDv7
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(UUIDExperimentalAPI::class)
class InstantTest {

@Test
fun testConversionInstant() {
val timestamp = Clock.System.now()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use a hardcoded Instant

val expected = timestamp.clampToMillisecondPrecision()

val uuid = UUIDv7(timeStamp = timestamp.toEpochMilliseconds())
val result = uuid.instant

assertEquals(expected, result)
}

private fun Instant.clampToMillisecondPrecision(): Instant {
return Instant.fromEpochMilliseconds(this.toEpochMilliseconds())
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ include(":kotlinx-uuid-core")

include(":kotlinx-uuid-exposed")
include(":kotlinx-uuid-sqldelight")
include(":kotlinx-uuid-datetime")