Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid json unmarshal for pgtype.Timestamp #2128

Closed
s-montigny-desautels opened this issue Sep 23, 2024 · 0 comments · Fixed by #2129
Closed

Invalid json unmarshal for pgtype.Timestamp #2128

s-montigny-desautels opened this issue Sep 23, 2024 · 0 comments · Fixed by #2129
Labels

Comments

@s-montigny-desautels
Copy link
Contributor

Describe the bug
pgtype.Timestamp use time.RFC3338Nano which expects timezone information. However, postgres don't return timezone information from a timestamp without time zone. As such:

select to_json(now()::timestamp without time zone) ;
           to_json            
------------------------------
 "2024-09-23T16:14:51.152496"

To Reproduce
Steps to reproduce the behavior:

package main

import (
	"context"
	"log"
	"os"

	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgtype"
)

func main() {
	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close(context.Background())

	row := conn.QueryRow(context.Background(), "select to_json(now()::timestamp)")
	var strTime string
	if err := row.Scan(&strTime); err != nil {
		panic(err)
	}

	var t pgtype.Timestamp
	if err := json.Unmarshal([]byte(strTime), &t); err != nil {
		panic(err) <-- panic here
	}
}

Expected behavior
pgtype.Timestamp parse the format of the timestamp returned by postgres

Actual behavior
pgtype.Timestamp expect timezone information from the string, but timestamp don't have timezone information.

panic: parsing time "2024-09-23T16:07:53.607446" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse "" as "Z07:00"

Version

  • Go: go version go1.23.1 linux/amd64
  • PostgreSQL: PostgreSQL 16.3 on x86_64-pc-linux-gnu, compiled by Debian clang version 12.0.1, 64-bit
  • pgx: 5.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant