服务器版本及gorm包:❯ docker exec -it mysql mysqld --versionmysqld Ver 5.7.29 for Linux on x86_64 (MySQL Community Server (GPL))❯ docker exec -it mysql mysql --versionmysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapperimport "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{}:// t1type T1 struct { ID uint `gorm:"primary_key"` // others are here // ...期待:请注意,我位于asia/dhaka(+06:00) 区域。created_at表的时间t1是我所在地区的BST当前时间。另一方面,表的时间initiated_at是t2UTC当前时间。但我希望这两个时间是相同的(我的意思是 UTC 或 BST)。想知道:之所以两次的地域不同。任何解决方案,使两个时间都在同一区域
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
在表中t1,根据您使用的本地时区CreatedAt设置 。Gormloc=Local
在 Table 中t2,initiated_at不是由Gorm它设置的,Mysql因为您使用默认值,CURRENT_TIMESTAMP如MySql。
解决方案:
您可以使用连接将Gorm时区更改为 UTC 。loc=UTC
或者
您可以将本地时区设置Mysql为时区。请注意,设置时间的位置。时间值,但不会更改 MySQL 的 time_zone 设置。为此,请参阅 time_zone 系统变量,它也可以设置为 DSN 参数。
- 1 回答
- 0 关注
- 216 浏览
添加回答
举报
0/150
提交
取消
