I make a HTTP call and unmarshal a createdTimestamp field from the larger json object:
CreatedTimestamp string `json:"createdTimestamp"`
An Example of what I receive from the HTTP call for the createdTimestamp would be: "2021-07-19T18:51:23".
It won't automatically convert it to a time.Time so the only type it would really accept is a string which was working until the type changed in Postgresql to timestamp with timezone < looks like this according to Postgresql db console >(yyyy-MM-dd HH:mm:ss.ffffff). where as before it was without timezone.
I am able to implement a custom unmarshal method for the time.Time object and even format it so I can output something like 2021-07-19 18:51:23.+00 but this won't be accepted by postgres.
The exact error I see when trying to insert this (via GoLang, postgres driver) is: pq: invalid input syntax for type timestamp with time zone: ""
This error comes after I try to execute an insert using db.Exec -> db being of type *sql.DB.
I'm doing this in Go , any help would be greatly appreciated!
Code to Unmarshal Json
type CustomTime struct {
time.Time
}
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
timeToInsertIntoDb := strings.Trim(string(b), "\"")
if timeToInsertIntoDb == "null" {
ct.Time = time.Time{}
return
}
timeToInsertIntoDb = timeToInsertIntoDb + "Z"
ct.Time, err = time.Parse(time.RFC3339, timeToInsertIntoDb)
return
}
With formatting, it depends on the output but I do receive the output of whatever the format is. So if I do, CustomTime.Time.Format(2006-02-01 15:04:05.-07)
I will get the output of 2021-07-19 18:51:23.+00
Though at this point, i'm not even sure about the exact format needed for Timestamp with Timezone, there isn't too much documentation on this for the Golang Postgres driver.
If there is any more information needed, please ask. I'm trying my best to organize this question.
Edit 1
As suggested, I tried to append on a 'Z' to the timestamp given from the http call. After doing a parse with time.RFC3339, I am given a time of 2021-07-19T18:51:23Z - this still failed (gave the same syntax error stated above). Tried it a few different ways with this parsing. With it formatted the way I stated above, and with it formatted with it's .String() method which would give 2021-07-20 18:51:23 +0000 UTC. Both failed with pq: invalid input syntax for type timestamp with time zone: ""
Code Changes during Unmarshal:
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
timeToInsertIntoDb := strings.Trim(string(b), "\"")
if timeToInsertIntoDb == "null" {
ct.Time = time.Time{}
return
}
timeToInsertIntoDb = timeToInsertIntoDb + "Z"
ct.Time, err = time.Parse(time.RFC3339, timeToInsertIntoDb)
return
}
Edit 2
Another thing to mention would be that I am using the "database/sql" package with the "github.com/lib/pq" as the Postgres driver for DB connection.
Which is why the error is from pq. Just wanted to clarify cause I know others are using gorm. I can write to other tables, it's just this table having the timestamp with timezone Postgres Column I guess.
I am making the call with db.Exec("INSERT INTO db (created_timestamp) VALUES ($1)", obj.createdTimestamp.Time
I've tried passing it along as a string (it's what I did before when it was working) but now this is where i'm at since people say it's better to pass a time.Time variable.