我想使用 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 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
另一種方法是讓 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`
}
now()這將告訴 go-pg / Postgres在插入新記錄時使用。然后,您可以在代碼中處理時區和內容,而數據庫將始終保留 UTC 時間戳。您可能需要使用 NTP 或類似方法正確配置服務器的時間。
- 2 回答
- 0 關注
- 190 瀏覽
添加回答
舉報
0/150
提交
取消