2
0

Add tests for big time and port fix to Timestamp.DecodeBinary

https://github.com/jackc/pgtype/pull/128
This commit is contained in:
Jack Christensen
2021-10-23 09:55:56 -05:00
parent e28459e9d1
commit 5cb98120c1
3 changed files with 46 additions and 2 deletions
+4 -2
View File
@@ -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}
}
+21
View File
@@ -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
+21
View File
@@ -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