Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode timestamp without timezone as local DateTime and decode timestamp with timezone respecting the timezone defined in the connection #342

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
20 changes: 18 additions & 2 deletions lib/src/types/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,15 @@ class PostgresBinaryDecoder {
if (dinput.timeZone.forceDecodeDateAsUTC) {
return DateTime.utc(2000).add(Duration(days: value));
}
return DateTime(2000).add(Duration(days: value));
// https://github.com/dart-lang/sdk/issues/56312
// ignore past timestamp transitions and use only current timestamp in local datetime
final nowDt = DateTime.now();
var baseDt = DateTime(2000);
if (baseDt.timeZoneOffset != nowDt.timeZoneOffset) {
final difference = baseDt.timeZoneOffset - nowDt.timeZoneOffset;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering, without fully seeing the context, if this operation is always valid. E.g. can this be close to or larger than a full day? Or instead of substracting a half hour, are we adding 23.5 hours, just because of a strange combination?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that there will not be this problem because the timezone transitions of the past are more related to the daylight saving time of countries that by law no longer have daylight saving time.

baseDt = baseDt.add(difference);
}
return baseDt.add(Duration(days: value));
case TypeOid.timestampWithoutTimezone:
final value = buffer.getInt64(0);
//infinity || -infinity
Expand All @@ -807,7 +815,15 @@ class PostgresBinaryDecoder {
if (dinput.timeZone.forceDecodeTimestampAsUTC) {
return DateTime.utc(2000).add(Duration(microseconds: value));
}
return DateTime(2000).add(Duration(microseconds: value));
// https://github.com/dart-lang/sdk/issues/56312
// 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
final nowDt = DateTime.now();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract these two into a method that does the correction.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes definitely

var baseDt = DateTime(2000);
if (baseDt.timeZoneOffset != nowDt.timeZoneOffset) {
final difference = baseDt.timeZoneOffset - nowDt.timeZoneOffset;
baseDt = baseDt.add(difference);
}
return baseDt.add(Duration(microseconds: value));

case TypeOid.timestampWithTimezone:
final value = buffer.getInt64(0);
Expand Down