今天给大家带来了《解析 postgresql TIMESTAMP 类型的值时出现问题》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~
问题内容
在 postgresql 中,我有一个名为 surveys 的表。
create table surveys(
survey_id uuid primary key not null default uuid_generate_v4(),
survey_name varchar not null,
survey_description text,
start_period timestamp,
end_period timestamp
);
正如您所看到的,只有 survey_id 和 survey_name 列是 not null。
在 Go 中,我想通过 post 请求在该表中创建新条目。我像这样发送 JSON 对象:
{
"survey_name": "name",
"survey_description": "description",
"start_period": "2019-01-01 00:00:00",
"end_period": "2019-02-28 23:59:59"
}
不幸的是,它引发了奇怪的错误:
parsing time ""2019-01-01 00:00:00"" as ""2006-01-02t15:04:05z07:00"": cannot parse " 00:00:00"" as "t"
我在哪里犯了错误以及如何解决我的问题?
models/surveys.go:
import (
"database/sql"
"time"
)
type nulltime struct {
time.time
valid bool
}
type survey struct {
id int `json:"survey_id"`
name string `json:"survey_name"`
description sql.nullstring `json:"survey_description"`
startperiod nulltime `json:"start_period"`
endperiod nulltime `json:"end_period"`
}
controllers/surveys.go:
var CreateSurvey = func(responseWriter Http.ResponseWriter, request *http.Request) {
// Initialize variables.
survey := models.Survey{}
var err error
// The decoder introduces its own buffering and may read data from argument beyond the JSON values requested.
err = json.NewDecoder(request.Body).Decode(&survey)
if err != nil {
log.Println(err)
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
return
}
defer request.Body.Close()
// Execute INSERT SQL statement.
_, err = database.DB.Exec("INSERT INTO surveys (survey_name, survey_description, start_period, end_period) VALUES ($1, $2, $3, $4);", survey.Name, survey.Description, survey.StartPeriod, survey.EndPeriod)
// Shape the response depending on the result of the previous command.
if err != nil {
log.Println(err)
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
return
}
utils.ResponseWithSuccess(responseWriter, http.StatusCreated, "The new entry successfully created.")
}
解决方案
错误已经说明了问题所在:
将时间“2019-01-01 00:00:00”解析为“2006-01-02t15:04:05z07:00”:无法将“00:00:00”解析为“t”
您正在传递 "2019-01-01 00:00:00",但它需要不同的时间格式,即 RFC3339 (UnmarshalJSON's default)。
要解决此问题,您要么想要以预期格式 "2019-01-01t00:00:00z00:00" 传递时间,要么定义自己的类型 customtime,如下所示:
const timefORMat = "2006-01-02 15:04:05"
type customtime time.time
func (ct *customtime) unmarshaljson(data []byte) error {
newtime, err := time.parse(timeformat, strings.trim(string(data), "\""))
if err != nil {
return err
}
*ct = customtime(newtime)
return nil
}
func (ct *customtime) marshaljson() ([]byte, error) {
return []byte(fmt.sprintf("%q", time.time(*ct).format(timeformat))), nil
}
小心,您可能还需要实现 valuer 和 scanner 接口,以便在数据库中解析时间,如下所示:
func (ct CustomTime) Value() (driver.Value, error) {
return time.Time(ct), nil
}
func (ct *CustomTime) Scan(src interface{}) error {
if val, ok := src.(time.Time); ok {
*ct = CustomTime(val)
} else {
return errors.New("time Scanner passed a non-time object")
}
return nil
}
Go Playground 示例。
今天关于《解析 PostgreSQL TIMESTAMP 类型的值时出现问题》的内容就介绍到这里了,是不是学起来一目了然!