Skip to content

Commit

Permalink
Fix BC dates in text format
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Mar 12, 2022
1 parent 4696622 commit 0c166c7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
54 changes: 43 additions & 11 deletions pgtype/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/jackc/pgx/v5/internal/pgio"
Expand Down Expand Up @@ -182,18 +183,32 @@ func (encodePlanDateCodecText) Encode(value interface{}, buf []byte) (newBuf []b
return nil, nil
}

var s string

switch date.InfinityModifier {
case Finite:
s = date.Time.Format("2006-01-02")
// Year 0000 is 1 BC
bc := false
year := date.Time.Year()
if year <= 0 {
year = -year + 1
bc = true
}

buf = strconv.AppendInt(buf, int64(year), 10)
buf = append(buf, '-')
buf = strconv.AppendInt(buf, int64(date.Time.Month()), 10)
buf = append(buf, '-')
buf = strconv.AppendInt(buf, int64(date.Time.Day()), 10)

if bc {
buf = append(buf, " BC"...)
}
case Infinity:
s = "infinity"
buf = append(buf, "infinity"...)
case NegativeInfinity:
s = "-infinity"
buf = append(buf, "-infinity"...)
}

return append(buf, s...), nil
return buf, nil
}

func (DateCodec) PlanScan(m *Map, oid uint32, format int16, target interface{}) ScanPlan {
Expand Down Expand Up @@ -256,12 +271,29 @@ func (scanPlanTextAnyToDateScanner) Scan(src []byte, dst interface{}) error {
case "-infinity":
return scanner.ScanDate(Date{InfinityModifier: -Infinity, Valid: true})
default:
t, err := time.ParseInLocation("2006-01-02", sbuf, time.UTC)
if err != nil {
return err
if len(sbuf) >= 10 {
year, err := strconv.ParseInt(sbuf[0:4], 10, 32)
if err != nil {
return fmt.Errorf("cannot parse year: %v", err)
}
month, err := strconv.ParseInt(sbuf[5:7], 10, 32)
if err != nil {
return fmt.Errorf("cannot parse month: %v", err)
}
day, err := strconv.ParseInt(sbuf[8:10], 10, 32)
if err != nil {
return fmt.Errorf("cannot parse day: %v", err)
}

if len(sbuf) == 13 && sbuf[11:] == "BC" {
year = -year + 1
}

t := time.Date(int(year), time.Month(month), int(day), 0, 0, 0, 0, time.UTC)
return scanner.ScanDate(Date{Time: t, Valid: true})
} else {
return fmt.Errorf("date too short")
}

return scanner.ScanDate(Date{Time: t, Valid: true})
}
}

Expand Down
4 changes: 4 additions & 0 deletions pgtype/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func isExpectedEqTime(a interface{}) func(interface{}) bool {

func TestDateCodec(t *testing.T) {
testutil.RunTranscodeTests(t, "date", []testutil.TranscodeTestCase{
{time.Date(-100, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(-100, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(-1, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(-1, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))},
{time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), new(time.Time), isExpectedEqTime(time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC))},
Expand Down

0 comments on commit 0c166c7

Please sign in to comment.