Skip to content

Commit 65b4368

Browse files
Fix for mauricio#240, also added some tests
1 parent 5d0a7e0 commit 65b4368

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object PostgreSQLTimestampEncoderDecoder extends ColumnEncoderDecoder {
3939
index =>
4040
new DateTimeFormatterBuilder()
4141
.appendPattern("yyyy-MM-dd HH:mm:ss")
42-
.appendPattern("." + ("S" * index ))
42+
.appendOptional(new DateTimeFormatterBuilder().appendPattern("." + ("S" * index )).toParser)
4343
.appendOptional(optionalTimeZone)
4444
.toFormatter
4545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.mauricio.async.db.postgresql.column
2+
3+
import com.github.mauricio.async.db.postgresql.messages.backend.PostgreSQLColumnData
4+
import io.netty.buffer.Unpooled
5+
import io.netty.util.CharsetUtil
6+
import org.joda.time
7+
import org.joda.time.DateTimeZone
8+
import org.specs2.mutable.Specification
9+
10+
class PostgreSQLTimestampEncoderDecoderSpec extends Specification {
11+
12+
def execute( columnType:Int, data : String ) : Any = {
13+
val date = data.getBytes( CharsetUtil.UTF_8 )
14+
PostgreSQLTimestampEncoderDecoder.decode(
15+
PostgreSQLColumnData("name",1,1,columnType,1,1,1),
16+
Unpooled.wrappedBuffer(date), CharsetUtil.UTF_8)
17+
}
18+
19+
"encoder/decoder" should {
20+
21+
"parse a date" in {
22+
val localDateTime = execute(ColumnTypes.Timestamp, "2018-08-15 18:26:36").asInstanceOf[time.LocalDateTime]
23+
localDateTime.getYear === 2018
24+
localDateTime.getMonthOfYear === 8
25+
localDateTime.getDayOfMonth === 15
26+
localDateTime.getHourOfDay === 18
27+
localDateTime.getMinuteOfHour === 26
28+
localDateTime.getSecondOfMinute === 36
29+
30+
}
31+
32+
"parse a date with timezone" in {
33+
DateTimeZone.setDefault(DateTimeZone.UTC)
34+
val dateTime = execute(ColumnTypes.TimestampWithTimezone, "2018-08-15 18:26:36+08").asInstanceOf[time.DateTime]
35+
dateTime.getYear === 2018
36+
dateTime.getMonthOfYear === 8
37+
dateTime.getDayOfMonth === 15
38+
dateTime.getHourOfDay === 10
39+
dateTime.getMinuteOfHour === 26
40+
dateTime.getSecondOfMinute === 36
41+
}
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)