Skip to content

Commit 543638c

Browse files
authored
fix: Try formatting timestamptz in a few formats (#322)
Looks like there is no good relient way to autodetect formats so better to keep it simple.
1 parent a24ce80 commit 543638c

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

cqtypes/timestamptz.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// const pgTimestamptzHourFormat = "2006-01-02 15:04:05.999999999Z07"
1010
// const pgTimestamptzMinuteFormat = "2006-01-02 15:04:05.999999999Z07:00"
11-
const pgTimestamptzSecondFormat = "2006-01-02 15:04:05.999999999Z07:00:00"
11+
// const pgTimestamptzSecondFormat = "2006-01-02 15:04:05.999999999Z07:00:00"
1212

1313
// this is the default format used by time.Time.String()
1414
const defaultStringFormat = "2006-01-02 15:04:05.999999999 -0700 MST"
@@ -40,7 +40,7 @@ func (dst *Timestamptz) Equal(src CQType) bool {
4040

4141
func (dst *Timestamptz) String() string {
4242
if dst.Status == Present {
43-
return dst.Time.Format(pgTimestamptzSecondFormat)
43+
return dst.Time.Format(time.RFC3339)
4444
} else {
4545
return ""
4646
}
@@ -119,25 +119,20 @@ func (dst *Timestamptz) DecodeText(src []byte) error {
119119
case "-infinity":
120120
*dst = Timestamptz{Status: Present, InfinityModifier: -Infinity}
121121
default:
122-
var format string
123-
sbufLen := len(sbuf)
124-
if sbufLen >= 19 && sbuf[10] == 'T' && sbuf[19] == 'Z' {
125-
format = time.RFC3339
126-
} else if sbufLen >= 19 && sbuf[10] == 'T' && sbuf[19] == '.' {
127-
format = time.RFC3339Nano
128-
} else {
129-
if sbufLen >= len(defaultStringFormat) {
130-
sbuf = sbuf[:len(defaultStringFormat)]
131-
}
132-
format = defaultStringFormat
122+
var tim time.Time
123+
var err error
124+
// there is no good way of detecting format so we just try few of them
125+
tim, err = time.Parse(time.RFC3339, sbuf)
126+
if err == nil {
127+
*dst = Timestamptz{Time: normalizePotentialUTC(tim), Status: Present}
128+
return nil
133129
}
134-
135-
tim, err := time.Parse(format, sbuf)
136-
if err != nil {
137-
return err
130+
tim, err = time.Parse(defaultStringFormat, sbuf)
131+
if err == nil {
132+
*dst = Timestamptz{Time: normalizePotentialUTC(tim), Status: Present}
138133
}
134+
return fmt.Errorf("cannot parse %s as Timestamptz", sbuf)
139135

140-
*dst = Timestamptz{Time: normalizePotentialUTC(tim), Status: Present}
141136
}
142137

143138
return nil

0 commit comments

Comments
 (0)