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