Skip to content

Commit

Permalink
Add DateTime support to nodatimes TimestampTzHandler
Browse files Browse the repository at this point in the history
Follow-up fix for #2439.
  • Loading branch information
davidroth committed Oct 16, 2020
1 parent e32698f commit 2bdb08d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/Npgsql.NodaTime/TimestampTzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public override NpgsqlTypeHandler<Instant> Create(PostgresType postgresType, Npg
}

sealed class TimestampTzHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsqlSimpleTypeHandler<ZonedDateTime>,
INpgsqlSimpleTypeHandler<OffsetDateTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>
INpgsqlSimpleTypeHandler<OffsetDateTime>, INpgsqlSimpleTypeHandler<DateTimeOffset>,
INpgsqlSimpleTypeHandler<DateTime>
{
readonly IDateTimeZoneProvider _dateTimeZoneProvider;
readonly BclTimestampTzHandler _bclHandler;
Expand Down Expand Up @@ -129,5 +130,14 @@ int INpgsqlSimpleTypeHandler<DateTimeOffset>.ValidateAndGetLength(DateTimeOffset

void INpgsqlSimpleTypeHandler<DateTimeOffset>.Write(DateTimeOffset value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
=> _bclHandler.Write(value, buf, parameter);

DateTime INpgsqlSimpleTypeHandler<DateTime>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
=> _bclHandler.Read<DateTime>(buf, len, fieldDescription);

int INpgsqlSimpleTypeHandler<DateTime>.ValidateAndGetLength(DateTime value, NpgsqlParameter? parameter)
=> _bclHandler.ValidateAndGetLength(value, parameter);

void INpgsqlSimpleTypeHandler<DateTime>.Write(DateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
=> _bclHandler.Write(value, buf, parameter);
}
}
11 changes: 7 additions & 4 deletions test/Npgsql.PluginTests/NodaTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,23 @@ public void TimestampTz()
var localZonedDateTime = utcZonedDateTime.WithZone(DateTimeZoneProviders.Tzdb[timezone]);
var offsetDateTime = localZonedDateTime.ToOffsetDateTime();
var dateTimeOffset = offsetDateTime.ToDateTimeOffset();
var dateTime = dateTimeOffset.DateTime;
var localDateTime = dateTimeOffset.LocalDateTime;

conn.ExecuteNonQuery("CREATE TEMP TABLE data (d1 TIMESTAMPTZ, d2 TIMESTAMPTZ, d3 TIMESTAMPTZ, d4 TIMESTAMPTZ, d5 TIMESTAMPTZ)");
conn.ExecuteNonQuery("CREATE TEMP TABLE data (d1 TIMESTAMPTZ, d2 TIMESTAMPTZ, d3 TIMESTAMPTZ, d4 TIMESTAMPTZ, d5 TIMESTAMPTZ, d6 TIMESTAMPTZ)");

using (var cmd = new NpgsqlCommand("INSERT INTO data VALUES (@p1, @p2, @p3, @p4, @p5)", conn))
using (var cmd = new NpgsqlCommand("INSERT INTO data VALUES (@p1, @p2, @p3, @p4, @p5, @p6)", conn))
{
cmd.Parameters.Add(new NpgsqlParameter("p1", NpgsqlDbType.TimestampTz) { Value = instant });
cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "p2", Value = utcZonedDateTime });
cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "p3", Value = localZonedDateTime });
cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "p4", Value = offsetDateTime });
cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "p5", Value = dateTimeOffset });
cmd.Parameters.Add(new NpgsqlParameter { ParameterName = "p6", Value = dateTime });
cmd.ExecuteNonQuery();
}

using (var cmd = new NpgsqlCommand("SELECT d1::TEXT, d2::TEXT, d3::TEXT, d4::TEXT, d5::TEXT FROM data", conn))
using (var cmd = new NpgsqlCommand("SELECT d1::TEXT, d2::TEXT, d3::TEXT, d4::TEXT, d5::TEXT, d6::TEXT FROM data", conn))
using (var reader = cmd.ExecuteReader())
{
reader.Read();
Expand All @@ -178,7 +181,7 @@ public void TimestampTz()
Assert.That(reader.GetFieldValue<OffsetDateTime>(i), Is.EqualTo(offsetDateTime));
Assert.That(reader.GetFieldValue<DateTimeOffset>(i), Is.EqualTo(dateTimeOffset));
Assert.That(() => reader.GetFieldValue<LocalDateTime>(i), Throws.TypeOf<InvalidCastException>());
Assert.That(() => reader.GetDateTime(i), Throws.TypeOf<InvalidCastException>());
Assert.That(() => reader.GetDateTime(i), Is.EqualTo(localDateTime));
Assert.That(() => reader.GetDate(i), Throws.TypeOf<InvalidCastException>());
}
}
Expand Down

0 comments on commit 2bdb08d

Please sign in to comment.