From 5cb98120c10c0ed3125786007d6c0861d34b8f79 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 23 Oct 2021 09:55:56 -0500 Subject: [PATCH] Add tests for big time and port fix to Timestamp.DecodeBinary https://github.com/jackc/pgtype/pull/128 --- timestamp.go | 6 ++++-- timestamp_test.go | 21 +++++++++++++++++++++ timestamptz_test.go | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/timestamp.go b/timestamp.go index 46644115..a184d232 100644 --- a/timestamp.go +++ b/timestamp.go @@ -141,8 +141,10 @@ func (dst *Timestamp) DecodeBinary(ci *ConnInfo, src []byte) error { case negativeInfinityMicrosecondOffset: *dst = Timestamp{Status: Present, InfinityModifier: -Infinity} default: - microsecSinceUnixEpoch := microsecFromUnixEpochToY2K + microsecSinceY2K - tim := time.Unix(microsecSinceUnixEpoch/1000000, (microsecSinceUnixEpoch%1000000)*1000).UTC() + tim := time.Unix( + microsecFromUnixEpochToY2K/1000000+microsecSinceY2K/1000000, + (microsecFromUnixEpochToY2K%1000000*1000)+(microsecSinceY2K%1000000*1000), + ) *dst = Timestamp{Time: tim, Status: Present} } diff --git a/timestamp_test.go b/timestamp_test.go index 74cb1221..ea7ef57a 100644 --- a/timestamp_test.go +++ b/timestamp_test.go @@ -1,6 +1,7 @@ package pgtype_test import ( + "context" "reflect" "testing" "time" @@ -33,6 +34,26 @@ func TestTimestampTranscode(t *testing.T) { }) } +// https://github.com/jackc/pgtype/pull/128 +func TestTimestampTranscodeBigTimeBinary(t *testing.T) { + conn := testutil.MustConnectPgx(t) + if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok { + t.Skip("Skipping due to no line type") + } + defer testutil.MustCloseContext(t, conn) + + in := &pgtype.Timestamp{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Status: pgtype.Present} + var out pgtype.Timestamp + + err := conn.QueryRow(context.Background(), "select $1::timestamptz", in).Scan(&out) + if err != nil { + t.Fatal(err) + } + + require.Equal(t, in.Status, out.Status) + require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time) +} + func TestTimestampNanosecondsTruncated(t *testing.T) { tests := []struct { input time.Time diff --git a/timestamptz_test.go b/timestamptz_test.go index 769c9239..c3f63967 100644 --- a/timestamptz_test.go +++ b/timestamptz_test.go @@ -1,6 +1,7 @@ package pgtype_test import ( + "context" "reflect" "testing" "time" @@ -33,6 +34,26 @@ func TestTimestamptzTranscode(t *testing.T) { }) } +// https://github.com/jackc/pgtype/pull/128 +func TestTimestamptzTranscodeBigTimeBinary(t *testing.T) { + conn := testutil.MustConnectPgx(t) + if _, ok := conn.ConnInfo().DataTypeForName("line"); !ok { + t.Skip("Skipping due to no line type") + } + defer testutil.MustCloseContext(t, conn) + + in := &pgtype.Timestamptz{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Status: pgtype.Present} + var out pgtype.Timestamptz + + err := conn.QueryRow(context.Background(), "select $1::timestamptz", in).Scan(&out) + if err != nil { + t.Fatal(err) + } + + require.Equal(t, in.Status, out.Status) + require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time) +} + func TestTimestamptzNanosecondsTruncated(t *testing.T) { tests := []struct { input time.Time