8000 Switch to JSR 310 Date and Time API by dragneelfps · Pull Request #233 · jasync-sql/jasync-sql · GitHub
[go: up one dir, main page]

Skip to content

Switch to JSR 310 Date and Time API #233

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

Merged
merged 3 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.0.0

* Switch to Java Date/Time API from Joda Time [#233](https://github.com/jasync-sql/jasync-sql/pull/233)
* For postgresql, add support for threeten.PeriodDuration for interval data type. [#233](https://github.com/jasync-sql/jasync-sql/pull/233)

## 1.2.3

* Enable CLIENT_FOUND_ROWS in r2dbc-mysql in favor of spring-data-r2dbc [#240](https://github.com/jasync-sql/jasync-sql/pull/240).
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ apply(plugin = "io.github.gradle-nexus.publish-plugin")
allprojects {

group = "com.github.jasync-sql"
version = "1.2.4"
version = "2.0.0"

apply(plugin = "kotlin")
apply(plugin = "maven-publish")
Expand Down
4 changes: 0 additions & 4 deletions db-async-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
val KOTLIN_VERSION: String by project
val KOTLIN_COROUTINES_VERSION: String by project
val SL4J_VERSION: String by project
val JODA_VERSION: String by project
val JODA_CONVERT_VERSION: String by project
val NETTY_VERSION: String by project
val KOTLIN_LOGGING_VERSION: String by project

Expand All @@ -17,8 +15,6 @@ dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLIN_COROUTINES_VERSION")
implementation("org.slf4j:slf4j-api:$SL4J_VERSION")
implementation("joda-time:joda-time:$JODA_VERSION")
implementation("org.joda:joda-convert:$JODA_CONVERT_VERSION")
implementation("io.netty:netty-transport:$NETTY_VERSION")
implementation("io.netty:netty-handler:$NETTY_VERSION")
compileOnly("io.netty:netty-transport-native-epoll:$NETTY_VERSION:linux-x86_64")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.jasync.sql.db

import com.github.jasync.sql.db.util.XXX
import org.joda.time.LocalDateTime
import java.time.LocalDateTime

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.github.jasync.sql.db.column

import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
import org.joda.time.LocalDate
import org.joda.time.ReadablePartial
import org.joda.time.format.DateTimeFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor

object DateEncoderDecoder : ColumnEncoderDecoder {

private const val ZeroedDate = "0000-00-00"

private val formatter = DateTimeFormat.forPattern("yyyy-MM-dd")
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

override fun decode(value: String): LocalDate? =
if (ZeroedDate == value) {
null
} else {
this.formatter.parseLocalDate(value)
LocalDate.parse(value, this.formatter)
}

override fun encode(value: Any): String {
return when (value) {
is java.sql.Date -> this.formatter.print(LocalDate(value))
is ReadablePartial -> this.formatter.print(value)
is java.sql.Date -> value.toLocalDate().format(this.formatter)
is TemporalAccessor -> this.formatter.format(value)
else -> throw DateEncoderNotAvailableException(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalDateTime
import org.joda.time.format.DateTimeFormatterBuilder
import com.github.jasync.sql.db.util.microsecondsFormatter
import java.time.LocalDateTime
import java.time.format.DateTimeFormatterBuilder

object LocalDateTimeEncoderDecoder : ColumnEncoderDecoder {

private const val ZeroedTimestamp = "0000-00-00 00:00:00"

private val optional = DateTimeFormatterBuilder()
.appendPattern(".SSSSSS").toParser()

private val format = DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendOptional(optional)
.appendOptional(microsecondsFormatter)
.toFormatter()

override fun encode(value: Any): String =
format.print(value as LocalDateTime)
(value as LocalDateTime).format(format)

override fun decode(value: String): LocalDateTime? =
if (ZeroedTimestamp == value) {
null
} else {
format.parseLocalDateTime(value)
LocalDateTime.parse(value, format)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalTime
import org.joda.time.format.DateTimeFormatterBuilder
import java.time.format.DateTimeFormatterBuilder

object SQLTimeEncoder : ColumnEncoder {

Expand All @@ -12,6 +11,6 @@ object SQLTimeEncoder : ColumnEncoder {
override fun encode(value: Any): String {
val time = value as java.sql.Time

return format.print(LocalTime(time.time))
return time.toLocalTime().format(format)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.github.jasync.sql.db.column

import org.joda.time.LocalTime
import org.joda.time.format.DateTimeFormatterBuilder
import com.github.jasync.sql.db.util.microsecondsFormatter
import java.time.LocalTime
import java.time.format.DateTimeFormatterBuilder

open class TimeEncoderDecoder : ColumnEncoderDecoder {
companion object {
val Instance = TimeEncoderDecoder()
}

private val optional = DateTimeFormatterBuilder()
.appendPattern(".SSSSSS").toParser()

private val format = DateTimeFormatterBuilder()
.appendPattern("HH:mm:ss")
.appendOptional(optional)
.appendOptional(microsecondsFormatter)
.toFormatter()

private val printer = DateTimeFormatterBuilder()
Expand All @@ -23,8 +21,8 @@ open class TimeEncoderDecoder : ColumnEncoderDecoder {
open fun formatter() = format

override fun decode(value: String): LocalTime =
format.parseLocalTime(value)
LocalTime.parse(value, formatter())

override fun encode(value: Any): String =
this.printer.print(value as LocalTime)
(value as LocalTime).format(printer)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.jasync.sql.db.column

import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import com.github.jasync.sql.db.util.microsecondsFormatter
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder

object TimeWithTimezoneEncoderDecoder : TimeEncoderDecoder() {

private val format = DateTimeFormat.forPattern("HH:mm:ss.SSSSSSZ")
private val format = DateTimeFormatterBuilder().appendPattern("HH:mm:ss")
.appendOptional(microsecondsFormatter)
.appendPattern("[X][Z]")
.toFormatter()

override fun formatter(): DateTimeFormatter = format
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.github.jasync.sql.db.column

import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
import java.sql.Timestamp
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.TemporalAccessor
import java.util.Calendar
import java.util.Date
import org.joda.time.DateTime
import org.joda.time.LocalDateTime
import org.joda.time.ReadableDateTime
import org.joda.time.format.DateTimeFormatterBuilder

open class TimestampEncoderDecoder : ColumnEncoderDecoder {
companion object {
Expand All @@ -17,9 +17,9 @@ open class TimestampEncoderDecoder : ColumnEncoderDecoder {
}

private val optional = DateTimeFormatterBuilder()
.appendPattern(MillisFormat).toParser()
.appendPattern(MillisFormat).toFormatter()
private val optionalTimeZone = DateTimeFormatterBuilder()
.appendPattern("Z").toParser()
.appendPattern("[X][Z]").toFormatter()

private val builder = DateTimeFormatterBuilder()
.appendPattern(BaseFormat)
Expand All @@ -34,19 +34,23 @@ open class TimestampEncoderDecoder : ColumnEncoderDecoder {

private val format = builder.toFormatter()

// java.util.Dates are constructed using the system default timezone, replicate this behavior when encoding a legacy date
private fun encodeLegacyDate(legacyDate: Date): String =
legacyDate.toInstant().atOffset(ZoneOffset.UTC).format(this.timezonedPrinter)

open fun formatter() = format

override fun decode(value: String): Any {
return formatter().parseLocalDateTime(value)
return LocalDateTime.parse(value, formatter())
}

override fun encode(value: Any): String {
return when (value) {
is Timestamp -> this.timezonedPrinter.print(DateTime(value))
is Date -> this.timezonedPrinter.print(DateTime(value))
is Calendar -> this.timezonedPrinter.print(DateTime(value))
is LocalDateTime -> this.nonTimezonedPrinter.print(value)
is ReadableDateTime -> this.timezonedPrinter.print(value)
is Timestamp -> encodeLegacyDate(value)
is Date -> encodeLegacyDate(value)
is Calendar -> encodeLegacyDate(value.time)
is LocalDateTime -> this.nonTimezonedPrinter.format(value)
is TemporalAccessor -> this.timezonedPrinter.format(value)
else -> throw DateEncoderNotAvailableException(value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.github.jasync.sql.db.column

import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import com.github.jasync.sql.db.util.microsecondsFormatter
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder

object TimestampWithTimezoneEncoderDecoder : TimestampEncoderDecoder() {

private val format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZ")
private val format = DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd HH:mm:ss")
.appendOptional(microsecondsFormatter)
.appendPattern("[X][Z]")
.toFormatter()

override fun formatter(): DateTimeFormatter = format

override fun decode(value: String): Any {
return formatter().parseDateTime(value)
return OffsetDateTime.parse(value, formatter())
}
}
< F438 input type="hidden" name="path" value="db-async-common/src/main/java/com/github/jasync/sql/db/util/EncodingUtils.kt" autocomplete="off" data-targets="deferred-diff-lines.inputs" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.jasync.sql.db.util

import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder

internal const val ALL_MICROS_FORMAT = ".[SSSSSS][SSSSS][SSSS][SSS][SS][S]"

val microsecondsFormatter: DateTimeFormatter = DateTimeFormatterBuilder()
.appendPattern(ALL_MICROS_FORMAT).toFormatter()
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.jasync.sql.db

import java.time.LocalDateTime
import org.assertj.core.api.Assertions.assertThat
import org.joda.time.LocalDateTime
import org.junit.Test

class RowDataTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.github.jasync.sql.db.column

import java.sql.Date
import java.sql.Timestamp
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatterBuilder
import kotlin.test.assertEquals
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormatterBuilder
import org.junit.Test

class TimestampEncoderDecoderSpec {

val encoder = TimestampEncoderDecoder()
val dateTime = DateTime()
.withDate(2013, 12, 27)
.withTime(8, 40, 50, 800)
val dateTime = OffsetDateTime.of(2013, 12, 27, 8, 40, 50, 800000000, ZoneOffset.UTC)

val result = "2013-12-27 08:40:50.800000"
val formatter = DateTimeFormatterBuilder().appendPattern("Z").toFormatter()
val resultWithTimezone = "2013-12-27 08:40:50.800000${formatter.print(dateTime)}"
val resultWithTimezone = "2013-12-27 08:40:50.800000${dateTime.format(formatter)}"

@Test
fun `should print a timestamp`() {
val timestamp = Timestamp(dateTime.toDate().time)
val timestamp = Timestamp.from(dateTime.toInstant())
assertEquals(encoder.encode(timestamp), resultWithTimezone)
}

Expand All @@ -30,13 +30,13 @@ class TimestampEncoderDecoderSpec {

@Test
fun `should print a date`() {
assertEquals(encoder.encode(dateTime.toDate()), resultWithTimezone)
assertEquals(encoder.encode(Date.from(dateTime.toInstant())), resultWithTimezone)
}

@Test
fun `should print a calendar`() {
val calendar = java.util.Calendar.getInstance()
calendar.time = dateTime.toDate()
calendar.time = Date.from(dateTime.toInstant())
encoder.encode(calendar) === resultWithTimezone
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ LOGBACK_VERSION=1.1.8
TEST_CONTAINERS_VERSION=1.15.1
MYSQL_CONNECTOR_VERSION=5.1.47
AWAITILITY_VERSION=3.1.5
THREETEN_EXTRA=1.6.0
4 changes: 0 additions & 4 deletions mysql-async/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
val KOTLIN_VERSION: String by project
val KOTLIN_COROUTINES_VERSION: String by project
val SL4J_VERSION: String by project
val JODA_VERSION: String by project
val JODA_CONVERT_VERSION: String by project
val NETTY_VERSION: String by project
val KOTLIN_LOGGING_VERSION: String by project

Expand All @@ -20,8 +18,6 @@ dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLIN_COROUTINES_VERSION")
compile("org.slf4j:slf4j-api:$SL4J_VERSION")
compile("joda-time:joda-time:$JODA_VERSION")
compile("org.joda:joda-convert:$JODA_CONVERT_VERSION")
compile("io.netty:netty-transport:$NETTY_VERSION")
compile("io.netty:netty-handler:$NETTY_VERSION")
compile("io.github.microutils:kotlin-logging:$KOTLIN_LOGGING_VERSION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ import java.math.BigInteger
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.time.Duration
import org.joda.time.DateTime
import org.joda.time.LocalDate
import org.joda.time.LocalDateTime
import org.joda.time.LocalTime
import org.joda.time.ReadableDateTime
import org.joda.time.ReadableInstant
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime

class BinaryRowEncoder(charset: Charset) {

Expand All @@ -59,7 +58,7 @@ class BinaryRowEncoder(charset: Charset) {
Double::class.java to DoubleEncoder,
java.lang.Double::class.java to DoubleEncoder,
LocalDateTime::class.java to LocalDateTimeEncoder,
DateTime::class.java to DateTimeEncoder,
OffsetDateTime::class.java to DateTimeEncoder,
LocalDate::class.java to LocalDateEncoder,
java.util.Date::class.java to JavaDateEncoder,
java.sql.Timestamp::class.java to SQLTimestampEncoder,
Expand All @@ -78,8 +77,8 @@ class BinaryRowEncoder(charset: Charset) {
is CharSequence -> this.stringEncoder
is java.math.BigInteger -> this.stringEncoder
is BigDecimal -> this.stringEncoder
is ReadableDateTime -> DateTimeEncoder
is ReadableInstant -> ReadableInstantEncoder
is OffsetDateTime -> DateTimeEncoder
is Instant -> ReadableInstantEncoder
is LocalDateTime -> LocalDateTimeEncoder
is java.sql.Timestamp -> SQLTimestampEncoder
is java.sql.Date -> SQLDateEncoder
Expand Down
Loading
0