我想使用 go-pg 将时间插入数据库,但插入后值会发生变化。toRound := time.Now()date := time.Date(toRound.Year(), toRound.Month(), toRound.Day(), 0, 0, 0, 0, toRound.Location())的值date是2020-03-18 00:00:00 +0700 WIB并插入使用go-pgreportMessage := &ReportMessage{ Total: ii, Date: date }_, err = p.ormer.Model(reportMessage).Returning("id").Insert()date插入后的值为2020-03-17 17:00:00+00:00:00看起来是因为时区如何在不受时区或其他任何影响的情况下将时间完全插入原始值?
2 回答

智慧大石
TA贡献1946条经验 获得超3个赞
另一种方法是让 go-pg ORM 自动处理这个问题。如果您将模型定义为:
type ReportMessage struct {
// Your data fields here
CreatedAt time.Time `pg:"default:now()" json:"created_at"`
UpdatedAt time.Time `pg:"default:now()" json:"updated_at`
}
这将告诉 go-pg / Postgres 在now()插入新记录时使用。然后,您可以在代码中处理时区和内容,而数据库将始终保持 UTC 时间戳。您可能需要使用 NTP 或类似方法正确配置服务器的时间。
- 2 回答
- 0 关注
- 125 浏览
添加回答
举报
0/150
提交
取消