go中time.Time使用Insert和Exec插入的结果时区不同 #1770

Closed
opened 2020-08-24 09:11:06 +00:00 by Mr1X · 0 comments

问题

go结构体字段类型为 time.Time的时候,使用xorm的 Insert方法和 Exec方法写入数据,得到的时区不一样

表结构

CREATE TABLE `time_date2` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT,
	`PubDate` DATE  DEFAULT NULL,
	`PubTime1` DATETIME  DEFAULT NULL,
	`PubTime2` timestamp  DEFAULT NULL,
    PRIMARY KEY (`ID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='时间测试表';

代码

type TimeDate struct {
	PubDate  time.Time `xorm:"PubDate "`
	PubTime1 time.Time `xorm:"PubTime1"`
	PubTime2 time.Time `xorm:"PubTime2"`
}

func TestTimeDate(t *testing.T) {

	now := time.Now()
	obj := &TimeDate{
		PubDate:  now,
		PubTime1: now,
		PubTime2: now,
	}

	_, err = db.Table("time_date").Insert(obj)
	log.Info(err)

	_, err = db.Exec("insert into time_date (PubDate, PubTime1, PubTime2) values (?, ?, ?)",
		obj.PubDate, obj.PubTime1, obj.PubTime2)
	log.Info(err)
}

写入结果,Insert是+8时区, Exec变成了0时区

| ID | PubDate | PubTime1 | PubTime2 |
|------+------------+---------------------+---------------------|
| 1 | 2020-08-24 | 2020-08-24 16:30:16 | 2020-08-24 16:30:16 |
| 2 | 2020-08-24 | 2020-08-24 08:30:17 | 2020-08-24 08:30:17

### 问题 go结构体字段类型为 time.Time的时候,使用xorm的 Insert方法和 Exec方法写入数据,得到的时区不一样 #### 表结构 ``` CREATE TABLE `time_date2` ( `ID` bigint(20) NOT NULL AUTO_INCREMENT, `PubDate` DATE DEFAULT NULL, `PubTime1` DATETIME DEFAULT NULL, `PubTime2` timestamp DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='时间测试表'; ``` ### 代码 ``` type TimeDate struct { PubDate time.Time `xorm:"PubDate "` PubTime1 time.Time `xorm:"PubTime1"` PubTime2 time.Time `xorm:"PubTime2"` } func TestTimeDate(t *testing.T) { now := time.Now() obj := &TimeDate{ PubDate: now, PubTime1: now, PubTime2: now, } _, err = db.Table("time_date").Insert(obj) log.Info(err) _, err = db.Exec("insert into time_date (PubDate, PubTime1, PubTime2) values (?, ?, ?)", obj.PubDate, obj.PubTime1, obj.PubTime2) log.Info(err) } ``` 写入结果,Insert是+8时区, Exec变成了0时区 | ID | PubDate | PubTime1 | PubTime2 | |------+------------+---------------------+---------------------| | 1 | 2020-08-24 | 2020-08-24 16:30:16 | 2020-08-24 16:30:16 | | 2 | 2020-08-24 | 2020-08-24 08:30:17 | 2020-08-24 08:30:17
lunny added the
kind
bug
label 2020-08-24 13:36:10 +00:00
lunny added this to the 1.2.0 milestone 2021-06-09 03:35:06 +00:00
lunny closed this issue 2021-07-14 04:20:27 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1770
No description provided.