8000 Switch to JSR 310 Date and Time API · jasync-sql/jasync-sql@b0aab75 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0aab75

Browse files
committed
Switch to JSR 310 Date and Time API
- Removes joda from encoding/decoding - Using Threeten for PeriodDuration
1 parent 152cdef commit b0aab75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+324
-294
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.0.0
4+
5+
* Switch to Java Date/Time API from Joda Time [#233](https://github.com/jasync-sql/jasync-sql/pull/233)
6+
* For postgresql, add support for threeten.PeriodDuration for interval data type. [#233](https://github.com/jasync-sql/jasync-sql/pull/233)
7+
38
## 1.2.1
49

510
* Deploy directly to maven central [#232](https://github.com/jasync-sql/jasync-sql/pull/232).

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ apply(plugin = "io.github.gradle-nexus.publish-plugin")
2424
allprojects {
2525

2626
group = "com.github.jasync-sql"
27-
version = "1.2.3"
27+
version = "2.0.0"
2828

2929
apply(plugin = "kotlin")
3030
apply(plugin = "maven-publish")

db-async-common/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
val KOTLIN_VERSION: String by project
22
val KOTLIN_COROUTINES_VERSION: String by project
33
val SL4J_VERSION: String by project
4-
val JODA_VERSION: String by project
5-
val JODA_CONVERT_VERSION: String by project
64
val NETTY_VERSION: String by project
75
val KOTLIN_LOGGING_VERSION: String by project
86

@@ -17,8 +15,6 @@ dependencies {
1715
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION")
1816
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLIN_COROUTINES_VERSION")
1917
implementation("org.slf4j:slf4j-api:$SL4J_VERSION")
20-
implementation("joda-time:joda-time:$JODA_VERSION")
21-
implementation("org.joda:joda-convert:$JODA_CONVERT_VERSION")
2218
implementation("io.netty:netty-transport:$NETTY_VERSION")
2319
implementation("io.netty:netty-handler:$NETTY_VERSION")
2420
compileOnly("io.netty:netty-transport-native-epoll:$NETTY_VERSION:linux-x86_64")

db-async-common/src/main/java/com/github/jasync/sql/db/RowData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.jasync.sql.db
22

33
import com.github.jasync.sql.db.util.XXX
4-
import org.joda.time.LocalDateTime
4+
import java.time.LocalDateTime
55

66
/**
77
*

db-async-common/src/main/java/com/github/jasync/sql/db/column/DateEncoderDecoder.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package com.github.jasync.sql.db.column
22

33
import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
4-
import org.joda.time.LocalDate
5-
import org.joda.time.ReadablePartial
6-
import org.joda.time.format.DateTimeFormat
4+
import java.time.LocalDate
5+
import java.time.format.DateTimeFormatter
6+
import java.time.temporal.TemporalAccessor
77

88
object DateEncoderDecoder : ColumnEncoderDecoder {
99

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

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

1414
override fun decode(value: String): LocalDate? =
1515
if (ZeroedDate == value) {
1616
null
1717
} else {
18-
this.formatter.parseLocalDate(value)
18+
LocalDate.parse(value, this.formatter)
1919
}
2020

2121
override fun encode(value: Any): String {
2222
return when (value) {
23-
is java.sql.Date -> this.formatter.print(LocalDate(value))
24-
is ReadablePartial -> this.formatter.print(value)
23+
is java.sql.Date -> value.toLocalDate().format(this.formatter)
24+
is TemporalAccessor -> this.formatter.format(value)
2525
else -> throw DateEncoderNotAvailableException(value)
2626
}
2727
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package com.github.jasync.sql.db.column
22

3-
import org.joda.time.LocalDateTime
4-
import org.joda.time.format.DateTimeFormatterBuilder
3+
import com.github.jasync.sql.db.util.microsecondsFormatter
4+
import java.time.LocalDateTime
5+
import java.time.format.DateTimeFormatterBuilder
56

67
object LocalDateTimeEncoderDecoder : ColumnEncoderDecoder {
78

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

10-
private val optional = DateTimeFormatterBuilder()
11-
.appendPattern(".SSSSSS").toParser()
12-
1311
private val format = DateTimeFormatterBuilder()
1412
.appendPattern("yyyy-MM-dd HH:mm:ss")
15-
.appendOptional(optional)
13+
.append(microsecondsFormatter)
1614
.toFormatter()
1715

1816
override fun encode(value: Any): String =
19-
format.print(value as LocalDateTime)
17+
(value as LocalDateTime).format(format)
2018

2119
override fun decode(value: String): LocalDateTime? =
2220
if (ZeroedTimestamp == value) {
2321
null
2422
} else {
25-
format.parseLocalDateTime(value)
23+
LocalDateTime.parse(value, format)
2624
}
2725
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.jasync.sql.db.column
22

3-
import org.joda.time.LocalTime
4-
import org.joda.time.format.DateTimeFormatterBuilder
3+
import java.time.format.DateTimeFormatterBuilder
54

65
object SQLTimeEncoder : ColumnEncoder {
76

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

15-
return format.print(LocalTime(time.time))
14+
return time.toLocalTime().format(format)
1615
}
1716
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.github.jasync.sql.db.column
22

3-
import org.joda.time.LocalTime
4-
import org.joda.time.format.DateTimeFormatterBuilder
3+
import com.github.jasync.sql.db.util.microsecondsFormatter
4+
import java.time.LocalTime
5+
import java.time.format.DateTimeFormatterBuilder
56

67
open class TimeEncoderDecoder : ColumnEncoderDecoder {
78
companion object {
89
val Instance = TimeEncoderDecoder()
910
}
1011

11-
private val optional = DateTimeFormatterBuilder()
12-
.appendPattern(".SSSSSS").toParser()
13-
1412
private val format = DateTimeFormatterBuilder()
1513
.appendPattern("HH:mm:ss")
16-
.appendOptional(optional)
14+
.appendOptional(microsecondsFormatter)
1715
.toFormatter()
1816

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

2523
override fun decode(value: String): LocalTime =
26-
format.parseLocalTime(value)
24+
LocalTime.parse(value, formatter())
2725

2826
override fun encode(value: Any): String =
29-
this.printer.print(value as LocalTime)
27+
(value as LocalTime).format(printer)
3028
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.github.jasync.sql.db.column
22

3-
import org.joda.time.format.DateTimeFormat
4-
import org.joda.time.format.DateTimeFormatter
3+
import com.github.jasync.sql.db.util.microsecondsFormatter
4+
import java.time.format.DateTimeFormatter
5+
import java.time.format.DateTimeFormatterBuilder
56

67
object TimeWithTimezoneEncoderDecoder : TimeEncoderDecoder() {
78

8-
private val format = DateTimeFormat.forPattern("HH:mm:ss.SSSSSSZ")
9+
private val format = DateTimeFormatterBuilder().appendPattern("HH:mm:ss")
10+
.appendOptional(microsecondsFormatter)
11+
.appendPattern("[X][Z]")
12+
.toFormatter()
913

1014
override fun formatter(): DateTimeFormatter = format
1115
}

db-async-common/src/main/java/com/github/jasync/sql/db/column/TimestampEncoderDecoder.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package com.github.jasync.sql.db.column
22

33
import com.github.jasync.sql.db.exceptions.DateEncoderNotAvailableException
44
import java.sql.Timestamp
5+
import java.time.LocalDateTime
6+
import java.time.ZoneOffset
7+
import java.time.format.DateTimeFormatterBuilder
8+
import java.time.temporal.TemporalAccessor
59
import java.util.Calendar
610
import java.util.Date
7-
import org.joda.time.DateTime
8-
import org.joda.time.LocalDateTime
9-
import org.joda.time.ReadableDateTime
10-
import org.joda.time.format.DateTimeFormatterBuilder
1111

1212
open class TimestampEncoderDecoder : ColumnEncoderDecoder {
1313
companion object {
@@ -17,9 +17,9 @@ open class TimestampEncoderDecoder : ColumnEncoderDecoder {
1717
}
1818

1919
private val optional = DateTimeFormatterBuilder()
20-
.appendPattern(MillisFormat).toParser()
20+
.appendPattern(MillisFormat).toFormatter()
2121
private val optionalTimeZone = DateTimeFormatterBuilder()
22-
.appendPattern("Z").toParser()
22+
.appendPattern("[X][Z]").toFormatter()
2323

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

3535
private val format = builder.toFormatter()
3636

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

3943
override fun decode(value: String): Any {
40-
return formatter().parseLocalDateTime(value)
44+
return LocalDateTime.parse(value, formatter())
4145
}
4246

4347
override fun encode(value: Any): String {
4448
return when (value) {
45-
is Timestamp -> this.timezonedPrinter.print(DateTime(value))
46-
is Date -> this.timezonedPrinter.print(DateTime(value))
47-
is Calendar -> this.timezonedPrinter.print(DateTime(value))
48-
is LocalDateTime -> this.nonTimezonedPrinter.print(value)
49-
is ReadableDateTime -> this.timezonedPrinter.print(value)
49+
is Timestamp -> encodeLegacyDate(value)
50+
is Date -> encodeLegacyDate(value)
51+
is Calendar -> encodeLegacyDate(value.time)
52+
is LocalDateTime -> this.nonTimezonedPrinter.format(value)
53+
is TemporalAccessor -> this.timezonedPrinter.format(value)
5054
else -> throw DateEncoderNotAvailableException(value)
5155
}
5256
}

0 commit comments

Comments
 (0)
0