Skip to content

Commit

Permalink
Add kotlinx-datetime module (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Jun 22, 2024
1 parent ca237aa commit 1f6c687
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
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
42 changes: 42 additions & 0 deletions kotlinx-uuid-datetime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Module kotlinx-uuid-datetime

Provides support for UUIDv7 using kotlinx-datetime.

```kotlin
dependencies {
implementation("app.softwork:kotlinx-uuid-datetime:LATEST")
}
```

When declaring a table having UUID as Primary Key:

```kotlin
// SQL DSL
object MyTable : KotlinxUUIDTable() {
// there is "id" property with the kotlin-uud type
}

// DAO API
class MyTableEntity(id: EntityID<UUID>) : KotlinxUUIDEntity(id) {
companion object : KotlinxUUIDEntityClass<MyTableEntity>(MyTable)

}
```

To declare a regular column, use `kotlinxUUID` function:

```kotlin
object MyTable : Table() {
val something = kotlinxUUID("SOME_COLUMN")
}
```

Unfortunately, there is a function called `uuid` in the base class, inside Exposed, this is why we can't
overwrite/override it, so it may lead to confusion. The function `uuid` only works with `java.util.UUID`:

```kotlin
object MyTable : Table() {
val column1 = kotlinxUUID("C1") // kotlinx.uuid.UUID
val column2 = uuid("C2") // java.util.UUID
}
```
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")
id("publish")
id("dokkaLicensee")
id("kover")
}

kotlin.jvmToolchain(11)

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

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

licensee {
allow("MIT")
}

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

java {
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 =
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()
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")

0 comments on commit 1f6c687

Please sign in to comment.