From 65b436816750117cb60fcccd32b52de53ecc0a08 Mon Sep 17 00:00:00 2001 From: Jochen Mader Date: Sun, 19 Aug 2018 00:38:31 +0200 Subject: [PATCH] Fix for #240, also added some tests --- .../PostgreSQLTimestampEncoderDecoder.scala | 2 +- ...ostgreSQLTimestampEncoderDecoderSpec.scala | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoderSpec.scala diff --git a/postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoder.scala b/postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoder.scala index 7c6c426d..1fd7d30b 100644 --- a/postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoder.scala +++ b/postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoder.scala @@ -39,7 +39,7 @@ object PostgreSQLTimestampEncoderDecoder extends ColumnEncoderDecoder { index => new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd HH:mm:ss") - .appendPattern("." + ("S" * index )) + .appendOptional(new DateTimeFormatterBuilder().appendPattern("." + ("S" * index )).toParser) .appendOptional(optionalTimeZone) .toFormatter } diff --git a/postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoderSpec.scala b/postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoderSpec.scala new file mode 100644 index 00000000..8a6faf32 --- /dev/null +++ b/postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLTimestampEncoderDecoderSpec.scala @@ -0,0 +1,45 @@ +package com.github.mauricio.async.db.postgresql.column + +import com.github.mauricio.async.db.postgresql.messages.backend.PostgreSQLColumnData +import io.netty.buffer.Unpooled +import io.netty.util.CharsetUtil +import org.joda.time +import org.joda.time.DateTimeZone +import org.specs2.mutable.Specification + +class PostgreSQLTimestampEncoderDecoderSpec extends Specification { + + def execute( columnType:Int, data : String ) : Any = { + val date = data.getBytes( CharsetUtil.UTF_8 ) + PostgreSQLTimestampEncoderDecoder.decode( + PostgreSQLColumnData("name",1,1,columnType,1,1,1), + Unpooled.wrappedBuffer(date), CharsetUtil.UTF_8) + } + + "encoder/decoder" should { + + "parse a date" in { + val localDateTime = execute(ColumnTypes.Timestamp, "2018-08-15 18:26:36").asInstanceOf[time.LocalDateTime] + localDateTime.getYear === 2018 + localDateTime.getMonthOfYear === 8 + localDateTime.getDayOfMonth === 15 + localDateTime.getHourOfDay === 18 + localDateTime.getMinuteOfHour === 26 + localDateTime.getSecondOfMinute === 36 + + } + + "parse a date with timezone" in { + DateTimeZone.setDefault(DateTimeZone.UTC) + val dateTime = execute(ColumnTypes.TimestampWithTimezone, "2018-08-15 18:26:36+08").asInstanceOf[time.DateTime] + dateTime.getYear === 2018 + dateTime.getMonthOfYear === 8 + dateTime.getDayOfMonth === 15 + dateTime.getHourOfDay === 10 + dateTime.getMinuteOfHour === 26 + dateTime.getSecondOfMinute === 36 + } + + } + +} \ No newline at end of file