Skip to content

Commit 4fc93d7

Browse files
committed
Add missing Instant converters
1 parent cd7728e commit 4fc93d7

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import kotlinx.datetime.TimeZone
77
import kotlinx.datetime.atStartOfDayIn
88
import kotlinx.datetime.atTime
99
import kotlinx.datetime.toInstant
10+
import kotlinx.datetime.toJavaInstant
1011
import kotlinx.datetime.toJavaLocalDate
1112
import kotlinx.datetime.toJavaLocalDateTime
13+
import kotlinx.datetime.toKotlinInstant
1214
import kotlinx.datetime.toKotlinLocalDate
1315
import kotlinx.datetime.toKotlinLocalDateTime
1416
import kotlinx.datetime.toLocalDateTime
@@ -248,9 +250,20 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
248250
LocalDate::class -> convert<Instant> { it.toLocalDate(defaultTimeZone) }
249251
java.time.LocalDateTime::class -> convert<Instant> { it.toLocalDateTime(defaultTimeZone).toJavaLocalDateTime() }
250252
java.time.LocalDate::class -> convert<Instant> { it.toLocalDate(defaultTimeZone).toJavaLocalDate() }
253+
java.time.Instant::class -> convert<Instant> { it.toJavaInstant() }
251254
LocalTime::class -> convert<Instant> { it.toLocalTime(defaultTimeZone) }
252255
else -> null
253256
}
257+
java.time.Instant::class -> when (toClass) {
258+
Long::class -> convert<java.time.Instant> { it.toEpochMilli() }
259+
LocalDateTime::class -> convert<java.time.Instant> { it.toKotlinInstant().toLocalDateTime(defaultTimeZone) }
260+
LocalDate::class -> convert<java.time.Instant> { it.toKotlinInstant().toLocalDate(defaultTimeZone) }
261+
java.time.LocalDateTime::class -> convert<java.time.Instant> { it.toKotlinInstant().toLocalDateTime(defaultTimeZone).toJavaLocalDateTime() }
262+
java.time.LocalDate::class -> convert<java.time.Instant> { it.toKotlinInstant().toLocalDate(defaultTimeZone).toJavaLocalDate() }
263+
Instant::class -> convert<java.time.Instant> { it.toKotlinInstant() }
264+
LocalTime::class -> convert<java.time.Instant> { it.toKotlinInstant().toLocalTime(defaultTimeZone) }
265+
else -> null
266+
}
254267
Float::class -> when (toClass) {
255268
Double::class -> convert<Float> { it.toDouble() }
256269
Long::class -> convert<Float> { it.roundToLong() }

docs/StardustDocs/topics/convertTo.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,20 @@ convertTo<T>()
88
```
99

1010
Any additional columns will be dropped.
11+
12+
You can provide custom converters and parsers:
13+
<!---FUN customConverters-->
14+
15+
```kotlin
16+
class IntClass(val value: Int)
17+
18+
@DataSchema
19+
class IntSchema(val ints: IntClass)
20+
21+
val df = dataFrameOf("ints")(1, 2, 3)
22+
df.convertTo<IntSchema> {
23+
convert<Int>().with { IntClass(it) }
24+
}
25+
```
26+
27+
<!---END-->

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

3+
import io.kotest.assertions.throwables.shouldNotThrow
34
import io.kotest.assertions.throwables.shouldThrow
45
import io.kotest.matchers.shouldBe
56
import kotlinx.datetime.Clock
@@ -137,4 +138,14 @@ class ConvertTests {
137138

138139
col.convertTo<Int?>().hasNulls() shouldBe false
139140
}
141+
142+
@Test
143+
fun `convert instant`() {
144+
println(Clock.System.now().toEpochMilliseconds())
145+
val kotlinxInstants = columnOf(Instant.fromEpochMilliseconds(1657283006955))
146+
shouldNotThrow<TypeConverterNotFoundException> {
147+
val javaInstant = kotlinxInstants.convertTo<java.time.Instant>()
148+
javaInstant.convertTo<Instant>() shouldBe kotlinxInstants
149+
}
150+
}
140151
}

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Modify.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.samples.api
22

33
import io.kotest.matchers.shouldBe
44
import org.jetbrains.kotlinx.dataframe.DataFrame
5+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
56
import org.jetbrains.kotlinx.dataframe.api.*
67
import org.junit.Test
78
import java.time.format.DateTimeFormatter
@@ -921,4 +922,19 @@ class Modify : TestBase() {
921922
.rename { isHappy }.into("isOK")
922923
// SampleEnd
923924
}
925+
926+
@Test
927+
fun customConverters() {
928+
// SampleStart
929+
class IntClass(val value: Int)
930+
931+
@DataSchema
932+
class IntSchema(val ints: IntClass)
933+
934+
val df = dataFrameOf("ints")(1, 2, 3)
935+
df.convertTo<IntSchema> {
936+
convert<Int>().with { IntClass(it) }
937+
}
938+
// SampleEnd
939+
}
924940
}

0 commit comments

Comments
 (0)