Skip to content

Commit

Permalink
Support postgres dates with "BC"
Browse files Browse the repository at this point in the history
  • Loading branch information
jozolam authored and jackc committed Nov 28, 2022
1 parent e19b507 commit 472e3ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions date.go
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/jackc/pgio"
Expand Down Expand Up @@ -138,6 +139,15 @@ func (dst *Date) DecodeText(ci *ConnInfo, src []byte) error {
case "-infinity":
*dst = Date{Status: Present, InfinityModifier: -Infinity}
default:
if strings.HasSuffix(sbuf, " BC") {
t, err := time.ParseInLocation("2006-01-02", strings.TrimRight(sbuf, " BC"), time.UTC)
t2 := time.Date(1-t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
if err != nil {
return err
}
*dst = Date{Time: t2, Status: Present}
return nil
}
t, err := time.ParseInLocation("2006-01-02", sbuf, time.UTC)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions date_test.go
Expand Up @@ -76,6 +76,7 @@ func TestDateSet(t *testing.T) {
{source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), result: pgtype.Date{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
{source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)), result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
{source: "1999-12-31", result: pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
{source: "0001-01-01 BC", result: pgtype.Date{Time: time.Date(0000, 01, 01, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
{source: customDate{t: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)}, result: pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}},
}

Expand Down

0 comments on commit 472e3ae

Please sign in to comment.