Exposed: Local Date Time

Created on 14 May 2019  路  3Comments  路  Source: JetBrains/Exposed

Hey guys,

I've been using Exposed to create a database for some timeseries data. I've noticed the following code in ColumnType.kt:

class DateColumnType(val time: Boolean): ColumnType() {
    override fun sqlType(): String  = if (time) currentDialect.dataTypeProvider.dateTimeType() else "DATE"

    override fun nonNullValueToString(value: Any): String {
        if (value is String) return value

        val dateTime = when (value) {
            is DateTime -> value
            is java.sql.Date -> DateTime(value.time)
            is java.sql.Timestamp -> DateTime(value.time)
            else -> error("Unexpected value: $value of ${value::class.qualifiedName}")
        }

        return if (time)
            "'${DEFAULT_DATE_TIME_STRING_FORMATTER.print(dateTime.toDateTime(DateTimeZone.getDefault()))}'"
        else
            "'${DEFAULT_DATE_STRING_FORMATTER.print(dateTime)}'"
    }

This uses DateTimeZone.getDefault() to get the string for the DateTime value. However, I do not want to use the System Locale Timezone to save values on the DB. Could we make a change to specify the timezone to use? Another (maybe easier) solution would be to include functionality for Joda's LocalDateTime instead.

Let me know what you guys think.

Fred

datetime

Most helpful comment

@Tapac That is not a solution to this issue. I argue this should be re-opened. Java LocalDateTime does not use a time zone. IF we use a LocalDateTime with exposed, it will assume system default time zone on insert.

All 3 comments

Now that I think about it, is there any reason why we are forcing the use of the default DateTimeZone when storing/querying the DB? Why not just write the date in the time zone defined by the user?

Java Time Api LocalDateTime available.

@Tapac That is not a solution to this issue. I argue this should be re-opened. Java LocalDateTime does not use a time zone. IF we use a LocalDateTime with exposed, it will assume system default time zone on insert.

Was this page helpful?
0 / 5 - 0 ratings