我在 Google Cloud Datastore NoSQL 数据库中有数据。此数据库中的对象包含 type Time.time
的字段,特别是 RFC3339 格式 (YYYY-MM-DDThh:mm:ssTZD)。我正在将其中一些数据移动到 Google Cloud SQL 数据库中,我想知道在 MySQL 数据库中存储 RFC3339 格式的时间数据时什么是最佳实践。
This post强烈建议以下内容:
All date and time columns shall be
INT UNSIGNED NOT NULL
, and shall store a Unix timestamp in UTC.
那么,我应该只使用 Golang's func (t Time) Unix()
吗?并以这种方式存储数据?有哪些替代方法来存储数据?如果我不听从发布者的建议,我可能会遇到什么样的问题?
最佳答案
如果您正在使用 go-sql-driver/mysql
,您可以通过添加 parseTime=true
要求驱动程序自动将 DATE 和 DATETIME 扫描到 time.Time > 到您的连接字符串。
参见 https://github.com/go-sql-driver/mysql#timetime-support
示例代码:
db, _ := sql.Open("mysql", "root:@/?parseTime=true")
var myTime time.Time
row, err := db.QueryRow("SELECT current_timestamp()")
row.Scan(&myTime)
fmt.Println(myTime)
关于mysql - 在 MySQL 数据库中存储 RFC3339 时间时的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39988506/