【问题标题】:What is best practice when storing RFC3339 time in a MySQL database?在 MySQL 数据库中存储 RFC3339 时间时的最佳实践是什么?
【发布时间】:2017-02-20 15:49:48
【问题描述】:

我在 Google Cloud Datastore NoSQL 数据库中有数据。此数据库中的对象包含type Time.time 的字段,特别是 RFC3339 格式 (YYYY-MM-DDThh:mm:ssTZD)。我正在将其中一些数据移动到 Google Cloud SQL 数据库中,我想知道在 MySQL 数据库中存储 RFC3339 格式的时间数据时,什么是最佳实践。

This post 强烈建议如下:

所有日期和时间列应为INT UNSIGNED NOT NULL,并且应 以 UTC 格式存储 Unix 时间戳。

那么,我应该只使用Golang's func (t Time) Unix() 并以这种方式存储数据吗?存储数据的替代方法有哪些?如果我不听发帖者的建议,我可能会遇到哪些问题?

【问题讨论】:

    标签: mysql go time unix-timestamp google-cloud-sql


    【解决方案1】:

    如果您使用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)
    

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 2011-02-14
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多