gpt4 book ai didi

mysql - 默认时间戳在不同的mysql表中具有不同的时区

转载 作者:行者123 更新时间:2023-12-01 22:15:05 25 4
gpt4 key购买 nike

服务器版本 & gorm包裹:

❯ docker exec -it mysql mysqld --version
mysqld Ver 5.7.29 for Linux on x86_64 (MySQL Community Server (GPL))

❯ docker exec -it mysql mysql --version
mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper

import "github.com/jinzhu/gorm"

两张表:

mysql> desc t1;
+------------+-------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------------+------+-----+-------------------+----------------+
| ... | | | | | |
| created_at | timestamp | YES | | CURRENT_TIMESTAMP | |
+------------+-------------------+------+-----+-------------------+----------------+

mysql> desc t2;
+----------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+-------------------+----------------+
| ... | ... | ... | ... | ... | ... |
| initiated_at | timestamp | YES | | CURRENT_TIMESTAMP | |
+----------------+------------------+------+-----+-------------------+----------------+

gorm定义 stuct{} 时的模型如下:

// t1
type T1 struct {
ID uint `gorm:"primary_key"`
// others are here
// ...

CreatedAt time.Time `gorm:"timestamp;default:CURRENT_TIMESTAMP" json:"created_at" form:"created_at" query:"created_at" sql:"DEFAULT:current_timestamp"`
}

// t2 ...
type T2 struct {
ID uint `gorm:"primary_key"`
// others are here
// ...
InitiatedAt time.Time `gorm:"timestamp;default:CURRENT_TIMESTAMP" json:"initiated_at" form:"initiated_at" query:"initiated_at" sql:"DEFAULT:current_timestamp"`
}

使用未初始化的时间戳插入

dbSource := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
cnf.Username,
cnf.Password,
cnf.Host,
cnf.Port,
cnf.DBName,
)

db, err := gorm.Open("mysql", dbSource)
if err != nil {
logrus.Warn("Got error when connect database:", err)
return err
}

t1 := T1{} // created_at is not set
t2 := T2{} // initiated_at is not set

tx := db.Begin()
defer func() {
if r := recover(); r != nil {
logrus.Error("Rolling back")
tx.Rollback()
}
}()
// If failed to begin transaction
if err := tx.Error; err != nil {
return err
}

if err := db.Create(&t1).Error; err != nil {
logrus.Warn(err)
// rollback the transaction in case of error
tx.Rollback()
return derror.ErrorBadRequest
}
if err := db.Create(&t2).Error; err != nil {
logrus.Warn(err)
// rollback the transaction in case of error
tx.Rollback()
return derror.ErrorBadRequest
}

// Or commit the transaction
if err := tx.Commit().Error; err != nil {
logrus.Warn(err)
// rollback the transaction in case of error
tx.Rollback()
return derror.ErrorBadRequest
}

我所看到的,什么时候选择查询

mysql> select * from t1;
+-----+---------------------+
| ... | created_at |
+-----+---------------------+
| ... | 2020-03-24 02:38:26 |
+-----+---------------------+

mysql> select * from t2;
+-----+---------------------+
| ... | initiated_at |
+-----+---------------------+
| ... | 2020-03-23 20:38:26 |
+-----+---------------------+

期望:

请注意,我在 asia/dhaka (+06:00) 地区。和 created_at的时间的 t1表是我所在地区的 BST 当前时间。另一方面, initiated_at 的时间的 t2表是 UTC 当前时间。

但我希望这两个时间都是相同的(我的意思是 UTC 或 BST)。

想知道:
  • 这两次是不同地区的原因。
  • 任何解决方案,使两个时间都在同一区域
  • 最佳答案

    表中t1 , CreatedAtGorm 设置在本地时区,因为您使用 loc=Local .

    引用:https://github.com/jinzhu/gorm/blob/master/callback_create.go#L32

    并在表 t2 , initiated_at不是由 Gorm 设置的它由 Mysql 设置因为您使用默认值作为 CURRENT_TIMESTAMPMySql .

    解决方案:

    您可以更改Gorm使用 loc=UTC 将时区转换为 UTC在连接。

    或者

    您可以将本地时区设置为 Mysql时区。
    请注意,设置 time.Time 值的位置,但不会更改 MySQL 的 time_zone 设置。为此,请参阅 time_zone 系统变量,它也可以设置为 DSN 参数。

    引用:https://github.com/go-sql-driver/mysql#loc

    关于mysql - 默认时间戳在不同的mysql表中具有不同的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60821753/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com