From 138423f5e15cf087a97bb3e8d9ad87b302de5338 Mon Sep 17 00:00:00 2001 From: tomyeh Date: Thu, 3 Jul 2014 22:39:17 +0800 Subject: [PATCH] #7 Support timestamptz --- lib/connection.dart | 8 +++++++- lib/format_value.dart | 9 +++++---- test/postgresql_test.dart | 12 ++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/connection.dart b/lib/connection.dart index 5ba0b4e..b60293f 100644 --- a/lib/connection.dart +++ b/lib/connection.dart @@ -530,9 +530,15 @@ class _Connection implements Connection { case _PG_DATE: return DateTime.parse(UTF8.decode(data)); + case _PG_TIMESTAMPZ: + var str = UTF8.decode(data), + cc = str[str.length - 3]; + if (cc == '+' || cc == '-') + str += ":00"; //convert to ISO 8601 (2012-02-27 13:27:00.123+02:00) + return DateTime.parse(str).toLocal(); + // Not implemented yet - return a string. case _PG_MONEY: - case _PG_TIMESTAMPZ: case _PG_TIMETZ: case _PG_TIME: case _PG_INTERVAL: diff --git a/lib/format_value.dart b/lib/format_value.dart index 6d024ee..9f95b9d 100644 --- a/lib/format_value.dart +++ b/lib/format_value.dart @@ -80,6 +80,9 @@ _formatDateTime(DateTime datetime, String type) { throw new Exception('Unexpected type: $type.'); //TODO exception type } + if (t == 'timestamptz') + datetime = datetime.toUtc(); + pad(i) { var s = i.toString(); return s.length == 1 ? '0$s' : s; @@ -111,10 +114,8 @@ _formatDateTime(DateTime datetime, String type) { } } - if (t == 'timestamptz') { - // Add timezone offset. - throw new Exception('Not implemented'); //TODO - } + if (t == 'timestamptz') + sb.write("Z"); return "'${sb.toString()}'"; } diff --git a/test/postgresql_test.dart b/test/postgresql_test.dart index a837ce3..31c2de5 100644 --- a/test/postgresql_test.dart +++ b/test/postgresql_test.dart @@ -289,6 +289,18 @@ main() { ); }); + test('Select timestamptz with milliseconds', () { + final DateTime time = new DateTime(1979, 12, 20, 9, 0, 12); + conn.execute('create temporary table dart_unit_test (a timestamptz)'); + conn.execute("insert into dart_unit_test values (@time)", {"time": time}); + + conn.query('select a from dart_unit_test').toList().then( + expectAsync1((rows) { + expect(rows[0][0], equals(time)); + }) + ); + }); + test('Select DateTime', () { conn.execute('create temporary table dart_unit_test (a date)');