Skip to content

Commit ab75ea7

Browse files
author
Isaque Neves
committed
fix decode timestamp without timezone as local DateTime, ignore past timezone transitions
1 parent 558f2ef commit ab75ea7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/src/types/binary_codec.dart

+18-2
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,15 @@ class PostgresBinaryDecoder {
796796
if (dinput.timeZone.forceDecodeDateAsUTC) {
797797
return DateTime.utc(2000).add(Duration(days: value));
798798
}
799-
return DateTime(2000).add(Duration(days: value));
799+
// https://github.com/dart-lang/sdk/issues/56312
800+
// ignore past timestamp transitions and use only current timestamp in local datetime
801+
final nowDt = DateTime.now();
802+
var baseDt = DateTime(2000);
803+
if (baseDt.timeZoneOffset != nowDt.timeZoneOffset) {
804+
final difference = baseDt.timeZoneOffset - nowDt.timeZoneOffset;
805+
baseDt = baseDt.add(difference);
806+
}
807+
return baseDt.add(Duration(days: value));
800808
case TypeOid.timestampWithoutTimezone:
801809
final value = buffer.getInt64(0);
802810
//infinity || -infinity
@@ -807,7 +815,15 @@ class PostgresBinaryDecoder {
807815
if (dinput.timeZone.forceDecodeTimestampAsUTC) {
808816
return DateTime.utc(2000).add(Duration(microseconds: value));
809817
}
810-
return DateTime(2000).add(Duration(microseconds: value));
818+
// https://github.com/dart-lang/sdk/issues/56312
819+
// ignore previous timestamp transitions and use only the current system timestamp in local date and time so that the behavior is correct on Windows and Linux
820+
final nowDt = DateTime.now();
821+
var baseDt = DateTime(2000);
822+
if (baseDt.timeZoneOffset != nowDt.timeZoneOffset) {
823+
final difference = baseDt.timeZoneOffset - nowDt.timeZoneOffset;
824+
baseDt = baseDt.add(difference);
825+
}
826+
return baseDt.add(Duration(microseconds: value));
811827

812828
case TypeOid.timestampWithTimezone:
813829
final value = buffer.getInt64(0);

0 commit comments

Comments
 (0)