Skip to content

Commit

Permalink
Converting DateTimes to UTC without string modification.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed May 18, 2024
1 parent 3d6f623 commit 57c05d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions lib/src/types/text_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,24 @@ class PostgresTextDecoder {

case TypeOid.timestampWithTimezone:
case TypeOid.timestampWithoutTimezone:
return DateTime.parse(di.asText);
final raw = DateTime.parse(di.asText);
return DateTime.utc(
raw.year,
raw.month,
raw.day,
raw.hour,
raw.minute,
raw.second,
raw.millisecond,
raw.microsecond,
);

case TypeOid.numeric:
return di.asText;

case TypeOid.date:
return DateTime.parse(di.asText);
final raw = DateTime.parse(di.asText);
return DateTime.utc(raw.year, raw.month, raw.day);

case TypeOid.json:
case TypeOid.jsonb:
Expand Down
4 changes: 2 additions & 2 deletions test/decode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {
final rs = await conn
.execute("SELECT '1999-01-08 04:05:06'::TIMESTAMP WITHOUT TIME ZONE");
final item = rs.single.single as DateTime;
expect(item.toIso8601String(), '1999-01-08T04:05:06.000');
expect(item.toIso8601String(), '1999-01-08T04:05:06.000Z');
});

test('interval', () async {
Expand All @@ -51,7 +51,7 @@ void main() {
test('date', () async {
final rs = await conn.execute("SELECT '1999-01-08'::DATE");
final item = rs.single.single as DateTime;
expect(item.toIso8601String(), '1999-01-08T00:00:00.000');
expect(item.toIso8601String(), '1999-01-08T00:00:00.000Z');
});

test('json', () async {
Expand Down

0 comments on commit 57c05d1

Please sign in to comment.