2016-10-11 23:53
浏览 226


I have data in a Google Cloud Datastore NoSQL database. The objects in this database contain fields of type Time.time, specifically RFC3339 format (YYYY-MM-DDThh:mm:ssTZD). I am moving some of this data into a Google Cloud SQL database and I am wondering what is considered best practice when storing RFC3339 formatted time data in a MySQL database.

This post strongly suggests the following:

All date and time columns shall be INT UNSIGNED NOT NULL, and shall store a Unix timestamp in UTC.

So, should I just make use of Golang's func (t Time) Unix() and store the data that way? What are some alternative approaches to storing the data? What kinds of issues might I run into if I don't heed the poster's advice?

图片转代码服务由CSDN问答提供 功能建议

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

这篇文章强烈建议以下内容: < blockquote>

所有日期和时间列均应为 INT UNSIGNED NOT NULL ,并应 在UTC中存储Unix时间戳。 \ n

所以,我应该只使用 Golang的 func(t Time)Unix()< / code> 并以这种方式存储数据? 有哪些替代方法可以存储数据? 如果我不注意发布者的建议,会遇到什么问题?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtv7174 2016-10-12 03:55

    If you're using the go-sql-driver/mysql you can ask the driver to scan DATE and DATETIME automatically to time.Time, by adding parseTime=true to your connection string.


    Example code:

    db, _ := sql.Open("mysql", "root:@/?parseTime=true")
    var myTime time.Time
    row, err := db.QueryRow("SELECT current_timestamp()")
    打赏 评论

相关推荐 更多相似问题