Hey! anyone know if Exposed supports OffsetDateTime as a column type? or any suggestions how to work around not being able to save a timezone would be really appreciated 🙂
Peter Scardera
12/10/2021, 4:55 PM
I will just convert the time to UTC before inserting actually OR save an Instant
d
dave08
12/12/2021, 3:44 AM
You could make your own type based on an existing one
🙌 1
h
hfhbd
12/15/2021, 11:41 AM
sample for postgres and kotlin-datetime Instant:
Copy code
// Custom implementation since Exposed kotlin-datetime timestamp does not use UTC...
public fun Table.instant(name: String): Column<Instant> =
registerColumn(name = name, type = PostgresInstantColumn())
private class PostgresInstantColumn : ColumnType(), IDateColumnType {
override val hasTimePart = true
override fun sqlType(): String = "TIMESTAMPTZ"
override fun valueFromDB(value: Any): Instant = when (value) {
is Instant -> value
is java.time.Instant -> value.toKotlinInstant()
is java.sql.Timestamp -> value.toInstant().toKotlinInstant()
is String -> value.toInstant()
else -> error("Unexpected value of type Instant: $value of ${value::class.qualifiedName}")
}
override fun notNullValueToDB(value: Any): java.sql.Timestamp = when (value) {
is Instant -> java.sql.Timestamp.from(value.toJavaInstant())
is java.time.Instant -> java.sql.Timestamp.from(value)
is String -> java.sql.Timestamp.from(value.toInstant().toJavaInstant())
else -> error("Unexpected value of type Instant: $value of ${value::class.qualifiedName}")
}
}