Skip to content

Commit 7803ec3

Browse files
Fix pgtype.Timestamp json unmarshal
Add the missing 'Z' at the end of the timestamp string, so it can be parsed as timestamp in the RFC3339 format.
1 parent fd0c654 commit 7803ec3

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

pgtype/timestamp.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func (ts *Timestamp) UnmarshalJSON(b []byte) error {
104104
case "-infinity":
105105
*ts = Timestamp{Valid: true, InfinityModifier: -Infinity}
106106
default:
107-
// PostgreSQL uses ISO 8601 for to_json function and casting from a string to timestamptz
108-
tim, err := time.Parse(time.RFC3339Nano, *s)
107+
// PostgreSQL uses ISO 8601 wihout timezone for to_json function and casting from a string to timestampt
108+
tim, err := time.Parse(time.RFC3339Nano, *s+"Z")
109109
if err != nil {
110110
return err
111111
}
@@ -225,7 +225,6 @@ func discardTimeZone(t time.Time) time.Time {
225225
}
226226

227227
func (c *TimestampCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
228-
229228
switch format {
230229
case BinaryFormatCode:
231230
switch target.(type) {

pgtype/timestamp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ func TestTimestampUnmarshalJSON(t *testing.T) {
128128
result pgtype.Timestamp
129129
}{
130130
{source: "null", result: pgtype.Timestamp{}},
131-
{source: "\"2012-03-29T10:05:45Z\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC), Valid: true}},
132-
{source: "\"2012-03-29T10:05:45.555Z\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}},
131+
{source: "\"2012-03-29T10:05:45\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC), Valid: true}},
132+
{source: "\"2012-03-29T10:05:45.555\"", result: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}},
133133
{source: "\"infinity\"", result: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}},
134134
{source: "\"-infinity\"", result: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}},
135135
}

0 commit comments

Comments
 (0)