Skip to content

Commit

Permalink
Fix timestamp to be returned as Unspecified DateTime in legacy mode (n…
Browse files Browse the repository at this point in the history
…pgsql#5466)

Fixes npgsql#5465

(cherry picked from commit 69ee3a4)
Signed-off-by: monjowe <jonas.westman@monitor.se>
  • Loading branch information
roji authored and JonasWestman committed Dec 20, 2023
1 parent 7ae6f91 commit 0bb48cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public override bool CanConvert(DataFormat format, out BufferRequirements buffer

protected override DateTime ReadCore(PgReader reader)
{
if (_timestamp)
{
return PgTimestamp.Decode(reader.ReadInt64(), DateTimeKind.Unspecified, _dateTimeInfinityConversions);
}

var dateTime = PgTimestamp.Decode(reader.ReadInt64(), DateTimeKind.Utc, _dateTimeInfinityConversions);
return !_timestamp && (!_dateTimeInfinityConversions || dateTime != DateTime.MaxValue && dateTime != DateTime.MinValue)
return !_dateTimeInfinityConversions || dateTime != DateTime.MaxValue && dateTime != DateTime.MinValue
? dateTime.ToLocalTime()
: dateTime;
}
Expand Down
8 changes: 8 additions & 0 deletions test/Npgsql.Tests/Types/LegacyDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public Task Timestamp_with_all_DateTime_kinds([Values] DateTimeKind kind)
NpgsqlDbType.Timestamp,
DbType.DateTime);

[Test]
public async Task Timestamp_read_as_Unspecified_DateTime()
{
await using var command = DataSource.CreateCommand("SELECT '2020-03-01T10:30:00'::timestamp");
var dateTime = (DateTime)(await command.ExecuteScalarAsync())!;
Assert.That(dateTime.Kind, Is.EqualTo(DateTimeKind.Unspecified));
}

[Test]
[TestCase(DateTimeKind.Utc, TestName = "Timestamptz_write_utc_DateTime_does_not_convert")]
[TestCase(DateTimeKind.Unspecified, TestName = "Timestamptz_write_unspecified_DateTime_does_not_convert")]
Expand Down

0 comments on commit 0bb48cd

Please sign in to comment.