pgxtype-experiment2
Jack Christensen 2017-02-22 18:08:05 -06:00
parent 0ca0ee7206
commit 83a5447cd2
3 changed files with 114 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import (
type Date struct {
// time.Time is embedded to hide internal implementation. Possibly do date
// implementation at some point rather than simply delegating to time.Time.
// Also TODO handling Infinity and -Infinity
t time.Time
}
@ -21,7 +22,7 @@ func (d *Date) DecodeText(r io.Reader) error {
}
if size == -1 {
return fmt.Errorf("invalid length for int8: %v", size)
return fmt.Errorf("invalid length for date: %v", size)
}
buf := make([]byte, int(size))

96
pgtype/timestamptz.go Normal file
View File

@ -0,0 +1,96 @@
package pgtype
import (
"fmt"
"io"
"time"
"github.com/jackc/pgx/pgio"
)
const pgTimestamptzFormat = "2006-01-02 15:04:05.999999999Z07:00"
const microsecFromUnixEpochToY2K = 946684800 * 1000000
type Timestamptz struct {
// time.Time is embedded to handle Infinity and -Infinity
// TODO - infinity
t time.Time
}
func (t *Timestamptz) DecodeText(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {
return err
}
if size == -1 {
return fmt.Errorf("invalid length for timestamptz: %v", size)
}
buf := make([]byte, int(size))
_, err = r.Read(buf)
if err != nil {
return err
}
t.t, err = time.Parse(pgTimestamptzFormat, string(buf))
if err != nil {
return err
}
return nil
}
func (t *Timestamptz) DecodeBinary(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {
return err
}
if size != 8 {
return fmt.Errorf("invalid length for timestamptz: %v", size)
}
microsecSinceY2K, err := pgio.ReadInt64(r)
if err != nil {
return err
}
microsecSinceUnixEpoch := microsecFromUnixEpochToY2K + microsecSinceY2K
t.t = time.Unix(microsecSinceUnixEpoch/1000000, (microsecSinceUnixEpoch%1000000)*1000)
return nil
}
func (t Timestamptz) EncodeText(w io.Writer) error {
buf := []byte(t.t.Format(pgTimestamptzFormat))
_, err := pgio.WriteInt32(w, int32(len(buf)))
if err != nil {
return nil
}
_, err = w.Write(buf)
return err
}
func (t Timestamptz) EncodeBinary(w io.Writer) error {
_, err := pgio.WriteInt32(w, 8)
if err != nil {
return err
}
microsecSinceUnixEpoch := t.t.Unix()*1000000 + int64(t.t.Nanosecond())/1000
microsecSinceY2K := microsecSinceUnixEpoch - microsecFromUnixEpochToY2K
_, err = pgio.WriteInt64(w, microsecSinceY2K)
return err
}
func (t Timestamptz) Time() time.Time {
return t.t
}
func TimestamptzFromTime(t time.Time) Timestamptz {
return Timestamptz{t: t}
}

View File

@ -2033,13 +2033,7 @@ func encodeTime(w *WriteBuf, oid OID, value time.Time) error {
case DateOID:
return pgtype.DateFromTime(value).EncodeBinary(w)
case TimestampTzOID, TimestampOID:
microsecSinceUnixEpoch := value.Unix()*1000000 + int64(value.Nanosecond())/1000
microsecSinceY2K := microsecSinceUnixEpoch - microsecFromUnixEpochToY2K
w.WriteInt32(8)
w.WriteInt64(microsecSinceY2K)
return nil
return pgtype.TimestamptzFromTime(value).EncodeBinary(w)
default:
return fmt.Errorf("cannot encode %s into oid %v", "time.Time", oid)
}
@ -2060,19 +2054,26 @@ func decodeTimestampTz(vr *ValueReader) time.Time {
return zeroTime
}
if vr.Type().FormatCode != BinaryFormatCode {
vr.err = errRewoundLen
var t pgtype.Timestamptz
var err error
switch vr.Type().FormatCode {
case TextFormatCode:
err = t.DecodeText(&valueReader2{vr})
case BinaryFormatCode:
err = t.DecodeBinary(&valueReader2{vr})
default:
vr.Fatal(ProtocolError(fmt.Sprintf("Unknown field description format code: %v", vr.Type().FormatCode)))
return zeroTime
return time.Time{}
}
if vr.Len() != 8 {
vr.Fatal(ProtocolError(fmt.Sprintf("Received an invalid size for an timestamptz: %d", vr.Len())))
return zeroTime
if err != nil {
vr.Fatal(err)
return time.Time{}
}
microsecSinceY2K := vr.ReadInt64()
microsecSinceUnixEpoch := microsecFromUnixEpochToY2K + microsecSinceY2K
return time.Unix(microsecSinceUnixEpoch/1000000, (microsecSinceUnixEpoch%1000000)*1000)
return t.Time()
}
func decodeTimestamp(vr *ValueReader) time.Time {